考虑以下简化示例:
#include <utility>
#include <memory>
int test_lack()
{
auto lam = []
{
return 10;
};
// move lam to the heap
void* ptr = new decltype(lam)(std::move(lam));
// retrieve the result of lam
int res = (*static_cast<decltype(lam)*>(ptr))();
if (ptr) // important
delete static_cast<decltype(lam)*>(ptr);
return res;
}
test_lack():
sub rsp, 8
mov edi, 1
call operator new(unsigned long)
mov esi, 1
mov rdi, rax
call operator delete(void*, unsigned long)
mov eax, 10
add rsp, 8
ret
Clang 3.7 -O3 optimizes this into:
test_lack():
mov eax, 10
ret
为什么Clang能够优化给定代码而GCC失败?
是否有任何编译器标志或代码改进可以允许GCC对给定代码执行更积极的优化?
只是答案的简短补充:
GCC 5.2 doesn't implement标准提案N3364,这意味着它无法像Clang那样优化对运营商的新调用。