考虑
struct base {};
struct child : base {};
众所周知,sizeof(child)
可以通过 empty base 优化为 1。
现在,考虑
struct base {};
struct child : base {base b;};
编译器现在可以应用空基优化,还是必须 sizeof(child)
至少为 2?
最佳答案
不,它不能。来自同一引用:
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/44909407/