代码之家  ›  专栏  ›  技术社区  ›  HolyBlackCat

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

  •  3
  • HolyBlackCat  · 技术社区  · 5 年前

    Cppreference 声称,除其他外,你可以

    1. 类模板的成员枚举

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

    我的结局是:

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

    叮当声(8.0.0与 -std=c++17 -pedantic-errors 编译它。

    海湾合作委员会(9.1) STD=C++ 17学究错误 )拒绝代码

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

    MSVC(V19.20带 /std:c++latest 同时拒绝代码

    error C3113: an 'enum' cannot be a template
    

    Try it on gcc.godbolt.org

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

    1 回复  |  直到 5 年前
        1
  •  2
  •   Ryan Haining    5 年前

    中有一些示例 the standard ([temp.expl.spec]/6)这表明你拥有的是正确的。其中之一是:

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

    好像是GCC的bug。