Richard
Richard

Reputation: 287

Initialize an static member in an fully specialized class template

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.

Upvotes: 6

Views: 1133

Answers (1)

Prasoon Saurav
Prasoon Saurav

Reputation: 92864

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;

Upvotes: 6

Related Questions