简体   繁体   中英

Why are unions which have members with differing access control not standard-layout?

§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.0.7.3 exists because of §9.2.13

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 ).

However, this does not seem to apply to unions, as all (non-static data) members of a union have the same address. Is this a defect in the standard? Or is there some compelling reason why that I'm not seeing?

在我看来,允许在union案例中使用多个访问说明符的好处并没有因不一致的代价和引入的其他单词而被抵消。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM