The snippet below should not compile according to [dcl.enum]/7 in the C++ Standard.

  • Thread starter Thread starter Belloc
  • Start date Start date
B

Belloc

Guest
The following code

#include <iostream>
#include <typeinfo>

enum E {
a,
b = 0x7FFFFFFFFFFFFFFF
};

int main()
{
std::cout << "sizeof(0x7FFFFFFFFFFFFFFF) " << sizeof(0x7FFFFFFFFFFFFFFF) << '\n';
std::cout << "sizeof(enum E) " << sizeof(enum E) << '\n';
std::cout << "sizeof(E underlying type) " << sizeof(std::underlying_type<E>::type) << '\n';
}

prints

sizeof(0x7FFFFFFFFFFFFFFF) 8
sizeof(enum E) 4
sizeof(E underlying type) 4

But the code should not compile according to [dcl.enum]/7 which says (emphases are mine):

For an enumeration whose underlying type is not fixed, the underlying type is an integral type that can represent all the enumerator values defined in the enumeration.

If no integral type can represent all the enumerator values, the enumeration is ill-formed.

It is implementation-defined which integral type is used as the underlying type except that the underlying type shall not be larger than int unless the value of an enumerator cannot fit in an int or unsigned int.


If the enumerator-list is empty, the underlying type is as if the enumeration had a single enumerator with value 0.



PS: The compiler just emits a warning (C4309) for the assignment

b = 0x7FFFFFFFFFFFFFFF

Continue reading...
 
Back
Top