为什么空基类优化 (EBO) 在 MSVC 中不起作用?
为什么在 Visual C++ 中没有完全应用空基类优化 (EBO)?
Why is the empty base class optimization (EBO) not being fully applied in Visual C++?
如果我有很多基类,有什么办法可以帮助编译器做这个优化吗?
If I have a lot of base classes, is there any way for me to help the compiler make this optimization?
#include <iostream>
struct T1 { };
struct T2 { };
struct T3 { };
struct T4 { };
struct T5 { };
struct T6 { };
struct Test : T1, T2, T3, T4, T5, T6 { };
int main() { std::cout << sizeof(Test); } // Prints 5
推荐答案
这是 Visual C++ 编译器中的一个长期存在的错误.当一个类派生自多个空基类时,只会使用空基优化(EBO)对初始空基类进行优化.
This is a longstanding bug in the Visual C++ compiler. When a class derives from multiple empty base classes, only the initial empty base class will be optimized using the empty base optimization (EBO).
此问题已于 2006 年在 Microsoft Connect 上报告:空基优化无法正常工作. 目前,Microsoft Connect 上看不到旧错误.有人告诉我这是一个临时问题,但我不知道什么时候会得到解决.同时,以下是 Jonathan Caves 对错误的回应,他是 Visual C++ 编译器团队的开发人员之一:
This issue was reported on Microsoft Connect in 2006: Empty Base Optimization Not Working Properly. At the moment, old bugs are not visible on Microsoft Connect. I am told that this is a temporary issue, though I do not know when it will be resolved. In the meantime, the following is the response to the bug from Jonathan Caves, who is one of the developers on the Visual C++ compiler team:
嗨:不幸的是,尽管这是 Visual C++ 对象模型中的一个错误,但我们目前无法修复它,因为修复它可能会破坏许多现有程序,因为对象的大小会发生变化.希望将来我们可以解决这个问题,但不能在产品的下一个版本中解决.
Hi: unfortunately even though this is a bug in the Visual C++ object model we are unable to fix it at this time given that fixing it would potentially break a lot of existing programs as the sizes of objects would change. Hopefully in the future we may be able to address this issue but not for the next release of the product.
感谢您报告问题.
相关文章