I have a program split up into two source files:
example.cpp
#include <iostream>
class A {
public:
A(int x) {
::std::cout << "In A(" << x << ")\n";
}
};
static A first(1);
static A second(2);
example__main.cpp
int main(int argc, const char *argv[])
{
return 0;
}
Is the output of this program guaranteed to be:
In A(1)
In A(2)
on all platforms and compilers? If so, where in the standard does it say this? Does it matter if I'm using namespaces and first
and second
appear in different namespaces? How about if they aren't static and I'm using an anonymous namespace?
Yes, the order of initialization is defined for non-local static objects if the declarations appear in the same translation unit.
From C++03,