
Neal Coombes wrote:
Bind is very convenient for use with shared_ptr:
struct C { void func() {} }; shared_ptr<C> c(new C); bind(&C::func, c)(); // calls c->func()
However I've found a snag when trying to do function composition:
class A { shared_ptr<C> func() { return shared_ptr<C>(new C); } }; A a; bind(&C::func, bind(&A::func, a)); // doesn't work, instead must: bind(&C::func, bind(&get_pointer<B>, bind(&A::func, a)));
This is actually a problem in mem_fn. Bind is doing the right thing, but the function object that is implicitly created by mem_fn(&C::func) doesn't handle rvalues of type shared_ptr<C>. If you patch mem_fn_template.hpp below the lines template<class U> R operator()(U & u) const { BOOST_MEM_FN_RETURN call(u, &u); } and insert template<class U> R operator()(U const & u) const { BOOST_MEM_FN_RETURN call(u, &u); } it should work. The reason that this overload is not already present is historical: when mem_fn was written many compilers had problems with the additional overload. Now most compilers are in a much better shape, but nobody has reported this as a problem, so it never got fixed. I'll fix it in CVS when I have time (to write the additional mem_fn tests based on your example.)