GCC和反虚拟化内联
GCC and devirtualization inlining
为什么,在下面的代码中,编译器,知道确切的类型,不能内联虚函数?
https://goo.gl/AgvefXusing namespace std;
struct Base{
void fire(){
show();
}
virtual void show() =0;/*{
cout << "base";
}*/
};
struct A : Base{
inline __attribute__((__always_inline__)) void show() {
cout << "a";
}
};
int main() {
A a;
for (int i=0;i<1000;i++){
a.fire();
}
//a.fire();
return 0;
}
我能在没有CRTP的情况下做些什么吗?
我不擅长阅读汇编代码,但我看到你的例子,当在main:
- 1000被加载到寄存器
- 1被减去,
- a的地址被加载,
- 在 之前调用输出操作符
- 跳回循环的开始。
我没有看到呼叫fire
或show
。它保留了显示的实现,但没有使用它。这是正确的,因为它有外部连杆。
如果您将所有内容放在匿名命名空间中,则可以省略实现。
你能把你对汇编代码的推理放在你的问题中吗?作为参考,下面是从提供链接生成的汇编代码:
.LC0:
.string "a"
A::show():
movl $1, %edx
movl $.LC0, %esi
movl std::cout, %edi
jmp std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long)
main:
pushq %rbx
movl $1000, %ebx
.L3:
movl $1, %edx
movl $.LC0, %esi
movl std::cout, %edi
call std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long)
subl $1, %ebx
jne .L3
xorl %eax, %eax
popq %rbx
ret
subq $8, %rsp
movl std::__ioinit, %edi
call std::ios_base::Init::Init()
movl $__dso_handle, %edx
movl std::__ioinit, %esi
movl std::ios_base::Init::~Init(), %edi
addq $8, %rsp
jmp __cxa_atexit
相关文章:
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 奇怪的结构&GCC&clang(void*返回类型)
- GCC本机矩阵运算库
- PowerPC ppc64le上的Gcc Woverloaded虚拟错误
- gcc和c++17的过载解析失败
- 数据成员SFINAE的C++17测试:gcc vs clang
- GCC对可能有效的代码抛出init list生存期警告
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 使用 GCC 卸载的 OpenMP 卸载失败,并出现"Ptx assembly aborted due to errors"
- 为什么与常规GCC不同,即使有"学究性错误",MinGW-GCC也能容忍丢失的返回类型
- 使用gcc从静态链接的文件中查找可选符号
- 普通环路未使用gcc 4.8.5自动矢量化
- 有了gcc,是否可以链接库,但前提是它存在
- 在clang++预处理器中确定gcc工具链版本
- 为什么 gcc 编译这个而 msvc 没有
- 为什么lambda在clang上崩溃而不是在gcc上崩溃
- 我可以检测和更改 gcc/g++ 中结构的当前数据对齐设置吗?
- 为什么 gcc 不能去虚拟化这个函数调用?
- GCC对简单类进行去虚拟化
- GCC和反虚拟化内联