考虑
struct base {};
struct child : base {};
众所周知,通过应用空基优化,sizeof(child)
可以为 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/56798409/