简体   繁体   English

转发声明类模板的成员枚举

[英]Forward-declare a member enumeration of a class template

With C++11's strongly typed enum s, it is possible to declare a member enumeration of a class like so: 使用C ++ 11的强类型enum s,可以这样声明一个类的成员枚举:

class X {
public:
    enum class E;
};

enum class X::E { a, b };

However, when making X a class template: 但是,将X类模板时:

template <typename T>
class X {
public:
    enum class E;
};

template <typename T>
enum class X<T>::E { a, b };

gcc 4.7.2 and clang 3.0 both complain with "error: 'enum X::E' is an enumeration template [-pedantic]" and "error: enumeration cannot be a template", respectively. gcc 4.7.2和clang 3.0都分别抱怨“错误:'enum X :: E'是枚举模板[-pedantic]”和“错误:枚举不能是模板”。 The section of the standard I think is relevant (and which, in fact, this question originated from) is §14 Templates, the first paragraph of which states: 我认为标准的相关部分(实际上是这个问题的来源)是§14模板,其第一段指出:

The declaration in a template-declaration shall 模板 声明中的声明

  • declare or define a function or a class, or 声明或定义函数或类,或者
  • define a member function, a member class, a member enumeration , or a static data member of a class template or of a class nested within a class template, or 定义 类模板或嵌套在类模板中的类的成员函数,成员类, 成员枚举或静态数据成员,或
  • define a member template of a class or class template, or 定义类或类模板的成员模板,或
  • be an alias-declaration . 是一个别名声明

(emphasis mine). (强调我的)。 So is this a compiler bug, or am I mis-interpreting the statement entirely? 那么这是编译器错误,还是我完全误解了该语句?

I have been asked for creating this answer. 已要求我创建此答案。 See paragraph [temp.mem.enum] 14.5.1.4/1 of the C++ standard: 参见C ++标准的[temp.mem.enum] 14.5.1.4/1段落:

An enumeration member of a class template may be defined outside the class template definition. 可以在类模板定义之外定义类模板的枚举成员。 [ Example: [ 示例:

 template<class T> struct A { enum E : T; }; A<int> a; template<class T> enum A<T>::E : T { e1, e2 }; A<int>::E e = A<int>::e1; 

—end example ] —末例 ]

Newer version of clang (3.4) compiles your code successfully with flag -pedantic-errors whereas gcc 4.8.1 still considers it is an error . 较新版本的clang (3.4)使用flag -pedantic-errors 成功编译了代码 ,而gcc 4.8.1 仍认为这是一个错误 I think it is a gcc bug. 我认为这是gcc错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM