Reputation: 2207
Static initialization is well described subject, here and even at this site here
And everywhere it is written that problem will occurs if there exists different compilation units with related static variables. And if static variables exists in one compilation unit, there shouldn't be problem: they will be initialized in order of their position in file.
But I have this code:
template <typename T>
class A{
public:
int _data;
T _obj;
A(int data) :_data(data){}
};
template <typename T>
class B{
public:
const static B<T> nullObj;
B(int data) :_a(new A<T>(data)){}
A<T> *_a;
};
template <typename T>
class C{
public:
const static C<T> nullObj;
C() :_a(nullObj._a){}
C(bool t) :_a(B<T>::nullObj._a){
_a->_data++; //FAILS HERE!
}
A<T> *_a;
};
template <typename T>
const B<T> B<T>::nullObj(0);
template <typename T>
const C<T> C<T>::nullObj(false);
class _B{};
class _A{ public: _A(){}; C<_B> g; };
int main(){
return 0;
}
And it fails at runtime before entering main() function, because it tries to initialize const C<T> C<T>::nullObj(false);
before const B<T> B<T>::nullObj(0);
is initialized. Despite their position in one file.
Why does it tries to initialize second static variable before first static variable? Does there exist chapter in C++ Standart where described real situation with sequence of static initialization?
Upvotes: 5
Views: 1775
Reputation: 43662
The point is in _A's constructor. As cppreference states:
The default constructor for class T is trivial (i.e. performs no action) if all of the following is true:
This is not trivially constructible
class _A{ public: _A(){} };
while this is
class _A{ public: };
and in the case of the example this definitely hasn't a trivial constructor:
class _A{ public: _A(){} C<_B> g; };
that means the constructor does perform initialization routines for the members of the class. As the standard says 14.7.1 [temp.inst]
the initialization (and any associated side-effects) of a static data member does not occur unless the static data member is itself used in a way that requires the definition of the static data member to exist
and since the non-trivial constructor just provided us the use (not the need) for static variables to be initialized, the CRT begins its work and starts initializing
const static C<T> nullObj;
which in turn dereferences something unitialized and you have undefined behavior.
To solve, as quantdev as stated, either you initialize B<_B>::nullObj
before this process or change the design of your code.
Upvotes: 1
Reputation: 23813
C++ Standard, section 14.7.1 [temp.inst]:
Unless a member of a class template or a member template has been explicitly instantiated or explicitly specialized, the specialization of the member is implicitly instantiated when the specialization is referenced in a context that requires the member definition to exist; in particular, the initialization (and any associated side-effects) of a static data member does not occur unless the static data member is itself used in a way that requires the definition of the static data member to exist
C<T>
is instanciated before B<T>
, so the order of initialization is well defined, and is :
1) const static C<T> nullObj;
2) const static B<T> nullObj;
Since C
constructor dereference B<T>::nullObj._a
, you have undefined behavior.
Solution:
You must use the static member B<_B>::nullObj
somehow for it to be initialized, e.g. if you do :
class _B{};
class _A{ public: _A() : b(B<_B>::nullObj) {}; B<_B> b; C<_B> g; };
Then B<_B>::nullObj
is indeed initialized before C
constructor needs it
Upvotes: 5
Reputation: 238411
Template instances are not defined until they are instantiated. The first time C<>
is instantiated is in _A
's constructor. And B<>
is instantiated by C<>
, so B<_B>
is defined when C<_B>
is instantiated. Therefore C<_B>
is before B<_B>
.
You can add explicit template instantiation template class B<_B>;
before the definition of _A
, which I believe should fix the order.
Upvotes: 2