std::atexit
Min standard notice:
Header: <cstdlib>
Registers the function pointed to by func to be called on normal program termination (via std::exit() or returning from the main function)
# Declarations
int atexit( /* c-atexit-handler */* func );
int atexit( /* atexit-handler */* func );
(until C++11)
int atexit( /* c-atexit-handler */* func ) noexcept;
int atexit( /* atexit-handler */* func ) noexcept;
(since C++11)
extern "C" using /* c-atexit-handler */ = void();
extern "C++" using /* atexit-handler */ = void();
(exposition only*)
# Parameters
func: pointer to a function to be called on normal program termination
# Return value
0 if the registration succeeds, nonzero value otherwise.
# Notes
The two overloads are distinct because the types of the parameter func are distinct (language linkage is part of its type).
# Example
#include <cstdlib>
#include <iostream>
void atexit_handler_1()
{
std::cout << "At exit #1\n";
}
void atexit_handler_2()
{
std::cout << "At exit #2\n";
}
int main()
{
const int result_1 = std::atexit(atexit_handler_1);
const int result_2 = std::atexit(atexit_handler_2);
if (result_1 || result_2)
{
std::cerr << "Registration failed!\n";
return EXIT_FAILURE;
}
std::cout << "Returning from main...\n";
return EXIT_SUCCESS;
}