Whats the “static initialization order fiasco”?
[Recently created (on 9/97). Click here to go to the next FAQ in the “chain” of recent changes.] A subtle way to kill your project. The static initialization order fiasco is a very subtle and commonly misunderstood aspect of C++. Unfortunately it’s very hard to detect — the errors occur before main() begins. In short, suppose you have two static objects x and y which exist in separate source files, say x.cpp and y.cpp. Suppose further that the constructor for the y object calls some method on the x object. That’s it. It’s that simple. The tragedy is that you have a 50%-50% chance of dying. If the compilation unit for x.cpp happens to get initialized first, all is well. But if the compilation unit for y.cpp get initialized first, then y’s constructor will get run before x’s constructor, and you’re toast. I.e., y’s constructor will call a method on the x object, yet the x object hasn’t yet been constructed. I hear they’re hiring down at McDonalds. Enjoy your new job flipping burgers. If