Topic: initiation order of template static data members


Author: weidai@eskimo.com (Wei Dai)
Date: 1996/12/09
Raw View
What does the current draft say about the initialization order of static
data members of classes generated from templates?  In the following
program for example, what in what order will A's constructor be called?

#include <iostream.h>

class A
{
public:
 A(int i);
 int m_i;
};

A::A(int i) : m_i(i)
{
 cout << "A::A called with argument " << i << endl;
}

template <class T> class B
{
public:
 B(T t) : m_t(t) {}
 T m_t;
 static A a1;
 static A a2;
};

// **
template <class T> A B<T>::a2 = 2;
template <class T> A B<T>::a1 = 1;
template B<double>;
// **

int main()
{
 return 0;
}

Compiled with MSVC 4.2, this program prints out:

A::A called with argument 1
A::A called with argument 2

Is this a correct behavior?  How about if the section above marked with
"// **" is replaced by:

template <class T> A B<T>::a1 = 1;
template B<double>;
A B<double>::a2 = 2;

Thanks in advance.

Wei Dai


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]