班级成员重新排序 [英] Class members reordering

查看:52
本文介绍了班级成员重新排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我读到了有关编译器对类中的成员进行排序的功能。从C ++ 11标准开始:

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.

我想知道它的外观喜欢在实践中。主要编译器(我对g ++,clang和msvc感兴趣)是否在某些情况下对类成员进行重新排序?

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?

如果否,是否还会发生其他事情,从而导致不同的编译器上的对象布局不同(或使用不同的编译器标志时)?
假定未使用任何虚拟方法,因此未创建任何vtable。

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.

推荐答案

实际上,问题是,模拟。确保类布局符合您的期望的唯一符合标准的方法是,确保类为标准布局类型-可以保证每个合格编译器的布局相同。

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.

这篇关于班级成员重新排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆