常见的初始序列和比对 [英] Common initial sequence and alignment
问题描述
在思考这个问题的反例时,我想出了:
While thinking of a counter-example for this question, I came up with:
struct A
{
alignas(2) char byte;
};
但是如果这是合法的和标准布局,它是布局兼容的 struct B
?
But if that's legal and standard-layout, is it layout-compatible to this struct B
?
struct B
{
char byte;
};
此外,如果我们有
struct A
{
alignas(2) char x;
alignas(4) char y;
};
// possible alignment, - is padding
// 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
// x - - - y - - - x - - - y - - -
struct B
{
char x;
char y;
}; // no padding required
union U
{
A a;
B b;
} u;
A
和 B
?如果是,是否包括 A :: y
& B :: y
?
Is there a common initial sequence for A
and B
? If so, does it include A::y
& B::y
? I.e., may we write the following w/o invoking UB?
u.a.y = 42;
std::cout << u.b.y;
(对于C ++ 1y /fixed C ++ 11的答案也很受欢迎)
(answers for C++1y / "fixed C++11" also welcome)
-
请参阅[basic.align]对齐方式和[dcl.align] -
See [basic.align] for alignment and [dcl.align] for the alignment-specifier.
[basic.types] / 11说基本类型如果两个类型 T1
和 T2
是相同类型,则 T1
和 T2
是布局兼容类型。 (一个根本的问题是 A :: byte
和 B :: byte
是否具有布局兼容类型)
[basic.types]/11 says for fundamental types "If two types T1
and T2
are the same type, then T1
and T2
are layout-compatible types." (an underlying question is whether A::byte
and B::byte
have layout-compatible types)
[class.mem] / 16如果两个标准布局结构类型具有相同数量的非静态数据成员和对应的非静态
[class.mem]/16 "Two standard-layout struct 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."
[class.mem] / 18两个标准布局结构共享一个共同的初始
序列,如果对应成员具有布局兼容类型并且既不是位字段也不是
都是具有对于一个或多个初始成员的序列的相同宽度的位字段。
[class.mem]/18 "Two standard-layout structs share a common initial sequence if corresponding members have layout-compatible types and either neither member is a bit-field or both are bit-fields with the same width for a sequence of one or more initial members."
[class.mem] / 18如果标准布局联合包含两个或多个共享初始序列的标准布局结构,
,标准布局联合对象当前包含这些标准布局结构之一,允许
检查其中任何一个的公共初始部分。
[class.mem]/18 "If a standard-layout union contains two or more standard-layout structs that share a common initial sequence, and if the standard-layout union object currently contains one of these standard-layout structs, it is permitted to inspect the common initial part of any of them."
当然,在语言 - 律师一级,另一个问题是,这意味着对公共初始序列的检查是允许的。我猜其他段落可能使上述 ubx
未定义的行为(从未初始化的对象读取)。
Of course, on a language-lawyer level, another question is what it means that the inspection of the common initial sequence is "permitted". I guess some other paragraph might make the above u.b.x
undefined behaviour (reading from an uninitialized object).
推荐答案
它看起来像一个洞在标准。负责任的事情是提交缺陷报告。
It looks like a hole in the standard. The responsible thing would be to file a defect report.
有几件事:
- 一个问题。在
char
后添加short
也会使char
到2字节边界,而不更改公共子序列。 -
alignas
不是C ++ - 它同时添加到 C11 。由于标准布局属性是跨语言兼容性设施,因此可能最好是要求相应的对齐方式说明符匹配,而不是使用非静态成员对齐说明符来取消类。 - 如果成员对齐指定符属于成员的类型,则没有问题。其他问题可能由于缺乏对类型的调整而导致,例如可能需要对ABI处理它的函数参数
ret fn(alignas(4)char)
正确,但语言可能不提供此类调整。
- Your first example doesn't really demonstrate a problem. Adding a
short
after thechar
would also have the effect of aligning thechar
to a 2-byte boundary, without changing the common subsequence. alignas
is not C++-only; it was added simultaneously to C11. Since the standard-layout property is a cross-language compatibility facility, it is probably preferable to require corresponding alignment specifiers to match than to disqualify a class with a nonstatic member alignment-specifier.- There would be no problem if the member alignment specifiers appertained to the types of the members. Other problems may result from the lack of adjustment to types, for example a function parameter
ret fn( alignas(4) char )
may need to be mangled for the ABI to process it correctly, but the language might not provide for such adjustment.
这篇关于常见的初始序列和比对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!