Boost logo

Boost :

Subject: Re: [boost] [GSoC][Phoenix3] Regarding mutability of arguments passed to phoenix expressions
From: Joel de Guzman (joel_at_[hidden])
Date: 2010-06-14 06:28:41


On 6/14/10 5:59 PM, Mathias Gaunard wrote:
> Eric Niebler wrote:
>> On 6/12/2010 5:34 PM, Mathias Gaunard wrote:
>>> For captures, I don't see how anyone would want to do anything but
>>> capture everything by reference.
>>>
>>
>> function<int(int)> plus_i( int i )
>> {
>> return _1 + i;
>> }
>>
>> If you capture i by reference, this code will compile but crash at
>> runtime. Have fun debugging it! :-)
>
> My main use for lambdas (be them C++0x or DSEL ones) is to pass them
> directly to a higher-order function or an iterator adapter; I never
> store them past their lifetime.
>
> I'm not sure lambdas in callbacks or signals scenarios is where they
> really shine.

There's a big chunk of use cases where you really want to capture
by value. Spirit is one. Consider this innocent looking piece of
code:

     int i = 10;
     rule<...> r = *int_[push_back(val_, _1 + i)];

By-val capture is still my preference. Otherwise, all Spirit code
will be dangerously be put in the verge of collapse and we'll end
up with tons of user support questions. By-val is still the way to
go, after all you always have ref(i).

Regards,

-- 
Joel de Guzman
http://www.boostpro.com
http://spirit.sf.net

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