gpt4 book ai didi

c++ - 类模板状态数据成员,而不是可以显式特化的实体

转载 作者:可可西里 更新时间:2023-11-01 16:38:52 26 4
gpt4 key购买 nike

我在下面的代码中遇到错误:

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

我该如何解决?

最佳答案

您在这里混合了两种不同的方法。第一个是@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";

关于c++ - 类模板状态数据成员,而不是可以显式特化的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14178440/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com