开发者

Initialize an static member in an fully specialized class template

开发者 https://www.devze.com 2023-02-16 18:06 出处:网络
I can\'t seem to init an static member inside an fully specialized class template! I\'m trying to do the following:

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 开发者_JAVA百科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.


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消