Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2006-05-08 15:25:29


Gerardo Lamastra wrote:

> So, my first question would be: where could I find an exhaustive
> comparison between these two facilities? And, is there any indication
> on what to prefer, also related to future development and/or inclusion
> in C++ standards (If I remember well, boost::lambda has been proposed
> in tr1?)

Nobody has written an exhaustive comparison between boost::bind and
lambda::bind, but the general rule is to use lambda::bind if you use Lambda,
boost::bind otherwise. :-)

TR1 contains a 'bind' that is a superset of boost::bind. Both boost::bind
and boost::lambda::bind are reasonable approximations of std::tr1::bind.

> The situation is the following: if I try to create a nullary
> lamnda function with boost::lambda::bind()
> like this:
>
> generate_n(inserter(v, v.begin()), 10,
> boost::lambda::bind<Slave*>(
> boost::lambda::new_ptr<Slave>(),*this) );
>
> the compiler fails to compile: it tries to pass the this pointer
> as a const reference somehow, and then it fails claiming that
> a Slave(const Master& m) cannot be found; If I try to add one,
> it fails with a similar error.
>
> If I substitute the boost::lambda::bind() with a boost::bind(),
> it compiles & works perfectly.

Your code seems wrong. Remember that bind makes a copy of its arguments,
'*this' in this case. So when you construct a new Slave, its reference is
initialized to this internal copy of *this. The function object then
disappears and the result is a dangling reference. Use boost::ref(*this).

Now, if you are interested in why Lambda doesn't accept the code whereas
Bind does: Lambda always makes these internal copies const. boost::bind
propagates the const-ness of the function object to its members. In this
case the function object is non-const and so is the internal copy of *this.


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