Is it true that in the following code, which I took from another question, in accordance with the standard, static member variable w
should be initialized dynamically?
// MyClass.h:
class MyClass
{
public:
static int z;
};
// MyClass.cpp:
int MyClass::z = 4;
// MyOtherClass.h:
class MyOtherClass
{
public:
static int w;
};
// MyOtherClass.cpp:
int MyOtherClass::w = MyClass::z;
I disagree with the accepted answer, because according to this standard's wording:
An implementation is permitted to perform the initialization of a non-local variable with static storage duration as a static initialization even if such initialization is not required to be done statically, provided that ...
— the static version of the initialization produces the same value in the initialized variable as would be produced by the dynamic initialization if all variables not required to be initialized statically were initialized dynamically.
But how can the compiler possibly know what value will be produced by initialization of z
if its definition is unreachable from MyOtherClass.cpp
? If it can't know, then the condition can't be satisfied, then the only standard way to initialize w
is dynamic. Is it right and the answer to the other question is wrong?