Why does the C ++ standard specifically provide freedom of action regarding the memory layout of class data members with different access specifiers?

The C ++ 11 standard provides for in-memory ordering for non-static members of class data, but then specifically throws an exception for members with different access specifiers.

Why?

ยง 9.2.13

Non-stationary data members (non-union) of a class with the same access control (section 11) are allocated in such a way that later members have higher addresses in the class object. The distribution order of non-static data elements with different access control is not specified (clause 11). Requirements for alignment of the implementation can lead to the fact that immediately two neighboring elements will not be distributed immediately after each other; therefore, space may be required to manage virtual functions (10.3) and virtual base classes (10.1).

This part of the standard appeared on stackoverflow before, but I don't think it has ever been explained.

+6
source share
1 answer

N2062 is the first article in C ++ devoted to changes in the definition of POD C ++ 98/03. This was written as a means to solve the main problem 568 , which concerns POD and type layouts. It represents the beginning of a design that leads to standard C ++ 11 layouts and trivial copy definitions.

And yet, the N2062 does not even consider locating items with different access controls. It does not even provide an excuse for why this limitation exists. Also the final version of this proposal , which actually gives trivially-copied and standard layouts. All versions of these offers restrict access restriction as a fait accompli, not something that could be changed.

All this suggests that the author of the proposal knew at least one / ABI compiler, which reordered members based on access controls.

+2
source

Source: https://habr.com/ru/post/1013659/


All Articles