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-16 19:54:12


On 7/16/2012 4:23 AM, Nathan Ridge wrote:
>
>>> You did not evaluate it. Try adding a () at the end:
>>>
>>> phx::for_each(phx::ref(tokens),
>>> phx::lambda
>>> [
>>> phx::push_back(phx::ref(string_tokens), phx::arg_names::arg))
>>> ]
>>> )();
>>
>> Adding the evaluation worked. I get the expected results now.
>>
>> I now have the simplest pure phoenix method to parse a boost tokenizer
>> into a vector of strings. Is there a helper method that can be written
>> to automatically append the phx::lambda in this case. This would give
>> the following results.
>>
>> phx::for_each(phx::ref(tokens), phx::push_back(phx::ref(string_tokens),
>> phx::arg_names::arg)))();
>
> I don't see why you insist on using phoenix::for_each.
>
> The advantage of phoenix::for_each over boost::for_each is that phoenix::
> for_each is lazy (it allows you to delay some of the arguments to be
> passed in later). You are not making use of that feature here - you are
> providing all of the arguments up-front, and as a result, you have to
> call the resulting delayed function with no arguments.
>
> You should only use the lazy version of a function when you are taking
> advantage of the fact that it's lazy; in other cases it's simpler to use
> the regular, non-lazy version. In this case, you need push_back() to be
> lazy, but not for_each(), so you are complicating things unnecessarily
> by using the phoenix version of for_each.
>
> This works just fine, as I've mentioned before:
>
> boost::for_each(tokens, phx::push_back(phx::ref(string_tokens), phx::arg_names::arg)));
>
> Note that it is not inconsistent that you are using boost::for_each for
> one algorithm, and phoenix::push_back for another. The phoenix:: algorithms
> are exactly like the boost:: algorithms except that they add laziness.
> Since adding laziness complicates the interface of the function, it follows
> that you should only use the lazy version in instances where you actually
> need the laziness.

Exactly my thoughts. I second that. There's no use in having a lazy
function that you will evaluate immediately anyway.

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