I read this and I can't think of a scenario for which X::_x
will be used before initialized in file X.cpp
(assuming the compiler treats X.h
and X.cpp
one after the other)
Can someone explain to me how such scenario could happen?
I read this and I can't think of a scenario for which X::_x
will be used before initialized in file X.cpp
(assuming the compiler treats X.h
and X.cpp
one after the other)
Can someone explain to me how such scenario could happen?
Static initialization order fiasco happens when you have multiple translation units* that use static initialization, and one of the initialization routines require data produced by another one.
If all you have is a single header and a single translation unit, static initialization order fiasco does not apply, because initialization order within the same unit is well defined (it follows the order of declaration).
* that's a fancy name for a cpp file.
I think it cannot happen with just the code on the page, however there may be more code, behaving as described on the page. In one of these files, one could have a static object of type X:
// File X3.cpp
#include "X.h"
X x__;
Now, if:
the default constructor of the class X calls someMethod
// File X2.cpp
X::X() {
someMethod();
}
and
then someMethod() will be called before x_ has been initialized.