简体   繁体   中英

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

I was playing around with class templates and statics and saw this:

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

compiler error: play.cpp:84: error: too few template-parameter-lists

I thought when I said rat<3>::ki was instantiating that template and defining the static for that particular template and thus the use of rat<3> would be fine from there on..how come this isn't working?

should be

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

but better use false for bool then 0 since it's more readable

Also in case you want to make you variable initialized for all templates as true for example:

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

And you can still specialize template for I = 3 :

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

You forgot the template:

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

And of course MSVS accepts your syntax (but not if I turn off language extensions).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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