I would like to know if I am in an atexit() handler. I'm looking for the moral equivalent of std::uncaught_exceptions() for exit() calls.
I want to do this so I can have the destructors in my static objects behave one way when my main() function returns, and another way when exit() is called.
I could add a call to atexit() that sets a bool I inspect, but that runs into ordering issues. For example, this:
#include <iostream>
#include <stdlib.h>
struct Setter {
static void set_in_at_exit() { in_at_exit = true; std::cout << "setting\n";}
Setter() { std::cout << "constructor\n"; atexit(set_in_at_exit); }
static bool in_at_exit;
};
bool Setter::in_at_exit = false;
struct A {
A() { static Setter s; }
~A() { if (Setter::in_at_exit) {
std::cout << "in at exit\n";
}
std::cout << "My destructor\n"; }
};
A a;
int main()
{
return 0;
}
produces:
constructor
My destructor
setting
Which is not what I want. The destructor doesn't know it is in an exit handler.