|
Boost : |
Subject: Re: [boost] [GSoC][Phoenix3] Regarding mutability of arguments passed to phoenix expressions
From: Eric Niebler (eric_at_[hidden])
Date: 2010-06-11 12:30:56
On 6/11/2010 12:17 PM, Mathias Gaunard wrote:
> Eric Niebler wrote:
>
>> That gives us an opportunity to add a new feature to Phoenix: default
>> capture modes. I could see something like:
>>
>> ref[ phoenix-lambda-expression ]
>>
>> makes default capture by-ref, and
>>
>> val[ phoenix-lambda-expression ]
>>
>> makes it by-val. Under the hood, Phoenix would actually be storing every
>> captured variable both by value and by reference.
>
> What if the variable is not copyable?
If that can be detected at compile-time, we can avoid the copy.
> Copying is an expensive operation as well, why do it when you don't need
> to?
There's the ref(x) syntax for avoiding the copy completely. The reason
we have to take a copy when building the expression is because if we
want by-value capture to be the default then these two should be equivalent:
int x = 0;
auto f0 = val[ x += _1 ]; // capture x by val
auto f1 = x += _1; // capture x by val
f0(1);
f2(1);
// x is unchanged here
In the second case, if we simply capture by reference, we are never
given a chance to change the capture to by-value before we hand the
lambda over for evaluation elsewhere. Instead, we capture both by value
and by reference, and the default is to use the stored copy.
Despite the contrived-ness of this example, which makes by-value copy
seem like a bad default, I still think it's the way to go. BLL captures
by value (but interestingly makes an exception for the case above).
-- 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