Order of initialization of globals across TU
suggest changeInside one translation unit, namespace-scope variables with dynamic initialization are initialized in the order of their definitions. Across translation units, that order is unspecified.
A program with the following files
- foo.cpp
#include <iostream>
int dummyFoo = ((std::cout << "foo"), 0);
- bar.cpp
#include <iostream>
int dummyBar = ((std::cout << "bar"), 0);
- main.cpp
int main() {}
might produce as output:
foobar
or
barfoo
Using a namespace-scope object that is dynamically initialized in another translation unit is the static initialization order fiasco: that object may not have been initialized yet.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents