Boost logo

Boost :

Subject: Re: [boost] Phoenix.bind VS Lambda.bind
From: Giovanni Piero Deretta (gpderetta_at_[hidden])
Date: 2010-04-09 13:55:46


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!

HTH,

-- 
gpd

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