c++ - 枚举类型布局是否与其基础类型兼容?

标签 c++ enums language-lawyer c++14

我正在浏览 n3690 ,即将发布的草稿C++14标准,我在 7.2 部分看到段9 :

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



但是,我找不到任何说明枚举类型与其基础类型布局兼容的内容。对我来说,考虑到“基础类型”的含义的合理语义,这应该遵循这一点似乎很明显,但标准是否真的保证了这一点?

最佳答案

不,标准中没有黑字引用来说明这一点。最接近的是同一段落的第 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 Integral Promotions [conv.prom] 说

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)实现在枚举及其基础类型之间具有不同的字节序。这将是一个实现质量问题。出于所有实际目的,应该期望布局兼容性。

关于c++ - 枚举类型布局是否与其基础类型兼容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21956017/

相关文章:

c++ - 构建优化的 Qt4 - "./configure"标志及其含义

ruby-on-rails - 从枚举模型中设置 Rails 表单隐藏字段

c++ - 为什么 std::numeric_limits<SomeStruct>::infinity() "work"?

javascript - Angular:将字符串数组映射到 http get 调用中的枚举值数组

c++ - 具有非推导上下文的部分特化排序

c++ - 为什么 C struct hack 不适用于 C++ 模板声明?

c++ - 从 operator[] 返回对映射的 char* 的引用

c++ - 四元数 -> 欧拉角 -> 旋转矩阵问题 (GLM)

c++ - Windows 8 模拟器永远加载

java - 使用 Switch 语句创建对象 : Cannot make a static reference to the non-static method?