6

I can't seem to init an static member inside an fully specialized class template!

I'm trying to do the following:

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

But i can't seem to init the static member, i tried everything like:

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

It doesn't compile, so any pointers on how to actually do this would be nice ;)

Edit: the answer beneath is correct but you also need to place the init in the .cpp file and not in the header file.

Thanks for your time, Richard.

1 Answer 1

6

Don't use template<> while defining Value because template<> is not allowed in member definition of explicitly specialized class[X<int> in this case]. Moreover you are missing a semicolon after }

This works for me:

template<typename Type> class X { }; template<> class X<int> { public: static int Value; }; int X<int>::Value = 0; 
Sign up to request clarification or add additional context in comments.

2 Comments

"template<> is not allowed in member definition of explicitly specialized class" -- Good to know, thanks!
Thanks, by removing the template<> and by placing the init in the .cpp file (which i figured out by your example). I can now compile it. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.