Say I have a static const int class member variable. It is initialized directly in the class definition, but it doesn't have a definition in a .cpp (which is ok since it's not odr-used).
Further, say this constant is used in the constructor initializer list of another class, and a global instance is created of that other class.
// mytype1.hpp
class MyType1
{
public:
static const int g_myConstant = 42; // no definition in cpp
};
// mytype2.hpp
class MyType2
{
public:
MyType2();
private:
int m_myMember;
};
// mytype2.cpp
MyType2::MyType2()
: m_myMember(MyType1::g_myConstant)
{
}
// otherfile.cpp
// is the reference to MyType1::g_myConstant in the ctor well defined?
MyType2 myType2GlobalInstance;
Is the construction of myType2GlobalInstance well defined? In other words: What guarantees does C++ make about static initialization order of static const class member variables?
Since there is no definition of that constant, there is probably no memory for it that needs to be initialized, and the variable behaves more like a preprocessor macro.. but is this guaranteed? Does it make a difference if the constant is defined or not?
Does it change anything if the member variable is static constexpr?