Boost logo

Boost Users :

From: Jaakko Jarvi (jajarvi_at_[hidden])
Date: 2003-10-13 11:52:51

In our last exciting episode Eelis van der Weegen wrote:
> Lines: 28
> User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US;
> rv:1.5b) Gecko/20030901 Thunderbird/0.2

> When Lambda's bind() is used to create a functor for dereferencing a
> stored data member pointer given an instance pointer, it seems it is not
> possible to pass the resulting functor a temporary instance pointer. For
> example:

> #include <boost/lambda/bind.hpp>
> #include <boost/lambda/lambda.hpp>

> struct S { int i; };

> S * f ();

> void g (S * p)
> {
> using namespace boost::lambda;

> bind(&S::i, _1)(p); // ok
> bind(&S::i, _1)(p + 0); // not ok, "no match for call ..."
> bind(&S::i, _1)(f()); // not ok, "no match for call ..."
> }

> Does this restriction have a purpose? If not, is this something that
> needs to be (and can be) fixed?

Lambda functors cannot take a non-const rvalue as an argument.
There isn't a pretty fix for it at the momemnt, but there is some
work going on in the standards committee regarding the so called
"forwarding problem" which would allow a fix. So in a few years... :)

Now, there are several workarounds:

Use make_const helper function:

bind(&S::i, _1)(make_const(p + 0));

or do:

S* p2 = p + 0;
bind(&S::i, _1)(p2);

and more, check

5.9.2. Rvalues as actual arguments to lambda functors

in lambda docs.

  Cheers, Jaakko

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at