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

标签 c++ language-lawyer

考虑

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/

相关文章:

c++ - 将方法放在 .h 与 .cpp 文件中

C++重绘窗口

c++ - 为 Windows Mobile 5 编译 Qt 应用程序

c++ - 我应该让我的 MutexLock 易变吗?

c++ - 为什么调用纯虚拟链接器错误而不是编译错误?

c++ - "Nested"带括号的类模板参数推导 : GCC vs. clang

c++ - C++ 中从非引用类型自动推导引用模板参数

c++ - 如果在 strchr() 中传递 NULL 怎么办

c++ - 如果返回类具有析构函数,则忽略 gnu::warn_unused_result 属性

javascript - 论点在词汇环境中的位置在哪里?