Boost logo

Boost Users :

From: Jaakko Jarvi (jajarvi_at_[hidden])
Date: 2003-11-21 14:00:33

On Fri, 21 Nov 2003, Peter Dimov wrote:

> Allen Bierbaum wrote:
> > Hello all.
> >
> > I am having trouble with the boost lambda library. I am convinced
> > that my troubles come from some little thing I am overlooking, but
> > for the life of me I can't seem to find the problem. I have been
> > looking at it for the entire afternoon with no solution in sight.
> >
> > I have included some sample code below showing the basic idea of what
> > I am trying to accomplish. Can anyone provide me with a working
> > solution?
> >
> > The summary is:
> > - I have a vector of shared_ptr's to a class A
> > - That class has a method that returns a value I would like to test
> > for in a find_if call
> [...]
> > Can anyone tell me how I can call a method from a lamda method
> > iterating over a sequence of shared_ptrs? And then how do I have
> > this method also call another method?
> The short answer is that you can't, at least with the current
> implementation.
> There are three possible ways to call a member function with Lambda:
> (bl::_1 ->* &Foo::bar )(foo_ptr);
> bl::bind(&Foo::bar, *bl::_1)(foo_ptr);
> bl::bind(&Foo::bar, bl::_1)(foo_ptr);
> The first alternative doesn't work since shared_ptr does not have
> operator->*.
> The second alternative doesn't work since Lambda doesn't support *_1 for
> shared_ptr.

In particular, Lambda's return type deduction templates are not aware
of shared_ptr. The second example can be made to work by telling the
library what the return type of calling operator* on a shared_ptr<Foo> is:

    bl::bind(&Foo::bar, bl::ret<&Foo>(*bl::_1))(foo_ptr);

> The third alternative doesn't work since it is not supported by Lambda
> (probably by design), although it is supported by Bind, and may be supported
> by a future version of Lambda.

Yup, bind is special cased for shared_ptr's, lambda doesn't know anything
about them.

  Best, Jaakko

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