c++ - 我们可以专门化类模板的枚举(类型)成员吗?

标签 c++ enums

Cppreference声称,除其他事项外,您可以专注于

  1. member enumeration of a class template

由于没有提供示例,我试图猜测如何做到这一点。

我最终得到了以下结果:

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

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

Clang(带有 -std=c++17 -pedantic-errors 的 8.0.0)编译它。

GCC (9.1 with -std=c++17 -pedantic-errors) 拒绝代码

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

MSVC (v19.20 with /std:c++latest 也拒绝代码

error C3113: an 'enum' cannot be a template

Try it on gcc.godbolt.org

我是否正确地特化了枚举?如果没有,现在我要这样做吗?

最佳答案

the standard中有例子([temp.expl.spec]/6) 表明你所拥有的是正确的。那里是:

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

似乎是 gcc 错误。

关于c++ - 我们可以专门化类模板的枚举(类型)成员吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56300432/

相关文章:

c++ - dyld : Library not loaded: lib/libopencv_core. 3.0.dylib 原因:找不到图片

c++ - 如何将数字(用空格分隔)读入数组?

c++ - 从 Excel VBA 使用 C++ dll 时出现 "Bad DLL calling convention"- 如何解决?

java - 在枚举抽象方法中转换对象

c++ - 使用简单的 Boost::Spirit 语法?

c++ - 无法获取进程句柄

c++ - 扩展枚举类型

c# - 来自枚举的单选按钮组

c - 对 C 枚举的部分字节访问

c# - 我们可以将枚举作为通用数据类型吗?