What happens if an enum cannot fit into an integral type?

删除回忆录丶 提交于 2019-12-22 03:49:11

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/39620802/what-happens-if-an-enum-cannot-fit-into-an-integral-type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!