Boost logo

Boost :

Subject: Re: [boost] Phoenix.bind VS Lambda.bind
From: Alexey Tkachenko (alek.tkachenko_at_[hidden])
Date: 2010-04-09 13:43:39


>> Hi,
>> Now I'm looking at Phoenix and Lambda libraries. Could somebody
>> explain why Lambda allows to use the following expression:
>> void foo(int) { ... }
>> ...
>> bind(foo, _1)(10);
>>
>> yet Phoenix does not.
>> Phoenix requires to use helper variable:
>> void foo(int) { ... }
>> ...
>> int i = 0;
>> bind(foo, _1)(i);
>>
>> I've read document about "Forwarding Function Problem"
>> (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1385.htm). I
>> understand the core issue, but how does Lambda.bind solve it?
>
> Lambda takes by value, Phoenix takes by reference.
>

Both Lambda and Phoenix pass arguments by reference or by const
reference. Moreover if I write following code it compiles fine:
int add(int a, int b)
{
        return a + b;
}
void bind_expression()
{
        const int i = 5;
        cout << bind(add, _1, _2)(i, i) << endl;
        cout << bind(add, _1, _2)((const int&)10, i) << endl;
}
But if I change second 'bind' to (type cast is removed):
cout << bind(add, _1, _2)(10, i) << endl;
compilation failes.

For me it's kind of magic. I use boost 1.39 and my compiler - g++ 4.3.1

Regards,

Alexey.


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