Bear
Bear

Reputation: 355

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?

Upvotes: 12

Views: 362

Answers (1)

Bathsheba
Bathsheba

Reputation: 234715

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.

Upvotes: 10

Related Questions