[LC++]Using the "return value optimization"
Davide Bolcioni
9odb6x3rfr001 at sneakemail.com
Tue Aug 14 06:34:04 UTC 2001
Mark Phillips mark at austrics.com.au [mltuxcpp/linux-cpp list] wrote:
> Vincent Penquerc'h wrote:
> What I would really like it to do, is for return value
> optimization to take the following code:
>
> int a=doSomething(parameter);
>
> and convert it into:
>
> int a;
> doSomething(a, parameter);
>
> It would seem like this is not possible.
On the contrary, it is very similar to how return value optimization is
supposed to work, i.e. turn
class T { ... omitted ... }
...
T f() { T t; ... return t; }
...
T x = f();
into something very similar to your code. Instead of copy-constructing
the return value of f() from t to an unnamed temporary and the
copy-constructing x from this temporary, it is allowed to copy-construct
x from t directly. If you have
T g() { ... return <expression convertible to a T>; }
it is allowed to evaluate the expression and construct x directly. So
return value optimization buys you something only if you give the
compiler temporaries to skip, so to say; this is in theory, since
todays compilers may have difficulties ensuring correct temporary
handling and good optimization and the same time; on the other hand,
compilers are generally *not* allowed to skip construction of a named
variable, except under the "as if" rule, so YMMV. Personally, I code
paying little heed to return value optimization; if I need to return
values because that's how my solution is enginereed, I return values.
Should profiling show that the bottleneck is in this choice, I'll
reconsider it then (and use proxy classes).
Davide Bolcioni
--
There is no place like /home.
More information about the tuxCPProgramming
mailing list