枚举类型布局是否与其底层类型兼容? [英] Are enumeration types layout compatible with their underlying type?

查看:153
本文介绍了枚举类型布局是否与其底层类型兼容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过 n3690 查看即将到来的 C ++ 14 标准的草稿,我看到在部分 7.2 段落 9

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?

推荐答案

否,标准中没有指定此字母的黑体字报价。最接近的是同一段落的第7点

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 [...]底层类型是一个整数类型,代表所有
枚举中定义的枚举值。如果没有整数类型
可以表示所有的枚举值,枚举是
形式。 [...]

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

此外,4.5积分促销[conv.prom]说

Furthermore, 4.5 Integral promotions [conv.prom] says


4基本类型为
fied(7.2)的无范围枚举类型的prvalue可以转换为其基本类型的prvalue。

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.

正如评论中所指出的,可能有(猥亵的IMO)实现在枚举及其基础类型之间具有不同的字节序。这将是一个执行质量问题。对于所有实际用途,应该预期布局兼容性。

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.

这篇关于枚举类型布局是否与其底层类型兼容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆