简体   繁体   English

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

[英]C++: How to define an enum class outside of a template class body in which it is declared?

I have some C++ code that takes the following form: 我有一些采用以下形式的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
};

With this code, I'm trying to get the enum class to be accessible by methods inside foo::bar and to be able to store data of the type of this enum class. 通过此代码,我试图使foo :: bar中的方法可以访问该枚举类,并能够存储该枚举类类型的数据。 The enum class is also not meant to be of a template type - the enum class enumerators are integers/the default type of an enum class. 枚举类也不是模板类型-枚举类的枚举数是整数/枚举类的默认类型。

However, when I compile this is MinGW/Code::Blocks this seems to produce two error message, both on the line: 但是,当我编译这是MinGW / Code :: Blocks时,这似乎会产生两个错误消息,都在行上:

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

error: template declaration of 'enum baz' 错误:“ enum baz”的模板声明

error: foo<type>::bar has not been declared 错误:尚未声明foo <type> :: bar

I think this is almost certainly a compiler bug. 我认为这几乎可以肯定是编译器错误。 Based on temp.mem.class and temp.mem.enum , I'd say this should definitely be valid C++. 基于temp.mem.classtemp.mem.enum ,我会说这肯定是有效的C ++。 clang as well as icc seem to compile this code just fine. clang和icc似乎都可以编译此代码。 GCC (MinGW is basically GCC) as well as MSVC apparently fail to compile this, however. 但是,GCC(MinGW基本上是GCC)以及MSVC显然无法对此进行编译。 It seems both compilers (even in their most recent versions) mistake this definition of an enum member of a class template for an attempt to declare an enum template (which would indeed be illegal)… 似乎两个编译器(即使在最新版本中)都错误地将此类对类模板的枚举成员的定义误认为是试图声明一个枚举模板(这确实是非法的)……

quick test here 在这里快速测试

Edit: In case of MSVC, there seems to already be an open issue here 编辑:在MSVC的情况下,似乎已经是一个开放的问题在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 C++ 中的类体之外定义一个专门的类方法? - How to define a specialized class method outside of class body in C++? 如何在类主体之外定义类模板的模板化方法 - How to define a templated method of a class template outside of class body 如何定义模板类模板友元函数,其参数之一是模板类主体之外的依赖名称? - How to define a template class template friend function which one of its parameters is a dependent name outside the template class body? C ++为什么我不能在声明它的类之外使用全局声明的枚举? - C++ Why am I unable to use an enum declared globally outside of the class it was declared in? 如何定义在两个类之外的模板类内部的非模板类中声明的友元函数? - How to define a friend function declared in a non template class internal to a template class outside of both classes? C ++-在模板类之外但在标头中定义成员函数 - C++ - Define member function outside template-class but in header 声明后初始化C ++模板类 - Initialize C++ Template Class After it is Declared 如何在 C++ 中定义其父级之外的嵌套类 - How to define a nested class outside its parent in C++ C ++类相关的typedef在类外部声明 - c++ class dependant typedef declared outside class C ++将内部枚举用于类模板 - C++ use inner enum for class template
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM