【问题标题】:Declare static object of a nested class template声明嵌套类模板的静态对象
【发布时间】:2013-12-03 21:17:10
【问题描述】:

我正在尝试在第一个类模板中声明嵌套类模板的静态对象,如下所示:

template <typename... a_t>
class A {
private:
    template <typename... b_t>
    class B {

    };

    static B<a_t...> b;
};

这是我期望的代码,但会导致编译错误,尽管 this answer here 适用于第二类模板中的非模板化成员:

template <typename... a_t>
template <typename... b_t>
A<a_t...>::B<b_t...> A<a_t...>::b; //incorrect?

实现此目的的正确语法是什么?

【问题讨论】:

  • 第一个示例为 GCC 4.8 编译,但不是第二个代码块,因为您犯了一个错误:您只需要 one 模板声明和 ::B&lt;b_t...&gt; 应该是::B&lt;a_t....&gt;.
  • @0x499602D2 正在回答它:模板 A::B A::b;
  • @0x499602D2 这是我这样做时得到的:“警告 C4346: 'A::?$B@$$W$Ra_t@BAAB@' : 依赖名称不是类型,”“错误 C2143:语法错误:缺少 ';'在'A::b'"之前
  • @NmdMystery Visual Studio 不完全支持可变参数模板(或 C++11)。尝试为每个类只使用一个模板参数编译相同的代码,看看它是否有效。 (即template&lt;class T&gt; struct A {...};
  • @0x499602D2 这会导致同样的错误,尽管你对 Visual Studio 的可变参数有问题是正确的:/

标签: c++ templates


【解决方案1】:

您忘记了 typename 关键字:

template <typename... a_t>
typename A<a_t...>::B<a_t...> A<a_t...>::b;

【讨论】:

  • 当我回到 VC++ 时,我会确保它工作正常(以防我似乎放弃了这个问题)。
猜你喜欢
  • 2019-03-23
  • 2015-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-23
相关资源
最近更新 更多