struct members memory layout

前端 未结 6 1999
既然无缘
既然无缘 2021-01-04 14:00

If I have a struct like this:

struct S {
    ANY_TYPE a;
    ANY_TYPE b;
    ANY_TYPE c;
} s;

Can I safely assume that the following assump

6条回答
  •  隐瞒了意图╮
    2021-01-04 15:00

    This changes in C++20 (at least the current draft): if you annotate a, b, and c with [[no_unique_address]] and they happen to be empty structures it's possible that they will all have the same address.

    In fact it's more complex - if any two are empty and annotated with that then all 3 can share an address.

提交回复
热议问题