Boost logo

Boost :

Subject: Re: [boost] [proto] RValue reference support?
From: Eric Niebler (eric_at_[hidden])
Date: 2011-08-11 14:11:18


On 8/11/2011 10:50 AM, John Maddock wrote:
>>> 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

This part I don't understand, and it's probably because I don't know the
particulars of the bigint domain. But I know if these things were
vectors, it would go something like this:

* walk the expression x*y + w*z (at runtime) and calculate the size of
the resulting vector
* allocate space for the result vector
* walk the expression x*y + w*z and evaluate it (also at runtime),
filling in the result vector directly (no temporaries).
* swap the result vector with a's

I'm ignorant of how bigint arithmetic works, but I wonder if a similar
scheme might work here.

> 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 :-)

OK, this much I understand. Thanks.

-- 
Eric Niebler
BoostPro Computing
http://www.boostpro.com

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