4 ms·
the article didn't mention the “static initialization order ‘fiasco’" https://isocpp.org/wiki/faq/ctors#static-init-order https://isocpp.org/wiki/faq/ctors#sta
by MichaelMoser123 3y ago
the article didn't mention the “static initialization order ‘fiasco’"
https://isocpp.org/wiki/faq/ctors#static-init-order https://isocpp.org/wiki/faq/ctors#static-init-order
If you have two static objects in two different files then good luck if one depends on the other, as the initialization order is not well defined.
If both files are in the same executable then you can get around that by linking the object files in the desired order - well mostly, kind of.
i have hit this problem at some point...
- josefx 3y agoThat sounds like hard mode, the easy solution is to just initialize them the first time they are used. GetInstance(){ if (!instance)instance = new Foo; return instance;}, add a lock if you need thread safety and done.
- MichaelMoser123 3y agoYes, you fix the problem by calling the initialization stuff from a single point.
- rcxdude 3y agoor just: Foo &getInstance() { static Foo instance; return instance; } Guaranteed to be threadsafe by the standard, even.