For example:
#include<iostream>
using namespace std;
class A
{
public:
A(){cout<<k<<endl;}//make some output
static int k;
};
A a;//before `k`'s definition
int A::k=666;
int main()
{
}
Is the answer guaranteed to be 666
(I've tested it in gcc8.1.0 the answer is 666
) or causing undefined behavior?
What's more, in this example, object a
and definition A::k
are in the same translation unit, what will happen if they are in different units, since
Initialization of static variables in different translation units is indeterminately sequenced
From my point of view, since in the same TU the initializaition order is fixed, the answer of the example above should be undfined.