使用枚举的 C++20 可以应用于模板吗?
Can C++20 using enum apply to templates?
根据 cppreference, both gcc and msvc have completed the implementation of C++20 feature using enum
, which means we can using-declaration 和 enum
:
struct A {
enum e { /* ... */ };
};
struct S {
using enum A::e;
};
但是当我将它应用到模板时:
template <class T>
struct S {
using enum T::e;
};
gcc rejects 它与:
<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 也拒绝它:
<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
我不知道为什么这不起作用,因为它似乎与非模板没有什么不同。
这是编译器错误还是格式错误?
来自proposal:
The elaborated-enum-specifier shall not name a dependent type and the type shall have a reachable enum-specifier.
在您的示例中,T::e
是依赖类型。
根据 cppreference, both gcc and msvc have completed the implementation of C++20 feature using enum
, which means we can using-declaration 和 enum
:
struct A {
enum e { /* ... */ };
};
struct S {
using enum A::e;
};
但是当我将它应用到模板时:
template <class T>
struct S {
using enum T::e;
};
gcc rejects 它与:
<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 也拒绝它:
<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
我不知道为什么这不起作用,因为它似乎与非模板没有什么不同。
这是编译器错误还是格式错误?
来自proposal:
The elaborated-enum-specifier shall not name a dependent type and the type shall have a reachable enum-specifier.
在您的示例中,T::e
是依赖类型。