4

Cppreference claims that, among other things, you can specialize a

  1. member enumeration of a class template

Since no examples were provided, I attempted to guess how to do that.

I ended up with following:

template <typename T> struct A
{
    enum E : int;
};

template <> enum A<int>::E : int {a,b,c};

Clang (8.0.0 with -std=c++17 -pedantic-errors) compiles it.

GCC (9.1 with -std=c++17 -pedantic-errors) rejects the code with

error: template specialization of 'enum A<int>::E' not allowed by ISO C++ [-Wpedantic]

MSVC (v19.20 with /std:c++latest also rejects the code with

error C3113: an 'enum' cannot be a template

Try it on gcc.godbolt.org

Did I specialize the enum correctly? If not, now do I do that?

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
  • This Q/A might help provide some insight: https://stackoverflow.com/questions/10552255/specializing-member-template-for-enum-type-arguments – Francis Cugler May 24 '19 at 23:27
  • GCC fails also for the related case where specialization is performed on the class template rather than a member or it, but MSVC manages that variation: https://gcc.godbolt.org/z/9p1lmn – dfrib May 25 '19 at 00:25

1 Answers1

3

There are examples in the standard([temp.expl.spec]/6) that suggest what you have is correct. The one there is:

template<> enum A<int>::E : int { eint };           // OK

Seems like a gcc bug.

Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
  • 1
    There is a bug report: [bug 61491](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61491) – Oliv May 25 '19 at 06:49
  • For reference: [How to disable GCC warnings for a few lines of code](https://stackoverflow.com/questions/3378560/how-to-disable-gcc-warnings-for-a-few-lines-of-code) (also applies for C++) – user202729 Sep 29 '19 at 03:46