gpt4 book ai didi

c++ - 非标准布局类的布局限制

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:24:34 25 4
gpt4 key购买 nike

编译器是否可以自由地对非标准布局类中的数据进行重新排序?比如是否允许改变

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

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

最佳答案

编辑:我最初记错了引用,允许编译器按照 9.2/14 执行此操作:

Nonstatic data members of a (non-union) class with the same access control (Clause 11) are allocated so that later members have higher addresses within a class object. The order of allocation of non-static data members with different access control is unspecified (11). Implementation alignment requirements might cause two adjacent members not to be allocated immediately after each other; so might requirements for space for managing virtual functions (10.3) and virtual base classes (10.1)

它不能做的是用相同的访问说明符重新排序属性。

也就是说,我不知道有任何实现可以随意对属性重新排序,甚至跨访问说明符。

关于c++ - 非标准布局类的布局限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17372235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com