繁体   English   中英

关于类模板中静态的编译器错误是什么意思?

[英]What does this compiler error regarding statics in a class template mean?

我正在玩类模板和静态,看到了这个:

template<int I>
struct rat
{
    static bool k;
};

bool rat<3>::k = 0; //this is line 84 of the only source file play.cpp

int main(int argc, char **argv)
{

    rat<3> r;
}

编译器错误:play.cpp:84:错误:模板参数列表太少

我想当我说rat <3> :: ki正在实例化那个模板并为那个特定的模板定义静态时,因此使用rat<3>就可以了。从那里开始,这是不行的?

应该

template<>
bool rat<3>::k = 0;

但最好使用falsebool然后0因为它更具可读性

如果你想让所有模板的变量初始化为true ,例如:

template<int I>
bool rat<I>::k = true;

你仍然可以专门为I = 3模板:

template<>
bool rat<3>::k = false;

你忘记了模板:

template<>
bool rat<3>::k = 0;

当然, MSVS接受您的语法(但如果我关闭语言扩展,则不会)。

暂无
暂无

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

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