Not.
But you can write the equivalent, albeit completely unreadable, code:
BigObj f() { BigObj x(g()); x.someMethod(); return x; }
translates (with a copy of elision) to:
void f(BigObj* obj) { new(obj) BigObj(g()); obj->someMethod(); } //... char z[sizeof(BigObj)]; f((BigObj*)&z[0]); //... ((BigObj*)&z[0])->~BigObj();
But seriously, just write your code so that the compiler can strip the copy. That is, it returns only one object without branching:
BigObj f() { BigObj x, y; // use x and y if(condition) return x; else return y; // cannot be elided } BigObj f() { if(condition) { BigObj x; return x; } else { BigObj y; return y; } // can be elided }
source share