6

Is this the correct way to initialize static data members of template classes?

template <typename T>
class Temp
{
public:
    static unsigned int x;
};

template <typename T>
unsigned int Temp<T>::x = 0;
NFRCR
  • 5,304
  • 6
  • 32
  • 37

1 Answers1

7

Yes. Yes, it is.

[C++11: 14.5.1.3/1] A definition for a static data member may be provided in a namespace scope enclosing the definition of the static member’s class template. [ Example:

template<class T> class X {
   static T s;
};

template<class T> T X<T>::s = 0;

—end example ]

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055