411 REFERENCE · Wikibooks
More C++ Idioms/Runtime Static Initialization Order Idioms
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Runtime Static Initialization Order Idioms
Intent
Control the order of initialization and destruction of non-local static objects across compilation units that are otherwise ordered in an implementation dependent manner.
Motivation
Order of initialization of static objects spread across different compilation units is not well defined. Order of destruction is the reverse of initialization order but initialization order itself is implementation defined. Bring order to this chaos.
The destructor of static objects are non-trivial and have important side-effects that have to happen
Solution and Sample Code
The following idioms are commonly used to control the order of initialization of static objects.
Construct On First Use
Nifty Counter Idiom (a.k.a. Schwarz Counter Idiom)
ja:More C++ Idioms/実行時の静的初期化順序に関するイディオム(Runtime Static Initialization Order Idioms)