加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 在模板化类中定义模板化类的静态数据成员

发布时间:2020-12-16 06:54:10 所属栏目:百科 来源:网络整理
导读:我在另一个类模板中有一个类模板.内部类有一个静态数据成员.我正在努力为它提供一个定义.以下示例适用于clang 3.8但不适用于 gcc-7.1 template typename Tstruct Out { template typename U struct In { static int var; };};template typename Ttemplate ty
我在另一个类模板中有一个类模板.内部类有一个静态数据成员.我正在努力为它提供一个定义.以下示例适用于clang 3.8但不适用于 gcc-7.1

template <typename T>
struct Out {
  template <typename U>
  struct In {
    static int var;
  };
};

template <typename T>
template <typename U>
int Out<T>::template In<U>::var;

gcc给出错误:

error: template definition of non-template ‘int Out<T>::In<U>::var’
 int Out<T>::template In<U>::var;
                             ^~~

我要做些什么才能让gcc开心?

编辑:结果摆脱模板使这工作:

template <typename T>
template <typename U>
int Out<T>::In<U>::var;

哪个仍然存在问题,这里允许使用模板吗?

解决方法

在In之前没有模板的情况下更常见这种类型的定义.此处不需要template关键字,因为Out< T> :: In是“当前特化的成员”.

有关在成员名称之前指定何时需要template关键字的规则,请参阅[temp.names] / 4.有关技术术语“当前专业化成员”的定义,请参见[temp.dep.type] / 4.

但关键字实际上是允许的,因为语法允许它在任何::和名称之间,语义只要求后面的名称与模板参数一起使用或命名类模板([temp.names] / 5 ),并且标准中没有其他规则禁止它.正如[temp.names] / 5中的注释所解释的那样:

[ Note: As is the case with the typename prefix,the template prefix is allowed in cases where it is not strictly necessary; i.e.,when the nested-name-specifier or the expression on the left of the -> or . is not dependent on a template parameter,or the use does not appear in the scope of a template. – end note]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读