C++静态常量模板成员初始化

C++ static const template member initialization

本文关键字:成员 初始化 静态 常量 C++      更新时间:2023-10-16

我在初始化模板类的静态常量成员变量时遇到了一些奇怪的问题。我的所有其他静态变量初始化都很好,但出于某种原因,它不喜欢这个。我把一些样本代码放在一起测试,它没有问题,所以我真的不知道发生了什么。

除此之外,我在定义使用模板类内部声明的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;
}