Forward-declare class template member enumeration

With C ++ 11 with hard-typed enum s, you can declare an enumeration of class members as follows:

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

However, when creating an X 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 about "error:" enum X :: E is the enumeration template [-pedantic] and "error: enumeration cannot be a template, respectively. Section of the standard that I consider relevant (and in fact, this question arose from), is ยง14 of the Templates, the first paragraph of which reads:

The declaration in the template declaration must

  • declare or define a function or class or
  • define a member function, a member class, an enumeration of elements, or a static data element of a class template or class nested in a class template, or
  • define a template element for a class or class template or
  • it is an ad alias.

(my emphasis). So is this a compiler error, or am I misinterpreting the expression?

+8
c ++ enums c ++ 11 templates forward-declaration
source share
1 answer

I was asked to create this answer. See paragraph [temp.mem.enum] 14.5.1.4/1 of the C ++ Standard:

A class template enumeration member can 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]

The newer version of clang (3.4) will successfully compile your code with the -pedantic-errors flag, while gcc 4.8.1 still considers this to be a bug . I think this is a gcc error.

+3
source share

All Articles