在 C++ 中前向声明枚举 [英] Forward declaring an enum in C++

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

问题描述

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

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?

澄清 2:我这样做是因为我在一个采用上述枚举的类中有私有方法,并且我不希望暴露枚举的值 - 例如,我不想让任何人知道 E 已定义作为

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
}

因为项目 X 不是我想让我的用户知道的.

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

所以,我想转发声明枚举,这样我就可以将私有方法放在头文件中,在 cpp 内部声明枚举,并将构建的库文件和头文件分发给人们.

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.

至于编译器 - 它是 GCC.

As for the compiler - it's GCC.

推荐答案

枚举不能前向声明的原因是,在不知道值的情况下,编译器无法知道枚举变量所需的存储空间.允许 C++ 编译器根据包含所有指定值所需的大小来指定实际存储空间.如果所有可见的是前向声明,则翻译单元无法知道选择了多大的存储大小——它可能是 charint 或其他东西否则.

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++ compilers 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 has been chosen – it could be a char, or an int, or something else.

来自 ISO C++ 标准的第 7.2.5 节:

From Section 7.2.5 of the ISO C++ Standard:

枚举的底层类型是一个整数类型,可以表示枚举中定义的所有枚举值.使用哪种整数类型作为枚举的底层类型是实现定义的,除非底层类型不应大于 int,除非枚举器的值不能放入 intunsigned int.如果 enumerator-list 为空,则底层类型就好像枚举有一个值为 0 的枚举器. sizeof() 的值应用于枚举类型,枚举类型的对象或枚举数是应用于基础类型的 sizeof() 值.

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 set up the call stack, the number of enumerations in an enumeration list must be known before the function prototype.

更新:

在 C++0X 中,已经提出并接受了一种用于前向声明枚举类型的语法.您可以在 Forward 查看该提案枚举声明(修订版 3)

In C++0X, a syntax for forward declaring enum types has been proposed and accepted. You can see the proposal at Forward declaration of enumerations (rev.3)

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

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