I've recently become painfully aware of the Static Initialization Order Fiasco. I am wondering though if the rule that "initialization order is undefined across translation units" still holds for static members in a parent class which are needed by static members in a child class.
For example, say we have (excluding, for brevity, all the # guards and includes)
// a.h
class A {
static int count;
static int register_subclass();
};
// a.cpp
int A::count = 0;
int A::register_subclass() {
return count ++;
}
And then a sub-classes of A,
// b.h
class B : public A {
static int id;
};
// b.cpp
int B::id = A::register_subclass();
There are two translation units here with static objects in one depending on static objects in the other on initialization... it seems like it may be an instance of the static initialization order fiasco.
My question is: is it actually safe?
That is, am I guaranteed that there is no chance that B::id will contain junk copied from A::count before the latter is initialized? From my own tests, A always seems to get initialized first, but I'm not sure how to introduce noise in the initialization order to increase the probability of failing if the behavior is undefined.