为什么在向前宣布时必须提供枚举的大小? [英] Why must an enumeration's size be provided when it is forward declared?

查看:151
本文介绍了为什么在向前宣布时必须提供枚举的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么枚举的大小与编译器相关,而类的大小不是。

I just can't see why the size of the enumeration is relevant for the compiler while the size of the class is not.

我的代码示例: p>

My code example:

class A;
enum E;   // must be enum E : int; in order to compile 

void f(const A & param);
void f(const E & param);

我在这里谈论标准的C ++编译器。
我知道MSVC让它编译和工作正常。所以问题是:

I'm talking here of standard C++ compilers. I know that MSVC let it compile and works fine. So the question is:

为什么没有标准化?

推荐答案

p>这已经标准化了,提案2764:如果您指定了基础类型,而在此之前不可能,枚举的转发声明(rev。3)允许向前声明枚举。

This has been standardized, proposal 2764: Forward declaration of enumerations (rev. 3) allowed the forward declaration of enums if you specify the underlying type, whereas before this was not possible.

主要原因是当底层类型未指定时,大小是实现定义的,并且可以依赖于枚举器值。从C ++ 11标准部分草案 7.2 [dcl.enum]

The main reason is that when the underlying type is not specified the size is implementation defined and can depend on the enumerator values. From the draft C++11 standard section 7.2 [dcl.enum]:


对于底层类型不固定的枚举,底层
类型是一个整数类型,可以表示枚举中定义的所有枚举值
。如果没有整数类型可以表示所有的
枚举值,枚举是不成形的。它是
实现定义哪个整数类型用作底层
类型,除了底层类型不应大于int
,除非枚举器的值不能适合int或unsigned
int。如果枚举器列表为空,那么底层类型就好像
枚举有一个值为0的枚举器。

For an enumeration whose underlying type is not fixed, the underlying type is an integral type that can represent all the enumerator values defined in the enumeration. If no integral type can represent all the enumerator values, the enumeration is ill-formed. It is implementation-defined which integral type is used as the underlying type 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.

当通过值传递它是有道理的,不知道底层类型是一个问题,但为什么它是一个问题,当它是一个指针或引用?这很重要,因为显然在某些架构上, char * int * 可以具有不同的大小,如 comp.lang.c ++讨论:GCC和枚举的前向声明

When passing by value it makes sense that not knowing the underlying type is an issue, but why is it an issue when it is a pointer or reference? This matters since apparently on some architectures, char* and int* can have different sizes as mentioned in this comp.lang.c++ discussion: GCC and forward declaration of enum:


[...]虽然在大多数架构上可能不是一个问题,在某些
架构上,指针将具有不同的大小,以防它是一个
的char指针。所以最后我们想象的编译器不知道
什么放在那里得到一个ColorsEnum * [...]

[...] While on most architectures it may not be an issue, on some architectures the pointer will have a different size, in case it is a char pointer. So finally our imaginary compiler would have no idea what to put there to get a ColorsEnum*[...]

我们有以下stackoverflow答案供参考,其中描述了 char *可能大于int * 的情况,后者支持

We have the following stackoverflow answer for reference which describes the case where char* can be larger than int*, which backs up the assertion in the discussion above.

某些关于指针的可能大小的更多细节,它看起来像 char * void * 是两个主要这里的异常,所以其他对象指针不应该有相同的问题。所以看起来这个例子最终是枚举的唯一。

Some more details on the possible sizes of pointers it looks like char * and void * are the two main exceptions here and so other object pointers should not have the same issues. So it seems like this case ends up being unique to enums.

这篇关于为什么在向前宣布时必须提供枚举的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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