Boost logo

Boost :

Subject: Re: [boost] [GSoC][Phoenix3] Regarding mutability of arguments passed to phoenix expressions
From: Giovanni Piero Deretta (gpderetta_at_[hidden])
Date: 2010-06-11 13:24:07


On Fri, Jun 11, 2010 at 6:03 PM, Eric Niebler <eric_at_[hidden]> wrote:
> On 6/11/2010 12:36 PM, Eric Niebler wrote:
>> On 6/11/2010 12:27 PM, David Abrahams wrote:
>>> At Fri, 11 Jun 2010 12:12:27 -0400, Eric Niebler wrote
>>
>>>> makes it by-val. Under the hood, Phoenix would actually be storing every
>>>> captured variable both by value and by reference.
>>>
>>> !! Sounds expensive.
>>
>> The plan has always been to make capture-by-value the default. Saving
>> off a reference as well is free.
>
> Well, there's a hitch.
>
>  ref[ bigobj += _1 ]
>
> Users might reasonably expect this to not make a copy of bigobj, and the
> way to avoid it is not obvious:
>
>  ref[ ref(bigobj) += _1 ]
>
> Clearly the user has already stated their intention to capture bigobj by
> reference and shouldn't have to say it again.
>
> I think default capture modes may be unworkable if we want the default
> mode to be by-value.
>

unless you forbid 'naked' lambdas:

 int x = 0;
 auto f0 = lambda [ x += _1 ]; // capture x by val
 auto f1 = lambda_r [ x += _1 ]; // capture x by ref
 auto f2 = x += _1 ; // just a proto expression tree, not a lambda

 f0(1); //ok
 f2(1); //ok
 f3(1); // no operator()

The lambda syntax becomes more heavy weight, but lambdas do stand up
more in code. You would have to protect nested lambdas anyway ...

-- 
gpd

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