std::exit

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

# 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

DRApplied toBehavior as publishedCorrect behavior
LWG 3C++98during cleanup, the behavior was unclear when (1) a function isregistered with std::atexit or (2) a static local object is initializedmade clear

# See also