是类的枚举转发声明吗? [英] Is in-class enum forward declaration possible?
问题描述
我知道在C ++ 11可以转发声明一个枚举类型(如果提供存储类型)。
枚举E:short;
void foo(E e);
....
枚举E:short
{
VALUE_1,
VALUE_2,
.... $但我想转发声明一个类中定义的枚举,例如 枚举Foo :: E:short;
void foo(E e);
....
class Foo
{
enum E:short
{
VALUE_1,
VALUE_2,
....
}
}
这样的可能在C ++ 11?
解决方案不,这样的向前声明是不可能的。 [decl.enum] / 5(粗体强调我):
如果 enum-key
枚举指示符应指的是以前在类中直接声明的枚举,即
nested-name-specifier 所指向的命名空间(即既不是继承的,也不是使用声明引入的
)
(在这种情况下, nested-name-specifier 将是您的类的名称,后跟 ::
。)
但是,枚举外部并使用不透明枚举声明。
I know that in C++11 it's possible to forward declare an enum type (if storage type is provided) e.g.
enum E : short;
void foo(E e);
....
enum E : short
{
VALUE_1,
VALUE_2,
....
}
But I would like to forward declare an enum defined within a class e.g.
enum Foo::E : short;
void foo(E e);
....
class Foo
{
enum E : short
{
VALUE_1,
VALUE_2,
....
}
}
Is something like this possible in C++11 ?
解决方案 No, such a forward declaration isn't possible. [decl.enum]/5 (bold emphasis mine):
If the enum-key is followed by a nested-name-specifier, the
enum-specifier shall refer to an enumeration that was previously
declared directly in the class or namespace to which the
nested-name-specifier refers (i.e., neither inherited nor introduced
by a using-declaration), and the enum-specifier shall appear in a
namespace enclosing the previous declaration.
(In this case the nested-name-specifier would be the name of your class followed by a ::
.)
You could, though, put the enumeration outside and use an opaque-enum-declaration.
这篇关于是类的枚举转发声明吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!