Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize an static member in an fully specialized class template

Tags:

c++

templates

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.

like image 450
Richard Avatar asked Mar 13 '11 12:03

Richard


1 Answers

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;
like image 185
Prasoon Saurav Avatar answered Oct 22 '22 07:10

Prasoon Saurav