gpt4 book ai didi

c++ - 为什么具有不同访问控制的成员的 union 不是标准布局?

转载 作者:太空宇宙 更新时间:2023-11-04 13:36:49 27 4
gpt4 key购买 nike

§9.0

7. A class S is a standard-layout class if it:

(7.3) has the same access control (Clause 11 ) for all non-static data members,

8 A standard-layout struct is a standard-layout class defined with the class-key struct or the class-key class . A standard-layout union is a standard-layout class defined with the class-key union .

AFAICT,由于 §9.2.13 而存在 §9.0.7.3

13 Nonstatic data members of a (non-union) class with the same access control (Clause 11 ) 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 (Clause 11 ). Implementation alignment requirements might cause two adjacent members not to be allocated immediately after each other; so might requirements for space for managing virtual functions ( 10.3 ) and virtual base classes ( 10.1 ).

但是,这似乎不适用于 union 体,因为 union 体的所有(非静态数据)成员都具有相同的地址。这是标准的缺陷吗?还是有一些令人信服的原因导致我没有看到?

最佳答案

在我看来,在 union 情况下允许多个访问说明符的好处并没有被引入的不一致和额外单词的成本所抵消。

关于c++ - 为什么具有不同访问控制的成员的 union 不是标准布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29280690/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com