Boost logo

Boost :

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


On 8/11/2011 11:25 AM, Felipe Magno de Almeida wrote:
>> On 8/11/2011 10:50 AM, John Maddock wrote:
>>> 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
>
> If proto supported rvalue references then a could reuse foo()'s temporary
> resources and maybe avoid dynamic allocation for a.

I'm not actually sure that's true in the general case. foo()'s temporary
is holding a value that is used in the computation. You can't reuse the
memory until you are done with the value, and in the general case,
that's not until you are done evaluating the expression. Consider how it
works for vectors. You want to evaluate the expression as:

tmp1 = foo();
tmp2 = vector of right size
for (int i in 0 to tmp2.size()-1)
  tmp2[i] = x[i]*y[i]+tmp1[i]*z[i];
swap(a, tmp2);

You're not ready to throw out tmp1 until after the for loop, so you need
tmp2.

OK, in this isolated case, you actually /could/ use tmp1 as your scratch
space:

  tmp1[i] = x[i]*y[i]+tmp1[i]*z[i];

But in the general case, you can't know that it's ok to stomp a value
(tmp1) that's still in use like this. And for bigint, I'm guessing
that's not the case.

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