gpt4 book ai didi

c - 如何将枚举常量的值设置在int范围之外?

转载 作者:太空狗 更新时间:2023-10-29 17:00:39 24 4
gpt4 key购买 nike

C99 标准要求用于定义枚举常量值的表达式具有可表示为 int 的值。

在 C99 标准的第 6.7.2.2 节第 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.

但是,枚举类型可以由实现定义为与任何整数类型兼容,包括值范围在 int 之外的整数类型。

在 C99 标准的第 6.7.2.2 节第 2 段中:

Each enumerated type shall be compatible with char, a signed integer type, or an unsigned integer type.

这意味着虽然您不能显式设置 int 范围之外的枚举常量值,但枚举常量的值可以在 int 范围之外code> 如果实现将枚举类型定义为与范围在 int 之外的整数类型兼容。


现在我知道了一种获取超出 int 范围的特定值的方法:dummy enumerators

enum hack{
DUMMY0 = INT_MAX,
DUMMY1,
/* supply as many more dummy enumerators as needed */
...
/* declare desired enumerator */
FOOBAR
};

这要归功于 C99 标准的 section 6.7.2.2 第 3 段:

An enumerator with = defines its enumeration constant as the value of the constant expression.
...
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.

不幸的是,这只适用于大于INT_MAX 值,因为每个后续枚举器的值只会递增。另一个警告是可能需要创建许多虚拟枚举器,以获取所需的特定枚举器。


这导致了以下问题:

  1. 有没有办法将枚举常量的值设置为 int 范围之外的值?
  2. 有没有更好的方法将 int 范围之外的值设置为枚举常量?
  3. 关于我的虚拟枚举器 hack,C99 标准是否对可以在单个enum 中声明的枚举器数量设置了限制?

最佳答案

How to set the value of an enumeration constant outside the range of int?

你不知道。

The C99 standard requires that the expression used to define the value of an enumeration constant has a value representable as an int.

是的,C11 标准没有改变这一切。

However, enumerated types can be defined by the implementation to be compatible with any integer type, including those with a range of values outside of int.

也正确。

This means that while you cannot explicitly set the value of an enumeration constant outside the range of an int, the value of an enumeration constant can be outside the range of an int if the implementation defines the enumeration type to be compatible with an integer type with a range outside of int.

这是不正确的,但我认为您发现了标准措辞中的一个弱点。 (更新:我不认为这真的是一个弱点;见下文)。你引用了 6.7.2.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.

这似乎仅在值由显式 表达式定义时适用,不适用于这样的情况:

enum too_big {
big = INT_MAX,
even_bigger
};

但这实际上行不通,因为even_bigger被声明为 int 类型的常量,显然不能有值 INT_MAX + 1 .

我强烈怀疑其意图是上面的声明是非法的(违反约束);可能 6.7.2.2 应该改写以使其更清楚。 (更新:我现在认为已经足够清楚了;见下文。)

gcc 的作者似乎同意我的观点:

$ cat c.c
#include <limits.h>
enum huge {
big = INT_MAX,
even_bigger
};
$ gcc -c c.c
c.c:4:5: error: overflow in enumeration values

因此,即使您的解释是正确的,您也不太可能能够编写和使用依赖于它的代码。

解决方法是使用整数(无论如何,枚举类型或多或少都是经过伪装的整数)。 const不幸的是,整数对象不是常量表达式,因此您可能不得不求助于使用预处理器:

typedef long long huge_t;
#define big ((huge_t)INT_MAX)
#define even_bigger (big + 1)

这假设long longint 宽,这很可能但不能保证(如果 int 至少为 64 位,则 long longint 可能大小相同)。

您的问题 1 和 2 的答案是否定的;您不能在 int 的范围之外定义枚举常量,无论是负数还是正数 .

关于您的问题 3,C11 标准的第 5.2.4.1 节(粗略地)说编译器必须在单个枚举中支持至少 1023 个枚举常量。大多数编译器实际上并没有施加固定的限制,但在任何情况下,所有常量的值都必须在 INT_MIN 范围内。 .. INT_MAX ,所以这对你没有多大好处。 (同一类型的多个枚举常量可以有相同的值。)

(翻译限制要求实际上比这更复杂。编译器必须支持至少一个程序,该程序至少包含所有枚举限制列表的一个实例。这是一个相当无用的要求如前所述。目的是满足标准要求的最简单方法是避免施加任何固定限制。)

更新:

我在 comp.std.c Usenet 新闻组上提出了这个问题。 Tim Rentsch 在那次讨论中提出了一个很好的观点,我现在认为:

enum too_big {
big = INT_MAX,
even_bigger
};

违反约束,需要编译器诊断。

我担心的是禁止显式值超出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.

不适用,因为不涉及(明确的)表达式。但是 6.7.7.2p3 说:

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.

(强调)。所以有一个表达式,其值必须可以表示为int。 ;它只是没有出现在源代码中。我对此不是 100% 满意,但我想说意图足够明确。

这是 discussion在 comp.std.c 上。

关于c - 如何将枚举常量的值设置在int范围之外?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18090541/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com