【问题标题】:C++ static const template member initializationC++ 静态 const 模板成员初始化
【发布时间】:2012-02-02 01:13:25
【问题描述】:

我在尝试初始化模板类的静态 const 成员变量时遇到了一些奇怪的问题。我所有的其他静态变量都初始化得很好,但由于某种原因它不喜欢这个。我整理了一些示例代码进行测试,它没有问题所以我真的不知道发生了什么。

除此之外,我在定义使用在模板类中声明的 typedef 的函数时也遇到了问题,同样的问题是找不到类型。这个问题我已经能够在下面的代码中重现。我知道修复它的一种方法是在类内部定义函数,但函数非常大,我试图使其与在类外部定义所有巨大函数保持一致,以使类定义更容易阅读。如果这是我唯一的选择,那么我想我将不得不破例......

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;
}

【问题讨论】:

    标签: c++ templates static initialization constants


    【解决方案1】:

    感谢一位同事,我找到了解决这两个问题的方法。

    对于第一个问题,静态成员变量,我已将定义移至 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;
    };
    

    【讨论】:

      【解决方案2】:

      我对您的代码进行了 2 处更改,并且编译正常。

      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;
      }
      

      【讨论】:

      • 这是一个“发现差异”的谜题吗?你做了什么改变,为什么?
      • 我不认为这是一个差异拼图。我改变了 mTestFunction() 的定义方式。
      • 所以你只是把 typedef 移到了课堂之外?你可以这么说。我想这也是另一种解决方案。有什么要考虑的,谢谢。我还有另一个问题,就像我说的,示例代码没有问题。我真的只是想看看是否有人知道可能导致我在 cmets 中指定的关于静态成员声明的错误。
      猜你喜欢
      • 2011-03-19
      • 1970-01-01
      • 1970-01-01
      • 2013-04-13
      • 1970-01-01
      • 2021-10-21
      • 2010-10-16
      • 1970-01-01
      相关资源
      最近更新 更多