Boost logo

Boost :

Subject: Re: [boost] phoenix::bind
From: Giovanni Piero Deretta (gpderetta_at_[hidden])
Date: 2008-10-02 09:49:32


On Thu, Oct 2, 2008 at 3:31 PM, Joel de Guzman
<joel_at_[hidden]> wrote:
> Giovanni Piero Deretta wrote:
>>
>> On Thu, Oct 2, 2008 at 1:10 PM, Joel de Guzman
>
>> OK, I'm really really missing something:
>>
>> #include <iostream>
>> #include <vector>
>> #include <boost/spirit/include/phoenix_core.hpp>
>> #include <boost/spirit/home/phoenix/stl.hpp>
>> #include <boost/spirit/include/phoenix_operator.hpp>
>>
>>
>> using namespace boost::phoenix;
>>
>> struct foo {
>> typedef void result_type;
>> void operator()(int x) const{
>> std::cout << x << std::endl;
>> }
>> };
>>
>> namespace px = boost::phoenix;
>>
>> int
>> main()
>> {
>> std::vector<int> r(10, 1);
>> foo f;
>> px::for_each(px::arg_names::arg1, f)(r);
>> return 0;
>>
>> }
>>
>> This compiles just fine and prints 1 ten times....
>> I do not need bind to use 'f', it is a perfectly fine unary function
>> object. Phoenix has no business messing with it :)
>
> Aha! You've just uncovered an undocumented feature that
> I was working on. In phoenix1 and in an experimental
> version of the scopes, this is actually valid:
>
> for_each(_1, val(f))(r);
>
> Phx always captures by value, so f is essentially val(f)
> and val(f)() *is* the f itself. Notice the extra nullary
> function application.
>
> (aside: I'm actually surprised that this feature got in.
> Some tests by Dan Marsden even takes advantage of it).
>

*scratches head*

Is it really undocumented? Look at http://tinyurl.com/4lnlh3 , the
section about Values

"
  add(arg1, 6)

Passing a second argument, 6, an actor<value<int> > is implicitly
created behind the scenes. This is also equivalent to:

  add(arg1, val(6))
"

Besides, this is the behavior one would expect (as it mimics std::bind).

> :-) You must be smiling now imagining me slapping
> my head! :-)
>

:)

Anyways, from your last post, I see that we basically agree on
lambda[] behavior, so I'll stop here.

-- 
gpd

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