恒枚举尺寸无论枚举值的数量 [英] Constant enum size no matter the number of enumerated values
问题描述
为什么是一个企业的大小枚举
总是2个或4个字节(分别为16位或32位架构),无论普查员的数量在类型?
Why is the size of an enum
always 2 or 4 bytes (on a 16- or 32-bit architecture respectively), regardless of the number of enumerators in the type?
编译器把一个枚举
像是在联盟
?
推荐答案
在C和C ++中,枚举
类型的大小是实现定义的,并且是相同某些整数类型的大小。
In both C and C++, the size of an enum
type is implementation-defined, and is the same as the size of some integer type.
一个常见的方法就是让所有枚举
类型相同的大小为 INT
,只是因为这是典型的类型这使得对于最有效的访问。使其成为一个单字节,例如,将节省的空间非常小的量,但可能需要更大,更慢code来访问它,这取决于CPU架构。
A common approach is to make all enum
types the same size as int
, simply because that's typically the type that makes for the most efficient access. Making it a single byte, for example, would save a very minor amount of space, but could require bigger and slower code to access it, depending on the CPU architecture.
在C,枚举的常量的是按类型定义的 INT
。因此,考虑:
In C, enumeration constants are by definition of type int
. So given:
enum foo { zero, one, two };
enum foo obj;
恩pression 零
的类型 INT
,但 OBJ <的/ code>的类型为
枚举富
,这可能会或可能不会有大小 INT
。鉴于常量类型 INT
的,它往往是更容易使枚举类型的大小相同。
the expression zero
is of type int
, but obj
is of type enum foo
, which may or may not have the same size as int
. Given that the constants are of type int
, it tends to be easier to make the enumerated type the same size.
在C ++中,规则是不同的;常量是枚举类型。但同样,这常常使得最有意义每个枚举
类型为一字,这是典型的 INT $ C的大小$ C>,出于效率的考虑。
In C++, the rules are different; the constants are of the enumerated type. But again, it often makes the most sense for each enum
type to be one "word", which is typically the size of int
, for efficiency reasons.
和2011年的ISO C ++标准添加到指定的枚举
键入底层整数类型的能力。例如,你现在可以这样写:
And the 2011 ISO C++ standard added the ability to specify the underlying integer type for an enum
type. For example, you can now write:
enum foo: unsigned char { zero, one, two };
这保证,无论是键入富
和常量零
,有一个
和两个
有一个大小为1字节。 C没有这个功能,并且它不支持旧的pre-2011 C ++编译器(除非他们提供它作为语言的扩展)。
which guarantees that both the type foo
and the constants zero
, one
, and two
have a size of 1 byte. C does not have this feature, and it's not supported by older pre-2011 C++ compilers (unless they provide it as a language extension).
(题外话如下。)
那么,如果你有一个枚举常量太大,不适合在 INT
?你并不需要2 31 ,甚至2 15 ,不同的常量要做到这一点:
So what if you have an enumeration constant too big to fit in an int
? You don't need 231, or even 215, distinct constants to do this:
#include <limits.h>
enum huge { big = INT_MAX, bigger };
的大
是 INT_MAX
,通常是2 31 -1的值,但可以作为小2 15 -1(32767)。值大
是隐含大+ 1
。
The value of big
is INT_MAX
, which is typically 231-1, but can be as small as 215-1 (32767). The value of bigger
is implicitly big + 1
.
在C ++中,这是确定的;编译器会简单地选择基础类型巨额
这是大到足以容纳值 INT_MAX + 1
。 (假设有这样一个类型;如果 INT
为64位,有没有整数类型比更大,这将是不可能的)
In C++, this is ok; the compiler will simply choose an underlying type for huge
that's big enough to hold the value INT_MAX + 1
. (Assuming there is such a type; if int
is 64 bits and there's no integer type bigger than that, that won't be possible.)
在C,因为枚举常量是类型 INT
,上面是无效的。它违反了 N1570规定的约束的6.7。 2.2p2:
In C, since enumeration constants are of type int
, the above is invalid. It violates the constraint stated in N1570 6.7.2.2p2:
这位前pression定义枚举常量的值应
是具有重presentable作为一个值的整型常量前pression
INT
The expression that defines the value of an enumeration constant shall be an integer constant expression that has a value representable as an int.
和因此编译器必须拒绝它,或者至少提醒一下。 GCC,例如,说:
and so a compiler must reject it, or at least warn about it. gcc, for example, says:
错误:溢出枚举值
这篇关于恒枚举尺寸无论枚举值的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!