Layout restriction for custom layout class

Is it possible for the compiler to reorder data in a custom layout class? For example, is it allowed to change

struct { char x; private: short y; public: char z; }; 

to

 struct { private: short y; public: char x; char z; }; 
+6
source share
1 answer

EDIT: I read the quote incorrectly, the compiler is allowed to do this in 9.2/14 :

Non-static data members of a (non-unitary) class with the same access (Section 11) are allocated so that later members have higher addresses inside the class object. The distribution order of non-static data members with different access control is not defined (11). Implementation alignment requirements can cause two neighboring members not to stand out immediately after each other; so space requirements for managing virtual functions (10.3) and virtual base classes (10.1)

What he cannot do is reorder the attributes with the same access specifier.

This says that I don’t know of any implementation that allows itself to change the order of attributes even by access specifiers.

+8
source

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


All Articles