Regarding the order of destruction of static variables in C++, are there any guarantees about the lifetime of static objects with respect to their static member variables?
For example, if I had something like this (insanely simplified example for demonstration purposes only):
class Object {
static std::vector< Object * > all_objects;
public
Object() {
all_objects.push_back( this );
}
~Object() {
all_objects.erase(
std::remove(all_objects.begin(), all_objects.end(), this),
all_objects.end());
}
};
Would this be "safe" with respect to static Objects in different compilation units? That is, is there any guarantee that the all_objects
member variable will stick around at least as long as any valid Object, or could there be an issue where all_objects
is destroyed prior to the last Object instance?
And does the answer change if the code is being used as a library (say within Python) rather than as a standalone program with its own main()?