C ++:如何在声明它的模板类主体之外定义一个枚举类?

问题描述 投票:1回答:1

我有一些C ++代码采用以下形式:

template <typename type>
class foo
{
    type a;
    class bar;
};

template <typename type>
class foo<type>::bar
{
    enum class baz;
};

template <typename type>
enum class foo<type>::bar::baz
{
    val1,
    val2
};

使用此代码,我试图通过foo :: bar中的方法访问枚举类,并且能够存储此枚举类的类型的数据。枚举类也不是模板类型 - 枚举类枚举器是整数/枚举类的默认类型。

但是,当我编译这是MinGW / Code :: Blocks时,这似乎产生两条错误消息,两者都在线上:

enum class foo<type>::bar::baz

错误:'enum baz'的模板声明

错误:foo <type> :: bar尚未声明

c++ templates enums nested forward-declaration
1个回答
2
投票

我认为这几乎肯定是一个编译器错误。基于temp.mem.classtemp.mem.enum,我认为这绝对应该是有效的C ++。 clang以及icc似乎编译这段代码就好了。然而,GCC(MinGW基本上是GCC)以及MSVC显然无法编译。似乎两个编译器(即使在最新版本中)都错误地将类模板的枚举成员定义为尝试声明枚举模板(这确实是非法的)......

quick test here

编辑:在MSVC的情况下,似乎已经有一个开放的问题here

© www.soinside.com 2019 - 2024. All rights reserved.