I often assume that size of an enumeration is the same as the size of its underlying type. But is it mandated by the standard?
The standard (C++14, n4296) says that every enumeration has an underlying type (7.2/5). The standard also says that objects are represented as sequences of bytes, and that the size of an object is related to its representation:
3.9/4 The object representation of an object of type T is the sequence of N unsigned char objects taken up by the object of type T, where N equals sizeof(T).
5.3.3/1 The sizeof operator yields the number of bytes in the object representation of its operand.
However, I was not able to find any relation between an enum's underlying type and the object representation. Is there any? If not, than I would argue that sizeof of an enumeration does not have to be sizeof of its underlying type.
So my questions are:
Is there any relation between an enum's underlying type and its object representation?
Does the standard really require that
sizeof(std::underlying_type_t<E>) == sizeof(E)for any enumeration E?