Initialize a static member in a fully specialized class template

I cannot run a static member inside a fully specialized class template!

I am trying to do the following:

template<typename Type> class X { }; template<> class X<int> { public: static int Value; } 

But I can not imagine a static member, I tried everything like:

 template<> int X<int>::Value = 0; 

It does not compile, so any pointers on how to actually do this will be nice;)

Edit: the answer below is correct, but you also need to put init in the .cpp file and not in the header file.

Thanks for your time, Richard.

+6
c ++ templates
source share
1 answer

Do not use template<> when defining Value , because template<> not allowed in the definition of a member of an explicitly specialized class [ X<int> in this case]. Also after }

missing a semicolon,

This one works for me :

 template<typename Type> class X { }; template<> class X<int> { public: static int Value; }; int X<int>::Value = 0; 
+6
source share

All Articles