初始化模板专门化的静态const
Initializing a static const for a template specialization
我创建了一个带有静态const元素的类模板。这样做的目的是让模板的每个专门化都拥有这个静态元素的自己的版本/值。下面是我的示例代码:
template < typename T > class C
{
public:
static const std::string NAME;
T something;
void printName() { std::cout << NAME << std::endl; }
};
class C1 : public C< int >{};
class C2 : public C< char >{};
template<> const std::string C< int >::NAME{ "my int" }; // compiles
template<> const std::string C< char >::NAME{ "my char" }; // compiles
//template<> const std::string C1::NAME{ "my int" }; // doesn't compile
//template<> const std::string C2::NAME{ "my char" }; // doesn't compile
//const std::string C1::NAME{ "my int" }; // doesn't compile
//const std::string C2::NAME{ "my char" }; // doesn't compile
int main()
{
C1 c1;
C2 c2;
c1.printName();
c2.printName();
std::cout << c1.NAME << " " << c2.NAME << std::endl;
std::cout << C1::NAME << " " << C2::NAME << std::endl;
}
当使用可编译版本时,输出是我期望的:
my int
my char
my int my char
my int my char
对于不能编译的行,错误信息(使用gcc 4.4)显示
ISO C++ does not permit 'C<int>::NAME' to be defined as 'C1::NAME'
为什么不允许?我的理解是,模板的完整专门化在所有方面都是一个类,这个类具有模板中声明的所有成员。因此,我希望能够通过使用作用域解析操作符来引用该类的静态成员。显然,我可以在初始化之后这样做,就像上面main的最后一行一样,但不能在初始化本身中这样做。
谁能解释一下为什么标准是这样写的?如果标准允许上面的"坏"语法,会出现什么样的问题?
在我看来,你遇到的问题与模板无关。问题是,您试图使用嵌套名称说明符中的派生类(而不是原始类)定义静态成员。你在这里也会遇到同样的问题:
struct A {
static int x;
};
struct B : A{};
int B::x = 42;
代码中的C1
和C2
不是C
的完全专门化,而是C
专门化的派生类。所以我不知道你在期待什么
相关文章:
- 初始化类中的静态 const 数组 - C++
- c++ 是否保证标头初始化的静态 const 成员跨编译单元和库共享单个实例?
- 匿名命名空间中的const与静态const
- 内联初始化的静态 const 类成员的初始化顺序保证
- 是否可以按模板类型更改静态 const 类成员的值
- 如何在类中声明静态 const 结构元素
- 与其他静态const成员初始化静态常量成员
- 如何在预处理器 (#if) 中强制转换静态 const 以避免溢出
- 使用模板参数初始化静态 const 成员
- 为什么将FMTFLAG指定两次 - 作为枚举的一部分,而另一个实例为静态const变量
- 将整数值分配为variadic模板列表到静态const std ::数组成员
- 静态const数据成员在另一个文件中定义
- 初始化在 C++ 中自定义类的构造函数中没有参数的静态 const 类成员
- 将静态 const 成员重新声明为 constexpr 是否会自动使其符合内联条件
- constexpr vs.静态const:哪个更喜欢
- 静态const颜色无法与Allegro5完全合作
- 模板类中的不可复制静态 const 成员类
- 编组静态const char*从C 到C#
- 枚举和静态const成员变量在模板特征类中的用法
- 在性能和内存使用方面,“静态const”,“ #define”和“枚举”之间的差异