Boost logo

Boost Users :

From: Johan Torp (johan.torp_at_[hidden])
Date: 2006-11-30 06:35:43


I haven't read all of your posts but I think your main problem is that
boost bind stores arguments by value and takes them by reference.
Because of this it requires lvalues as arguments. You can either add an
internal bind to boost::shared_ptrs get() method which returns a raw
pointer that can be bound or you can make a functor which accepts a raw
pointer (given it won't outlive the shared_ptr). The latter requires you
to create a temporary raw pointer when calling the functor since you
need an lvalue.

Consider the following:

struct A{ int foo() {} };

int main()
{
  boost::shared_ptr<A> sa;
  bind(&A::foo, bind(&boost::shared_ptr<A>::get,_1))(sa);

  A* pa=sa.get();
  bind(&A::foo, _1)(pa);
}

Best Regards, Johan Torp

boost-users-request_at_[hidden] wrote:

>On 11/29/06, "JOAQUIN LOPEZ MU?Z" <joaquin_at_[hidden]> wrote:
>
>First, thank you for your response.
>
>
>
>> std::cerr << "Value: " <<
>> (l::bind(&test_b::f, l::_1))(boost::ref(td)) << "\n";
>>
>>
>
>Well, I tried putting ref on the other side but this works...my way didn't.
>
>Now I would need to find how to use this solution for my original
>problem when td is actually a shared_ptr to an abstract base:
>
>boost::shared_ptr<test_b> td = boost::shared_ptr<test_b>(new test_d);
>
>This does not work:
>
>l::bind(l::bind(&test_b::f, l::_1), boost::ref(*l::_1))(td)
>
>Unlike boost::bind, lambda::bind doesn't work like this:
>
>l::bind(&test_b::f, l::_1)(td)
>
>This does but can't be used in an algorithm on a container holding
>smart pointers:
>
>l::bind(&test_b::f, l::_1)(boost::ref(*td))
>
>Interestingly, boost::bind has an operator == that will allow me to
>compare the result of f() to a value just fine...I thought I needed
>lambda for this. Boost::bind doesn't suffer from an inability to work
>directly with smart pointers and doesn't require special magic to use
>them vs. raw vs. non-ptr. So my original problem is solved by tossing
>lambda out the airlock but it might be worthwhile to find a way to
>accomplish a smart pointer member call with lambda for the future.
>
>So in the end, my problem is thus:
>
>std::find_if(cont.begin(), cont.end(), bind(&deref_val_type::f, _1) == 5);
>
>How is that accomplished with lambda if cont holds smart pointers? So
>far I haven't found any way...
>
>
>

-- 
System Developer
TouchTable AB
Tel: +46 (0)31 773 68 12
johan.torp_at_[hidden]

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