c++ - 为什么空基类优化(EBO)在MSVC中不起作用?

标签 c++ visual-c++ compiler-optimization empty-class

为什么在Visual C++中没有完全应用空的基类优化(EBO)?

如果我有很多基类,有什么办法可以帮助编译器进行优化?

#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)对仅初始空基类进行优化。
2006年在Microsoft Connect上报告了此问题:Empty Base Optimization Not Working Properly.目前,旧错误在Microsoft Connect上不可见。有人告诉我这是一个临时性问题,尽管我不知道何时会解决。同时,以下是对Jonathan Caves(该人是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.

Thanks for reporting the issue.

关于c++ - 为什么空基类优化(EBO)在MSVC中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64942242/

相关文章:

c++ - 转置数组

c++ - 如何在 C++ 中创建具有多态性的数组?

java - 将 Java jar 文件转换为 cpp

C++ 存储到文件

c++ - vector<>::maxsize() 的最大大小是多少?

c++ - 从其他专门函数调用专门函数

c++ - 在 C++ 中捕获 lambda 的全局引用是否会抑制别名优化?

c++ - 为什么从 vector 打印图像像素值会返回无意义?

compiler-construction - 是否有包含 LLVM 优化列表的网页?

c++ - GCC w/inline assembly & -Ofast 为内存操作数生成额外代码