I was told to use the singleton pattern instead of having global objects, which have the “static” storage duration, to avoid the static initialization order fiasco. But now that I have C++20 modules, should I?
Static initialization order fiasco
The static initialization order fiasco refers to the ambiguity in the order that objects with static storage duration in different translation units are initialized in. If an object in one translation unit relies on an object in another translation unit already being initialized, a crash can occur if the compiler decides to initialize them in the wrong order. For example, the order in which
Код: Выделить всё
.cpp
Within a single translation unit, the fiasco does not apply because the objects are initialized from top to bottom.
Storage duration “static” storage duration.
The storage for the object is allocated when the program begins and deallocated when the program ends. Only one instance of the object exists. All objects declared at namespace scope (including global namespace) have this storage duration, plus those declared with
Код: Выделить всё
static
Код: Выделить всё
extern
As far as I know…
The “static initialization order fiasco” means that which objects with the “static” storage duration, such as global objects, in non-module
Код: Выделить всё
.cpp
Then, with C++20 modules…
The dependencies between C++ module files (“translation units”) are seemingly clear with the
Код: Выделить всё
import
Код: Выделить всё
import
Источник: https://stackoverflow.com/questions/781 ... 20-modules