Boost logo

Boost Users :

Subject: Re: [Boost-users] [Phoenix] for_each calling push_back for stl containers
From: Jeffrey Lee Hellrung, Jr. (jeffrey.hellrung_at_[hidden])
Date: 2012-07-09 18:21:32


On Mon, Jul 9, 2012 at 3:07 PM, Ryan <boost_at_[hidden]> wrote:

> On Mon, Jul 9, 2012 at 2:57 PM, Nathan Ridge <zeratul976_at_[hidden]>wrote:
>
>>
>> Actually that's not true. Without using Phoenix at all, just using
>> Boost.Range,
>> this is simply:
>>
>> boost::push_back(string_tokens, tokens);
>>
>
> Interesting, I'll have to take a look at boost range.
>
>
> As for your question about why Phoenix for_each requires a lambda, I'm
>> afraid I
>> don't understand Phoenix well enough to answer that. Perhaps Thomas can
>> help us.
>
>
> Lets hope he weighs in then. I would really like to understand this
> sticking point.
>

I'm pretty sure it's a scoping issue. Your inner and outer arg1's should
actually bind at different times to different objects, and the only way to
signify that is to use scope the inner arg1 in a phx::lambda construct.

I don't remember exactly what you were trying to do, but I believe it was
something like

phx::for_each(arg1, phx::push_back(dest, arg1))(srce)

which evaluates to approximately

for_each(srce, F)

where F(x) evaluates dest.push_back(srce) (regardless of x) every time,
since the arg1 in phx::push_back(dest, arg1) is immediately bound to srce.
phx::lambda effectively delays this binding to give you what you want:

phx::for_each(arg1, phx::lambda(phx::push_back(dest, arg1)))(srce)
  => for_each(srce, phx::push_back(dest, arg1))

Note that my syntax is way off here, but this scoping issue is present not
just in Boost.Phoenix but also Boost.MPL and probably Boost.Bind/Std.Bind.
Also consider reading up on de Bruijn indices on wikipedia; at some point
in the past on this list, someone proposed using de Bruijn placeholders to
avoid phx::lambda (and Phoenix local variables) acrobatics.

HTH,

- Jeff



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net