【问题标题】:Nested class declarations with multiple template arguments具有多个模板参数的嵌套类声明
【发布时间】:2014-03-31 00:06:57
【问题描述】:

为什么以下内容符合要求有点困惑:

template<typename T>
struct Foo {
    template<int i>
    struct Bar {
        typedef int BarSizeType;
    };
};

template<typename T, int i>
void do_something(typename Foo<T>::Bar<i>::BarSizeType arg) {
    // ...
}

但这不是:

template<typename T, typename T2>
struct Foo {
  template<int i>
  struct Bar {
    typedef int BarSizeType;
  };
};

template<typename T, int i>
void do_something(typename Foo<T, T>::Bar<i>::BarSizeType arg) {
  // ...
}

编译错误是:

错误 C2143:语法错误:在 ' 之前缺少 ')' 错误 C2143:语法错误:缺少“;”在'之前 错误 C2988:无法识别的模板声明/定义
错误 C2059:语法错误:' 错误 C2039:“BarSizeType”:不是“全局命名空间”的成员
错误 C2059:语法错误:')'
错误 C2143:语法错误:缺少“;”在'{'之前
错误 C2447:“{”:缺少函数头(旧式正式列表?)

有什么方法可以编译,而无需对代码进行重大更改?我正在使用 vs2012 编译器。

【问题讨论】:

    标签: c++ templates typename


    【解决方案1】:

    这是 MSVC 中的一个错误;两者都不正确。你需要写template:

    void do_something(typename Foo<T>::template Bar<i>::BarSizeType arg) {
                                       ^ here
    
    void do_something(typename Foo<T, T>::template Bar<i>::BarSizeType arg) {
                                          ^ and here
    

    欲了解更多信息,请参阅Where and why do I have to put the "template" and "typename" keywords?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-23
      • 2015-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多