C++20 是否取消了 class 成员按升序排列的要求?

Does C++20 remove the requirement for class members to be in ascending order?

在 C++17 中有规范文本 [class.mem]/17:

Non-static data members of a (non-union) class with the same access control (Clause 14) are allocated so that later members have higher addresses within a class object. The order of allocation of non-static data members with different access control is unspecified.

还有[class.mem]/24:

If a standard-layout class object has any non-static data members, its address is the same as the address of its first non-static data member

这里有两个例子:

struct A { int x, y, z; } a;
struct F { public: int p; private: int q; public: int r; } f;

根据上述标准文本,C++17 保证 &a.x < &a.y&a.y < &a.z&f.p < &f.r(但不保证 &f.p < &f.q,因为 F 不是标准布局,因此 class.mem/24 不适用)。


然而,在 C++20 最终工作草案 N4860 中,根据 CWG 2404 进行了更改。 [class.mem]/17 已变成注释。但是,注释在 ISO 标准中是非规范的(意味着编译器供应商可以忽略它们)。我找不到任何其他可能适用的文本。

我的问题是: C++20 是否仍在某处指定(规范地)保证 &a.y < &a.z and/or &f.p < &f.r ?或者编译器现在是否有权在除标准布局 class 的第一个子对象之外的所有情况下对 class 成员重新排序?

假设 N4860 和已发布的标准之间没有进一步的变化,我想。

这仍然由 [expr.rel]/(4.2) 保证,描述了指针上内置 <<=>>= 表达式的行为值。

If two pointers point to different non-static data members of the same object, or to subobjects of such members, recursively, the pointer to the later declared member is required to compare greater provided the two members have the same access control ([class.access]), neither member is a subobject of zero size, and their class is not a union.