Вопрос

With C++11's strongly typed enums, it is possible to declare a member enumeration of a class like so:

class X {
public:
    enum class E;
};

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

However, when making X a class template:

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. 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:

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:

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. I think it is a gcc bug.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top