Why is gcc lacking optimization of heap allocation in local contexts?

Consider the following simplified example:

#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; } 

GCC 5.2-O3 compiles it into :

 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 in :

 test_lack(): mov eax, 10 ret 

Why is Clang able to optimize this code while GCC fails?

Are there any compiler flags or code improvements that can allow GCC to perform more aggressive optimizations for this code?


Just a short addition to the answer:

GCC 5.2 does not implement the standard N3364 proposal , which means that it cannot optimize this call for a new operator, as Klang does.

+6
source share

All Articles