如果枚举不能适合整数类型,会发生什么? [英] What happens if an enum cannot fit into an integral type?
问题描述
我遇到了关于底层类型的此问题的枚举,其中答案标准C ++ 7.2 / 5如下:
枚举的基础类型是一个整数类型,在枚举中定义的枚举值。它是实现定义的,哪个整数类型用作枚举的基本类型,除非底层类型不能大于int,除非枚举器的值不能适合int或unsigned int。
这在所有合理情况下都很清楚。 但是,如果我将枚举设置得如此可笑,甚至不能适应长时间,会发生什么?
)
这个行为是否由标准定义?
strong>的行为
enum foo:int
{
bar = INT_MAX,
oops
};
且类似未定义。
我通过将类型强制为 int
来欺骗了一下,但这同样适用于您平台上可用的最大整数类型。 p>
I came across this question about the underlying types of enums, where an answers quotes Standard C++ 7.2/5 as:
The underlying type of an enumeration is an integral type that can represent all the enumerator values defined in the enumeration. It is implementation-defined which integral type is used as the underlying type for an enumeration except that the underlying type shall not be larger than int unless the value of an enu- merator cannot fit in an int or unsigned int.
This is pretty clear for all reasonable cases. But what happens if I make an enum so ridiculously large that it can't even fit in a long long?
(I don't know why this would ever happen in practice, but maybe I'm feeling destructive and have a free afternoon)
Is this behavior defined by the standard?
The behaviour of
enum foo : int
{
bar = INT_MAX,
oops
};
and similar is undefined.
I've cheated a little here by forcing the type to an int
, but the same applies to the largest integral type available on your platform.
这篇关于如果枚举不能适合整数类型,会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!