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

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

问题描述

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



我的代码示例:

  A类; 
enum E; //必须是枚举E:int;以便编译

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

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



为什么这还没有被标准化?

解决方案

p>这已经标准化,提议2764:转发如果你指定了底层类型,那么枚举的声明(修订3)允许前向声明枚举,而在此之前是不可能的。



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



< >

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


当通过值时,不知道底层类型是一个问题是有意义的,但为什么是一个问题,当它是一个指针或引用?这很重要,因为显然在某些架构上, char * int * comp.lang.c ++讨论:GCC和转发枚举的声明:


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


我们有以下stackoverflow答案供参考,其中描述了 char *可以大于int * 的情况,它会备份



一些更多关于可能的指针大小的详细信息,它看起来像 char * void * 是两个主要的例外这里和其他对象指针不应该有相同的问题。所以看起来这种情况最终是唯一的枚举。


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

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);

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

Why this has not been standardized?

解决方案

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.

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]:

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.

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:

[...] 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*[...]

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.

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天全站免登陆