首页 > 解决方案 > Can C++20 using enum apply to templates?

问题描述

According to cppreference, both gcc and msvc have completed the implementation of C++20 feature using enum, which means we can using-declaration with an enum:

struct A { 
  enum e { /* ... */ }; 
};

struct S { 
  using enum A::e;
};

But when I apply it to the templates:

template <class T>
struct S { 
  using enum T::e;
};

gcc rejects it with:

<source>:7:14: error: 'using enum' of dependent type 'typename T::e'
    7 |   using enum T::e;
      |              ^~~~
<source>:7:17: note: declared here
    7 |   using enum T::e;
      |                 ^

msvc also rejects it with:

<source>(7): error C2868: 'e': ill-formed using-declaration; expected a qualified-name
<source>(8): note: see reference to class template instantiation 'S<T>' being compiled

I have no idea why this cannot work since it seems to be no different from non-templates.

Is this a compiler bug or just ill-formed?

标签: c++templatesenumsc++20using-declaration

解决方案


From the proposal:

The elaborated-enum-specifier shall not name a dependent type and the type shall have a reachable enum-specifier.

In your example T::e is a dependent type.


推荐阅读