Re: [boost] [bind/lambda] a functor holding a shared_ptr

AMDG Yuval Ronen wrote:
In the following code:
struct A { void func(); }
int main() { shared_ptr<A> a(new A); boost::bind(&A::func, a); }
the bind() call returns a function object which holds the shared_ptr by value (it's one of the shared owners). This is an important feature I'm using, because sometimes the functor is the last owner left standing, and I want to keep A alive until the functor's destructor (actually, I don't really like this design, but I'm talking about legacy code here). Replacing boost::bind with lambda::bind doesn't compile. Is this feature not supported by lambda::bind? If not supported now, is it planned to be supported in the future? Can I in the mean time implement this feature in my code with relative ease?
Thanks, Yuval You can explicitly dereference the shared_ptr.
#include <boost/shared_ptr.hpp> #include <boost/lambda/lambda.hpp> #include <boost/lambda/bind.hpp> struct A { void func() {} }; template<class T> struct identity_t { mutable T t; template<class Args> struct sig { typedef T& type; }; identity_t(const T& arg) : t(arg) {} T& operator()() const { return(t); } }; template<class T> identity_t<T> identity(const T& t) { return(t); } int main() { boost::shared_ptr<A> a(new A); boost::lambda::bind(&A::func, *boost::lambda::bind(identity(a)))(); } In Christ, Steven Watanabe

Steven Watanabe wrote:
int main() { boost::shared_ptr<A> a(new A); boost::lambda::bind(&A::func, *boost::lambda::bind(identity(a)))(); }
*lambda::constant(a) should work (but doesn't). This is arguably a bug in Lambda's contentsof_type handling of cv qualifiers, but I'm afraid to touch it. :-)
participants (2)
-
Peter Dimov
-
Steven Watanabe