枚举可以容纳大于INT_MAX的无符号整数吗? [英] can an enum hold unsigned integers greater than INT_MAX?

查看:404
本文介绍了枚举可以容纳大于INT_MAX的无符号整数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

enum Some_Flag {
        SOME_FLAG_A = 0x00000000u,
        SOME_FLAG_B = 0x00000001u,
        SOME_FLAG_C = 0x00000002u,
        /* ... */
        SOME_FLAG_Z = 0x80000000u,
};

uint32_t a;
a = SOME_FLAG_Z;

假设32位整数...
在C中有效吗?

Assuming 32 bit integers... Is this valid in C?

标准对我来说似乎是模棱两可的。

The standard seems ambiguous to me.

编辑:

引用标准:


6.4.4.3枚举常量

6.4.4.3 Enumeration constants

语义

2声明为枚举常量的标识符的类型为int。
前向引用:枚举说明符(6.7.2.2)。

2 An identifier declared as an enumeration constant has type int. Forward references: enumeration specifiers (6.7.2.2).

6.7.2.2枚举说明符

6.7.2.2 Enumeration specifiers

约束

2定义枚举常量值的表达式
是一个整数常量表达式,其值可表示为
int。

2 The expression that defines the value of an enumeration constant shall be an integer constant expression that has a value representable as an int.

语义

3枚举数列表中的标识符声明为
类型为int的常量127)带=的
枚举器将其枚举常量定义为
常量表达式的值。如果第一个枚举数没有=,则其
枚举常数的值为0。每个后续的没有=的枚举数将其枚举常数定义为
,作为通过将1加1得到的常数表达式
的值。到先前的枚举常数的值。
(使用带有=的枚举数可能会产生具有
值的枚举常量,该值会重复同一枚举中的其他值。)枚举的
枚举数也称为其成员。

3 The identifiers in an enumerator list are declared as constants that have type int and may appear wherever such are permitted.127) An enumerator with = defines its enumeration constant as the value of the constant expression. If the first enumerator has no =, the value of its enumeration constant is 0. Each subsequent enumerator with no = defines its enumeration constant as the value of the constant expression obtained by adding 1 to the value of the previous enumeration constant. (The use of enumerators with = may produce enumeration constants with values that duplicate other values in the same enumeration.) The enumerators of an enumeration are also known as its members.

4每个枚举类型应与char,有符号整数
类型或无符号整数类型兼容。类型的选择是
实现定义的(128),但应能够代表枚举的所有成员的
值。枚举类型为
,直到}紧接终止
枚举器声明列表的位置之后,才完整,然后才完成。

4 Each enumerated type shall be compatible with char, a signed integer type, or an unsigned integer type. The choice of type is implementation-defined,128) but shall be capable of representing the values of all the members of the enumeration. The enumerated type is incomplete until immediately after the } that terminates the list of enumerator declarations, and complete thereafter.

C11草案

约束似乎清楚地表明一个枚举是一个int,但是6.7.2.2_4似乎允许无符号的int ??

The constraints seem to clearly indicate that an enum is an int, but then 6.7.2.2_4 seems to allow unsigned ints ¿?

推荐答案

您的代码无效:

C90(6.5.2.2,枚举说明符):

C90 (6.5.2.2, Enumeration specifiers):


约束

定义枚举常量值的表达式应为整数常量表达式,其值可表示为 int

The expression that defines the value of an enumeration constant shall be an integral constant expression that has a value representable as an int.

C99(C11草案中未更改)(6.7.2.2,枚举说明符):

C99 (and unchanged in the C11 draft) (6.7.2.2, Enumeration specifiers):


约束


  1. 表达定义枚举常量值的应该是整数常量表达式,其值可以表示为 int

  1. The expression that defines the value of an enumeration constant shall be an integer constant expression that has a value representable as an int.


您的值超出了32位 int 的范围,因此这是一个错误(需要诊断) 。

Your values exceed the range of a 32-bit int, so this is an error (and a diagnostic is required).

请注意,这与初始化程序严格有关。枚举常量。例如,如果我们有

Note that this is strictly about the "initializers" of the enum constants. For example, if we have

enum foo { BAR = 42u };

然后,此约束表明值 42u 必须能够可以放在 int 中(它确实做到了;它只是未签名的42和42可以放在 int 中)。

then this constraint says that the value 42u must be able to fit in an int (which it does; it's just an unsigned 42 and 42 fits in an int).

BAR 本身的类型是 int (令人惊讶的是,不是 enum foo )。

The type of BAR itself is int (surprisingly enough, not enum foo).

但是,如果您声明类型为 enum foo 的变量,则它的大小和签名由实现定义。它将基于一些现有的整数类型(可以存储所有枚举值),但是实际使用的类型在实现之间可能有所不同(并且在不同的 enum 类型之间也可能不同)

But if you declare a variable of type enum foo, then its size and signedness is implementation defined. It will be based on some existing integer type (that can store all the enum values), but which type is actually used can differ between implementations (and also between different enum types).

这篇关于枚举可以容纳大于INT_MAX的无符号整数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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