Prevent static initialization order “fiasco”, C++
The modern, more pattern-oriented way is not to use globals in the first place. There’s no other way around it. It wouldn’t be much of a “fiasco”, otherwise!
The modern, more pattern-oriented way is not to use globals in the first place. There’s no other way around it. It wouldn’t be much of a “fiasco”, otherwise!
The initialization steps are given in 3.6.2 “Initialization of non-local objects” of the C++ standard: Step 1: x and y are zero-initialized before any other initialization takes place. Step 2: x or y is dynamically initialized – which one is unspecified by the standard. That variable will get the value 1 since the other variable … Read more
You have answered your own question. Static initialization order is undefined, and the most elegant way around it (while still doing static initialization i.e. not refactoring it away completely) is to wrap the initialization in a function. Read the C++ FAQ items starting from https://isocpp.org/wiki/faq/ctors#static-init-order
Solving order of initialization: First off, this is just a temporary work-around because you have global variables that you are trying to get rid of but just have not had time yet (you are going to get rid of them eventually aren’t you? 🙂 class A { public: // Get the global instance abc static … Read more