Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are enumeration types layout compatible with their underlying type?

I'm looking through n3690, a draft of the upcoming C++14 standard, and I see in section 7.2 paragraph 9:

Two enumeration types are layout-compatible if they have the same underlying type.

However, I can't find anything that says an enumeration type is layout-compatible with its underlying type. To me it seems obvious that this should follow given the reasonable semantics for what "underlying type" means, but is it actually guaranteed by the standard?

like image 324
Shea Levy Avatar asked Oct 21 '22 13:10

Shea Levy


1 Answers

NO, there is no black-letter quote from the Standard that specifies this. The closest that one can get is point 7 of that same paragraph

7 [...] 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. [...]

Furthermore, 4.5 Integral promotions [conv.prom] says

4 A prvalue of an unscoped enumeration type whose underlying type is fixed (7.2) can be converted to a prvalue of its underlying type.

As pointed out in the comments, there could be (devious IMO) implementations that have different endianess between an enum and its underlying type. That would be a Quality of Implementation issue. For all practical purposes, layout-compatibility should be expected.

like image 83
TemplateRex Avatar answered Oct 29 '22 22:10

TemplateRex