http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58487

            Bug ID: 58487
           Summary: Missed return value optimization
           Product: gcc
           Version: 4.8.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jm at bourguet dot org

$ cat rva.cpp
#include <iostream>

class rva
{
public:
    rva() { std::cout << "Default construction\n"; }
    rva(rva const&) { std::cout << "Copy construction\n"; }
    rva& operator=(rva const&) { std::cout << "Assignation\n"; }
    ~rva() { std::cout << "Destruction\n"; }
};

rva f(int i) {
    if (i == 0) {
        rva result;
        return result;
    } else {
        return rva();
    }
}

int main()
{
    { std::cout << "f(0)\n"; f(0); }
    { std::cout << "\nf(1)\n"; f(1); }
    { std::cout << "\ng(0)\n"; g(0); }
    { std::cout << "\ng(1)\n"; g(1); }
    return 0;
}
$ g++-4.8.1 --version
g++-4.8.1 (GCC) 4.8.1
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ g++-4.8.1 rva.cpp  
$ ./a.out
f(0)
Default construction
Copy construction
Destruction
Destruction

f(1)
Default construction
Destruction


It seems to me that for f(0) the copy construction could be avoided.

Reply via email to