http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57971
Bug ID: 57971 Summary: Improve copy elision when returning structs by value Product: gcc Version: 4.8.1 Status: UNCONFIRMED Severity: enhancement Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: scovich at gmail dot com Hi all, In the testcase below, bar() and baz() perform copy elision as expected, but blah() does not, in spite of its being functionally identical to baz(): #include <cstdio> struct foo { foo() { printf("make\n"); } foo(foo const &) { printf("copy\n"); } void frob() { printf("frob\n"); } }; foo bar(bool) { foo f; f.frob(); return f; } foo baz(bool mknew) { if (mknew) return foo(); return bar(mknew); } foo blah(bool mknew) { if (mknew) return foo(); foo f = bar(mknew); return f; } int main() { printf("*** bar ***\n"); bar(false); printf("*** baz ***\n"); baz(false); printf("*** blah ***\n"); blah(false); } Output is: $ g++ -Wall bug.cpp && ./a.out *** bar *** make frob *** baz *** make frob *** blah *** make frob copy I assume that bar() and baz() exploit the named and unnamed return value optimizations, respectively, but blah() is missed because it needs both optimizations together.