HolyBlackCat
HolyBlackCat

Reputation: 96043

Can we specialize a enum (type) member of a class template?

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?

Upvotes: 4

Views: 384

Answers (1)

Ryan Haining
Ryan Haining

Reputation: 36792

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.

Upvotes: 3

Related Questions