A member of the state data of a class template, not an object 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 this?

+7
source share
1 answer

Here you mix two different approaches. The first is the proposal 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 // (ie of the class class_name<string, false>) string const class_name<string, false>::value = "Str"; 

Alternatively, you can completely write a generic class template and explicitly set a 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"; 
+5
source

All Articles