Why is Visual C++ not performing return-value optimization on the most trivial code?

前端 未结 1 1328
一生所求
一生所求 2021-02-07 14:18

Does Visual C++ not perform return-value optimization?

#include 
struct Foo { ~Foo() { printf(\"Destructing...\\n\"); } };
Foo foo() { return Foo()         


        
相关标签:
1条回答
  • 2021-02-07 15:18

    When I test with this:

    #include <iostream>
    struct Foo { 
        Foo(Foo const &r) { std::cout << "Copying...\n"; }
        ~Foo() { std::cout << "Destructing...\n"; }
        Foo() {}
    };
    
    Foo foo() { return Foo(); }
    
    int main() { Foo f = foo(); }
    

    ...the output I get is:

    Destructing...
    

    No invocation of the copy constructor, and only one of the destructor.

    0 讨论(0)
提交回复
热议问题