#include #include using namespace std; void Handler1(void) { cout << endl; cout << "A call to Handler 1" << endl; cout << endl; return; } void Handler2() { cout << endl; cout << "A call to Handler 2" << endl; cout << endl; return; } void Handler3(int code, void * ) { cout << endl; cout << "Handler 3 called status= " << code << endl; cout << endl; return; } void Handler4(int code, void * data) { cout << endl; cout << "Handler 4 called status= " << code << " data= " << reinterpret_cast(data) << endl; cout << endl; return; } void Handler5(int code, void * data) { cout << endl; cout << "Handler 5 called status= " << code << " data= " << static_cast (data) << " *data = " << *(static_cast(data)) << endl; cout << endl; return; } // I need this in the global space as the local variables // (on the stack, main's activation record) // have gone out of scope when the exit function is called. int value = 7; int main() { // just to demo we can call a handler multiple times. atexit(Handler1); atexit(Handler1); atexit(Handler2); atexit(Handler1); // some permutations on on_exit on_exit(Handler3, static_cast(nullptr)); on_exit(Handler4, reinterpret_cast(42)); on_exit(Handler5, static_cast (&value)); return 5; }