C++静态常量模板成员初始化
C++ static const template member initialization
我在初始化模板类的静态常量成员变量时遇到了一些奇怪的问题。我的所有其他静态变量初始化都很好,但出于某种原因,它不喜欢这个。我把一些样本代码放在一起测试,它没有问题,所以我真的不知道发生了什么。
除此之外,我在定义使用模板类内部声明的typedefs的函数时也遇到了问题,同样的问题是找不到类型。这个问题我已经能够在下面的代码中重现了。我知道解决这个问题的一种方法是在类内部定义函数,但这个函数确实很大,我正努力使它与在类外部定义所有巨大函数保持一致,以使类定义更容易阅读。如果这是我唯一的选择,那么我想我必须破例。。。
class tTestType
{
public:
tTestType(int32_t val) : fValue(val) { }
private:
int32_t fValue;
};
template<class T>
class tTestTemplate
{
public:
tTestTemplate() { }
private:
typedef std::vector<int32_t> tSomeVec;
tSomeVec mTestFunction() const;
static const tTestType kTestStatic;
};
// Should cause the following errors but I can't reproduce them for some reason:
// error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
// error C2988: unrecognizable template declaration/definition
// error C2059: syntax error : 'constant'
template<class T>
const tTestType tTestTemplate<T>::kTestStatic(10);
// Causes the following errors:
// error C2143: syntax error : missing ';' before 'tTestTemplate<T>::mTestFunction'
// error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
// fatal error C1903: unable to recover from previous error(s); stopping compilation
template<class T>
tTestTemplate<T>::tSomeVec tTestTemplate<T>::mTestFunction() const
{
tSomeVec result;
result.push_back(0);
return result;
}
多亏了一位同事,我找到了解决这两个问题的方法。
对于第一个问题,即静态成员变量,我已经将定义移动到CPP文件中,并使用了模板专用化。在我发布的测试代码中,这并没有中断,因为基本类型(int、float等)可以处理这个问题,但如果你使用更复杂的类型,比如类,那么它应该会导致错误。我知道,这个解决方案不是世界上最好的办法,但它是唯一一个有点干净的办法。如果有人有更好的解决方案,请告诉我:
template<>
const tTestType tTestTemplate<uint32_t>::kTestStatic(10);
对于第二个问题,使用类中声明的类型的函数,我采用了我在最初的文章中描述的解决方案,只是将函数定义移动到了模板类中,所以现在看起来是这样的:
template<class T>
class tTestTemplate
{
public:
tTestTemplate() { }
private:
typedef std::vector<int32_t> tSomeVec;
// Declaring the function inside the class to fix the compiler error.
tSomeVec mTestFunction() const
{
tSomeVec result;
result.push_back(0);
return result;
}
static const tTestType kTestStatic;
};
我对您的代码进行了两次更改,并且编译得很好。
class tTestType
{
public:
tTestType(int32_t val) : fValue(val) { }
private:
int32_t fValue;
};
typedef std::vector<int32_t> tSomeVec;
template<class T>
class tTestTemplate
{
public:
tTestTemplate() { }
private:
tSomeVec mTestFunction() const;
static const tTestType kTestStatic;
};
// Should cause the following errors but I can't reproduce them for some reason:
// error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
// error C2988: unrecognizable template declaration/definition
// error C2059: syntax error : 'constant'
template<class T>
const tTestType tTestTemplate<T>::kTestStatic(10);
// Causes the following errors:
// error C2143: syntax error : missing ';' before 'tTestTemplate<T>::mTestFunction'
// error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
// fatal error C1903: unable to recover from previous error(s); stopping compilation
template<class T>
tSomeVec tTestTemplate<T>::mTestFunction() const
{
tSomeVec result;
result.push_back(0);
return result;
}
相关文章:
- C++成员初始化
- c++构造函数成员初始化:传递参数
- C++正确的指针成员初始化
- 将另一个类的对象传递到当前类C++的构造函数中(不是成员初始化)
- WinLamb 错误:成员初始化非法
- 使用其他成员初始化结构的成员?
- C++模板类静态成员初始化
- 解释了构造函数成员初始化列表
- 如何在成员初始化列表中声明共享指针
- C++入门5版:使用get成员初始化另一个与shared_ptr无关的对象
- C++11 默认类成员初始化与初始值设定项列表同时
- 调用非默认构造函数作为成员初始化
- C++模板成员初始化:用右值移动构造,但用左值移动引用
- 类成员初始化C++
- 在成员初始化列表中,我可以创建对列表中不在列表中的成员变量的引用
- C :(不重复)积分静态成员初始化(不仅是声明!),导致链接器错误,原因
- 如何调用成员初始化器列表中参考成员的构造函数
- C 构造函数采用成员初始化器
- 与其他静态const成员初始化静态常量成员
- 静态内联成员初始化顺序