
on 04.05.2010 at 14:25 Giovanni Piero Deretta wrote :
Chad Nelson: [...] In #1, the compiler can eliminate all copies, if f is written in a RVO-friendly way. (It won't be able to in general if there's more than one return statement, or the return value is a ternary ?: expression.)
But in #2, there's going to be one allocation for the result of the unary op-. Even if operator- takes its parameter by value and directly flips its sign and returns it, I don't think that the compiler is allowed to allocate the return value and the parameter at the same address.
No, but swap is your friend:
X operator- (X x) { modify-in-place x;
X ret; // this should be cheap ret.swap(x); return ret; // NRVO should kick in }
i don't think it is a good idea to write such curious code in chase of a _possibility_ (there are no guarantees) of rvo -- Pavel P.S. if you notice a grammar mistake or weird phrasing in my message please point it out