Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 883
Bear Avatar asked Sep 21 '16 15:09

Bear


1 Answers

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.

like image 154
Bathsheba Avatar answered Oct 27 '22 00:10

Bathsheba