相同类型布局的变量结构是否与包含该类型数组的结构兼容? [英] Are structs of variables of the same type layout compatible with a struct containing an array of that type?
问题描述
这两个结构布局兼容吗?
Are these 2 structs layout-compatible?
struct One {
float x, y, z;
};
struct Two {
float c[3];
};
两者都包含3个浮点数,因此在某种程度上,该描述可以被认为是正确的(来自N3797):
Both contains 3 floats, so in a way, this description can be considered true (from N3797):
16两种标准布局结构(第9条)与布局兼容 如果它们具有相同数量的非静态数据成员,并且 相应的非静态数据成员(按声明顺序)具有 布局兼容类型(3.9).
16 Two standard-layout struct (Clause 9) types are layout-compatible if they have the same number of non-static data members and corresponding non-static data members (in declaration order) have layout-compatible types (3.9).
N4659具有不同的文本:
N4659 has a different text:
两个标准布局结构的通用初始序列(第12条) 类型是非静态数据成员和位字段的最长序列 按照声明顺序,从每个 结构,以使相应的实体具有布局兼容的 类型,并且两个实体都不是位域,或者两者都不是位域 具有相同的宽度.
The common initial sequence of two standard-layout struct (Clause 12) types is the longest sequence of non-static data members and bit-fields in declaration order, starting with the first such entity in each of the structs, such that corresponding entities have layout-compatible types and either neither entity is a bit-field or both are bit-fields with the same width.
两种标准布局结构(第12条)与布局兼容 类,如果它们的共同初始序列包含所有成员,并且 两种类别的位域都为(6.9).
Two standard-layout struct (Clause 12) types are layout-compatible classes if their common initial sequence comprises all members and bit-fields of both classes (6.9).
如果回答是否,则说明它们与布局不兼容,那么:委员会的意图是吗?也许他们确实希望One
和Two
是布局兼容的(也许委员会的成员读了这个并可以澄清).
If the answer is no, they are not layout-compatible, then: was it the intention of the committee? Maybe they do want One
and Two
to be layout-compatible (maybe a committee member reads this, and can clarify).
奖金问题:可以保证sizeof(One)==sizeof(Two)
吗?
Bonus question: is it guaranteed, that sizeof(One)==sizeof(Two)
?
推荐答案
好吧,没有:
[...],如果它们具有相同数量的非静态数据成员[...]
[...] if they have the same number of non-static data members [...]
One
具有三个成员:x
,y
和z
. Two
具有一个成员:c
.它们没有相同数量的非静态数据成员,因此它们与布局不兼容.
One
has three members: x
, y
, and z
. Two
has one member: c
. They don't have the same number of non-static data members, therefore they aren't layout compatible.
新的措词不同,但最终却在同一个地方. [basic.types] 将布局兼容定义为: /p>
The new wording is different but you end up at the same place. [basic.types] defines layout-compatible as:
如果T1和T2是同一类型,布局兼容的枚举或布局兼容的标准布局类类型,则两种类型的cv1 T1和cv2 T2是布局兼容的类型.
Two types cv1 T1 and cv2 T2 are layout-compatible types if T1 and T2 are the same type, layout-compatible enumerations, or layout-compatible standard-layout class types.
[class.mem] 定义与布局兼容的类是:
[class.mem] defines layout-compatible classes are:
如果两个标准布局结构类型的公共初始序列包含两个类的所有成员和位字段([basic.types]),则它们是布局兼容的类.
Two standard-layout struct types are layout-compatible classes if their common initial sequence comprises all members and bit-fields of both classes ([basic.types]).
常见的初始序列为:
两种标准布局结构类型的公共初始序列是非静态数据成员和位字段的最长序列(按声明顺序),从每个结构中的第一个此类实体开始,以便相应的实体具有布局兼容类型,两个实体都不是位字段,或者两者都不是宽度相同的位字段.
The common initial sequence of two standard-layout struct types is the longest sequence of non-static data members and bit-fields in declaration order, starting with the first such entity in each of the structs, such that corresponding entities have layout-compatible types and either neither entity is a bit-field or both are bit-fields with the same width.
此处,One
(float x
)的第一个成员与Two
(float c[3]
)的第一个成员在布局上不兼容,因此公共初始序列为空.
Here, the first member of One
(float x
) is not layout-compatible with the first member of Two
(float c[3]
), so the common initial sequence is empty.
这篇关于相同类型布局的变量结构是否与包含该类型数组的结构兼容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!