我们可以专业化类模板的枚举(类型)成员吗?

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

本文关键字:类型 成员 枚举 专业化 我们      更新时间:2023-10-16

cppreference声称,除其他外,您可以专业

  1. 类模板的成员枚举

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

我最终得到以下:

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(带有-std=c++17 -pedantic-errors的9.1(用

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

msvc(带有/std:c++latest的V19.20也拒绝

的代码
error C3113: an 'enum' cannot be a template

在gcc.godbolt.org上尝试一下

我是否正确地专注于枚举?如果没有,现在我要这样做?

标准中有示例([temp.expl.spec]/6(建议您拥有的正确。那里有:

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

似乎是GCC错误。