问题
recently I've read about compiler ability to reorded members in a class. From C++ 11 standard:
§ 9.2.13
[...] The order of allocation of non-static data members with different access control is unspecified.
I would like to know how does it look like in practice. Do the major compilers (I'm interested in g++, clang and msvc) reorder class members in some situtations?
If no, is there anything else that could happen which would result in different object layout on different compilers (or when using different compiler flags)? Assume that no virtual methods are used and so no vtable is created.
回答1:
The question is, in fact, moot. The only standard-compliant way to ensure the layout of the class follows your expectation is to make sure the class is a Standard Layout Type - and that would guarantee the same layout on every conformant compiler.
One of the requirements for such a type is that all members have the same access control.
来源:https://stackoverflow.com/questions/52766536/class-members-reordering