Boost logo

Boost :

Subject: Re: [boost] [proto] RValue reference support?
From: John Maddock (boost.regex_at_[hidden])
Date: 2011-08-11 13:50:34


>> But I'm figuring that the occurrence of temporaries on the RHS of an
>> expression, while not out of the question, is rare enough not to be too
>> concerned about.
>
> You can use Proto to eliminate the need for temporaries without rvalue
> reference support. What you describe above is a textbook application of
> expression templates. If it will save you 1 dynamic allocation every now
> and then, it's probably worth considering.
>
> Can you explain again why rvalue references are important for you? I'm
> not following.

OK, lets say we have an expression:

a = x * y + w * z;

The order of evaluation might be:

*assign x to a.
* multiply a by y
* create one temporary as a copy of w
* multiple temp by z
* add temp to a

So we need one temporary to evaluate the expression.

Now imagine that one of the variables isn't a variable at all, but a
temporary, lets say the result of a (non-protoized) function call:

a = x * y + foo() * z

Now we don't need to create a temporary for foo() * z since *we have one
already*. So if one of the terminals is an rvalue reference, we know that
we can reuse that value as a temporary and trash it's value with impunity -
as long as we do so after it's value has been used of course :-)

HTH, John.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk