简体   繁体   中英

class template state data member, not an entity that can be explicitly specialized

I got an error in the code below:

template<typename T, bool B = is_fundamental<T>::value>
class class_name;

template<>
class class_name<string, false>{
public:
    static string const value;
};

template<>
string const class_name<string, false>::value = "Str";
// error: not an entity that can be explicitly specialized.(in VC++)

How can I fix it?

You are mixing two different approaches here. The first is the one suggested by @KerrekSB

template<typename T, bool B = is_fundamental<T>::value>
class class_name;

// NOTE: template<> is needed here because this is an explicit specialization of a class template
template<>
class class_name<string, false>{
public:
    static string const value;
};

// NOTE: no template<> here, because this is just a definition of an ordinary class member 
// (i.e. of the class class_name<string, false>)
string const class_name<string, false>::value = "Str";

Alternatively, you could full write out the general class template and explicitly specialize the static member for <string, false>

template<typename T, bool B = is_fundamental<T>::value>
class class_name {
public:
    static string const value;
};

// NOTE: template<> is needed here because this is an explicit specialization of a class template member
template<>
string const class_name<string, false>::value = "Str";

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