член данных состояния шаблона класса, а не объект, который может быть явно специализирован

Я получил ошибку в коде ниже:

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++)

Как я могу это исправить?

7

Решение

Вы смешиваете два разных подхода здесь. Первый вариант, предложенный @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";

В качестве альтернативы, вы можете полностью написать общий шаблон класса и явно специализировать статический член для <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

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector