Boost logo

Boost Users :

Subject: Re: [Boost-users] [Phoenix] for_each calling push_back for stl containers
From: Joel de Guzman (joel_at_[hidden])
Date: 2012-07-09 19:14:43


On 7/10/2012 6:21 AM, Jeffrey Lee Hellrung, Jr. wrote:
> On Mon, Jul 9, 2012 at 3:07 PM, Ryan <boost_at_[hidden] <mailto:boost_at_[hidden]>> wrote:
>
> On Mon, Jul 9, 2012 at 2:57 PM, Nathan Ridge <zeratul976_at_[hidden]
> <mailto: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.
>
[...]

Yes, it's a scoping issue. Please read up:

   http://tinyurl.com/brufnnw

In essence, anytime you need a higher-order function passed in as an
argument (e.g. for_each's function argument), you'll need an explicit
lambda. The for_each example is actually mentioned in the docs:

   http://tinyurl.com/cjy82f8

It's really not that bad as you characterized it. Explicit is always
better than implicit. For example:

   for_each(arg1, lambda[cout << arg1])

This other example (again from the docs) *cannot* be done with an
implicit (unscoped) syntax. Nor can this be done with the mechanisms
in MPL or Boost.Lambda, for example:

   for_each(arg1,
     lambda(_a = arg2)
     [
         push_back(arg1, _a)
     ]
   )

Only the de-Bruijn syntax can do this as well. Please read up before
you bash the thing ;-)

Regards,

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

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