Class members reordering

Deadly 提交于 2019-12-02 15:56:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!