std::exit
Min standard notice:
Header: <cstdlib>
Causes normal program termination to occur.
# Declarations
void exit( int exit_code );
(until C++11)
[[noreturn]] void exit( int exit_code );
(since C++11)
# Parameters
exit_code: exit status of the program
# Return value
(none)
# Example
#include <cstdlib>
#include <iostream>
struct Static
{
~Static()
{
std::cout << "Static destructor\n";
}
};
struct Local
{
~Local()
{
std::cout << "Local destructor\n";
}
};
Static static_variable; // Destructor of this object *will* be called
void atexit_handler()
{
std::cout << "atexit handler\n";
}
int main()
{
Local local_variable; // Destructor of this object will *not* be called
const int result = std::atexit(atexit_handler); // Handler will be called
if (result != 0)
{
std::cerr << "atexit registration failed\n";
return EXIT_FAILURE;
}
std::cout << "test\n";
std::exit(EXIT_FAILURE);
std::cout << "this line will *not* be executed\n";
}
# Defect reports
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 3 | C++98 | during cleanup, the behavior was unclear when (1) a function isregistered with std::atexit or (2) a static local object is initialized | made clear |