【问题标题】:Can C++20 using enum apply to templates?使用枚举的 C++20 可以应用于模板吗?
【发布时间】:2021-07-04 02:29:45
【问题描述】:

根据cppreference,gcc和msvc都完成了C++20特性using enum的实现,这意味着我们canusing-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

我不知道为什么这不起作用,因为它似乎与非模板没有什么不同。

这是编译器错误还是格式错误?

【问题讨论】:

    标签: c++ templates enums c++20 using-declaration


    【解决方案1】:

    来自proposal

    详细枚举说明符不应命名依赖类型,并且该类型应具有可访问的枚举说明符。

    在您的示例中,T::e 是一个依赖类型。

    【讨论】:

    • 知道为什么依赖类型不允许这样做吗?
    • @Timo 不,不是真的 ;)
    猜你喜欢
    • 2020-01-14
    • 1970-01-01
    • 2016-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-05
    • 2014-09-20
    • 1970-01-01
    相关资源
    最近更新 更多