gpt4 book ai didi

c++ - 如果类包含基类的成员,编译器能否利用空基优化?

转载 作者:太空宇宙 更新时间:2023-11-04 12:35:03 25 4
gpt4 key购买 nike

考虑

struct base {};
struct child : base {};

众所周知,通过应用空基优化sizeof(child) 可以为 1。

但是,现在考虑一下

struct base {};
struct child : base {base b;};

编译器现在可以应用空基优化,还是 sizeof(child) 必须至少为 2?

引用:http://en.cppreference.com/w/cpp/language/ebo

最佳答案

不,它不能。来自同一引用:

Empty base optimization is prohibited if one of the empty base classes is also the type or the base of the type of the first non-static data member

因此 sizeof(child) >= 2

关于c++ - 如果类包含基类的成员,编译器能否利用空基优化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56798409/

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