dimanche 30 août 2015

Why can't std::function be optimised away?

After reading this question I tried looking at an even simpler case to see if std::function could be completely optimised away:

#include <functional>

int main()
{   
    auto f = []() { return 100; }; // a
    // std::function<int()> f = []() { return 100; }; // b
    return f();
}

the assembly with clang++ -std=c++11 -O3 (clang 3.6) for a and b respectively is:

main: #a
    movl    $100, %eax
    retq

main: #b
    pushq   %rax
    movl    $1, %edi
    callq   operator new(unsigned long)
    movl    $1, %esi
    movq    %rax, %rdi
    callq   operator delete(void*, unsigned long)
    movl    $100, %eax
    popq    %rdx
    retq

operator delete(void*, unsigned long):
    jmp operator delete(void*)

Why can't the construction of the std::function object be optimised away?

Aucun commentaire:

Enregistrer un commentaire