For return by value optimization, instead of calling the copy constructor or the assignment operator, gcc decides to optimize and push the address of the lhs of the expression into the rhs.
Foo createFoo() { Foo f2; return f2; } int main(){ Foo f1 = createFoo(); return 0; } OPTIMIZED TO void createFoo( Foo& f2 ) {...} int main() { Foo f1; createFoo(f1); return 0; } This is a problem when you want to explicitly define the copy constructor or assignment operator to do something other than deep copying. -- Summary: invalid return by value optimization Product: gcc Version: 3.4.6 Status: UNCONFIRMED Severity: blocker Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: khoaduynguyen at gmail dot com http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37487