如果一个枚举不能适用于整体类型,会发生什么? [英] What happens if an enum cannot fit into an integral type?

查看:157
本文介绍了如果一个枚举不能适用于整体类型,会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了有关底层类型的这个问题的枚举,其中答案引用标准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屋!

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