Are enumeration types layout compatible with their underlying type?

前端 未结 1 364
日久生厌
日久生厌 2021-01-13 04:59

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

相关标签:
1条回答
  • 2021-01-13 06:03

    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.

    0 讨论(0)
提交回复
热议问题