Boost logo

Boost :

Subject: Re: [boost] Phoenix.bind VS Lambda.bind
From: OvermindDL1 (overminddl1_at_[hidden])
Date: 2010-04-09 20:21:59


On Fri, Apr 9, 2010 at 11:55 AM, Giovanni Piero Deretta
<gpderetta_at_[hidden]> wrote:
> On Fri, Apr 9, 2010 at 6:43 PM, Alexey Tkachenko
> [snip]
>>
>> Both Lambda and Phoenix pass arguments by reference or by const
>> reference.
>
> IIRC lambda does perfect forwarding up to 3 arguments (i.e. it has
> overloads for all const and non const combinations up to 3 args).
>
> Phoenix does not: it takes its parameters by non 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
>>
>
> By explicitly casting to const int&, I think that, for the expression:
>
>   bind(add, _1, _2)((const int&)10, i)
>
> T1 is deduced as const in for
>
>  template <class T1, class T2>
>  operator()(T1&, T2&)
>
> forming a const reference that can bind to the temporary. I had never
> realized that the cast would have worked!

For note, I think it is expected for the new Phoenix v3 to use the
Functional.Forwarding adapters to fix that bug up to any number (not
just 3 or so) of arguments.


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