在c ++中转发枚举枚举 [英] Forward declaring an enum in c++

查看:100
本文介绍了在c ++中转发枚举枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行以下操作:

 枚举E; 

void Foo(E e);

枚举E {A,B,C};编译器拒绝的

我已经快速浏览了Google,而且这个共识似乎是你不能这样做,但是我不明白为什么。有人可以解释吗非常感谢。



澄清2:我正在这样做,因为我有一个类中的私有方法,使用这个枚举,我不希望枚举的值被暴露 - 所以例如,我不想让任何人知道E被定义为

 枚举E {
FUNCTIONALITY_NORMAL,FUNCTIONALITY_RESTRICTED ,FUNCTIONALITY_FOR_PROJECT_X
}

因为项目X不是我想让我的用户知道的东西。



所以,我想转发声明枚举,所以我可以把私有方法放在头文件中,在cpp内部声明枚举,并分发内置的库文件对于编译器来说,这是GCC。

解决方案

枚举不能转发的原因是,在不知道这些值的情况下,编译器无法知道枚举变量所需的存储空间。允许C ++编译器根据包含所有指定值所需的大小来指定实际的存储空间。如果所有可见的都是前向声明,则翻译单元不能知道将选择什么存储大小 - 它可以是一个char或一个int或其他内容。






从ISO C ++标准的第7.2.5节


枚举的底层类型是可以表示枚举中定义的所有枚举值的整体类型。它是实现定义的,哪个整数类型被用作枚举的基础类型,除了底层类型不应大于 int ,除非枚举器的值不能适合 int unsigned int 。如果枚举器列表为空,则底层类型就好像枚举具有值为0的单个枚举器。 sizeof()应用于枚举类型,枚举类型对象或枚举器的值是应用于基础类型的 sizeof()的值。


由于函数的调用者必须知道参数的大小才能正确设置调用堆栈,枚举列表中的枚举数必须在函数原型之前知道。



更新:
在C ++ 0X中,已经提出并接受了向前声明枚举类型的语法。您可以在 http:// www .open-std.org / jtc1 / sc22 / wg21 / docs / papers / 2008 / n2764.pdf


I'm trying to do something like the following:

enum E;

void Foo(E e);

enum E {A, B, C};

which the compiler rejects. I've had a quick look on Google and the consensus seems to be "you can't do it", but I can't understand why. Can anyone explain? Many thanks.

Clarification 2: I'm doing this as I have private methods in a class that take said enum, and I do not want the enum's values exposed - so, for example, I do not want anyone to know that E is defined as

enum E {
    FUNCTIONALITY_NORMAL, FUNCTIONALITY_RESTRICTED, FUNCTIONALITY_FOR_PROJECT_X
}

as project X is not something I want my users to know about.

So, I wanted to forward declare the enum so I could put the private methods in the header file, declare the enum internally in the cpp, and distribute the built library file and header to people.

As for the compiler - it's GCC.

解决方案

The reason the enum can't be forward declared is that without knowing the values, the compiler can't know the storage required for the enum variable. C++ Compiler's are allowed to specify the actual storage space based on the size necessary to contain all the values specified. If all that is visible is the forward declaration, the translation unit can't know what storage size will have been chosen - it could be a char or an int, or something else.


From Section 7.2.5 of the ISO C++ Standard:

The underlying type of an enumeration is an integral type that can represent all the enumerator values defined in the enumeration. It is implementation-defined which integral type is used as the underlying type for an enumeration except that the underlying type shall not be larger than int unless the value of an enumerator cannot fit in an int or unsigned int. If the enumerator-list is empty, the underlying type is as if the enumeration had a single enumerator with value 0. The value of sizeof() applied to an enumeration type, an object of enumeration type, or an enumerator, is the value of sizeof() applied to the underlying type.

Since the caller to the function must know the sizes of the parameters to correctly setup the call stack, the number of enumerations in an enumeration list must be known before the function prototype.

Update: In C++0X a syntax for foreward declaring enum types has been proposed and accepted. You can see the proposal at http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2764.pdf

这篇关于在c ++中转发枚举枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆