布局兼容类型的目的是什么? [英] What's the purpose of layout-compatible types?

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

问题描述

当两种类型 layout-compatible时,标准定义 .但是,我看不到标准中的任何地方,当两种类型 layout-compatible 会带来什么后果.似乎 layout-compatible 是一个未在任何地方使用的定义.

The standard defines when two types are layout-compatible. But, I don't see anywhere in the standard what the consequences are when two types are layout-compatible. It seems that layout-compatible is a definition which is not used anywhere.

布局兼容的目的是什么?

注意:可能意味着类型具有相同的布局(每个对应成员的offsetof相同),因此,例如,对于平凡可复制的类型,可以在它们之间复制基础字节.但是我在标准中看不到这样的东西.

Note: Supposedly, it could mean that the types have the same layout (offsetof is the same for each corresponding member), so for example, for trivially copyable types, underlying bytes can be copied between them. But I don't see something like this in the standard.

推荐答案

该标准确实定义了布局兼容性很重要的一种特定情况:在union s中.如果两个成员是与布局兼容的成员,并且其中一个是活动的union成员,则可以通过指向该union的任何与布局兼容的成员的指针/引用来访问该对象.这是通用初始序列"规则的结果.

The standard does define one specific case where layout compatibility matters: in unions. If two members are layout-compatible, and one of them is the active union member, then you may access that object through pointers/references to any layout-compatible member of that union. This is a consequence of the "common initial sequence" rule.

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

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