Are these 2 structs layout-compatible?
struct One {
float x, y, z;
};
struct Two {
float c[3];
};
Both contains 3 floats, so in a way, this description can be considered true (from N3797):
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 has a different text:
The common initial sequence of two standard-layout struct (Clause 12)
types is the longest sequence of non-static data members and bit-?elds
in declaration order, starting with the ?rst such entity in each of
the structs, such that corresponding entities have layout-compatible
types and either neither entity is a bit-?eld or both are bit-?elds
with the same width.
Two standard-layout struct (Clause 12) types are layout-compatible
classes if their common initial sequence comprises all members and
bit-?elds of both classes (6.9).
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).
Bonus question: is it guaranteed, that sizeof(One)==sizeof(Two)
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…