初始化使用模板参数作为类型的模板化类的静态成员?
Initialize a templated class's static member that uses a template argument as the type?
我有:
template<typename TypeA, typename TypeB>
class MyClass
{
public:
static TypeA StaticA;
static TypeB StaticB;
//...other stuff....
};
如何初始化"StaticA"(和StaticB)?
如果我这样做:(在头文件中,在类的声明下,但不在里面)
template<>
typename MyClass<TypeA, TypeB>::TypeA MyClass<TypeA, TypeB>::StaticA = TypeA();
给我:
'TypeA' was not declared in this scope.
'TypeB' was not declared in this scope.
template argument 1 is invalid
template argument 2 is invalid
这个:
template<typename TypeA, typename TypeB>
typename MyClass<TypeA, TypeB>::TypeA MyClass<TypeA, TypeB>::StaticA = TypeA();
给我:
conflicting declaration 'typename MyClass<TypeA, TypeB>::TypeA MyClass<TypeA, TypeB>::StaticA'
'MyClass<TypeA, TypeB>::StaticA' has a previous declaration as 'TypeA MyClass<TypeA, TypeB>::StaticA'
declaration of 'TypeA MyClass<TypeA, TypeB>::StaticA' outside of class is not definition [-fpermissive]
初始化使用模板参数作为类型的模板类的静态成员的正确方法是什么?
啊,正确的语法是:
template<typename TypeA, typename TypeB>
TypeA ResourceFactory<TypeA, TypeB>::StaticA = TypeA();
我最初的问题是:
template<typename TypeA, typename TypeB>
typename MyClass<TypeA, TypeB>:: TypeA MyClass<TypeA, TypeB>::StaticA = TypeA();
^remove^ ^-------remove--------^
由于所有的模板参数,我感到很困惑
与这个问题的答案相似,但并不完全重叠
相关文章:
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 静态成员函数使用相同的名称时出现模板类型名称错误
- 从相同类型的静态成员进行类内初始化
- 在类定义中推导内联静态成员函数的返回类型
- 如何声明和定义具有推导类型的静态成员
- (2 问题)"类"类型重新定义(即使 #pragma 一次),以及静态函数内的静态成员对象初始化?
- 初始化在类类型 #define 中定义的非静态成员数组,不带默认 ctor
- 初始化与类类型相同的静态成员(静态初始化顺序问题)
- 缺少类型说明符 - int 假定 声明静态成员后出错
- 定义非静态成员时获取C++中的"this"类型
- 创建类型列表并访问每种类型的静态成员?
- 如果静态成员未初始化并且成员类型是类本身,该怎么办?
- boost::bind 无法绑定到纯虚拟基类中定义的非静态函数模板成员类型
- 类型为专用数据成员的静态成员
- 提升侵入性unordered_set静态成员功能返回错误的尺寸类型
- 如何创建一个编译时静态类类型,该类型初始化具有特定值的成员容器
- 如果我创建一个修改值的迭代器,静态成员"reference"应该是什么类型?
- 静态成员函数中封闭类的C++类型
- 为什么constexpr静态成员(类型为class)需要定义
- 基于静态成员类型的模板函数专用化