简体   繁体   English

在完全专用的类模板中初始化静态成员

[英]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. 编辑:下面的答案是正确的,但您还需要将init放在.cpp文件中,而不是放在头文件中。

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]. 不使用template<>同时限定Value ,因为template<>中未明确地专门类的成员定义允许[ X<int>在这种情况下]。 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM