
2012/1/10 Vladimír Třebický <vladimir.trebicky@gmail.com>
On Tue, Jan 10, 2012 at 10:34 PM, Roman Perepelitsa
boost::function<void(int)> f = boost::bind(boost::weak_fn(&Foo::bar, some_weak_ptr), _1); f(1);
It's it better to bind &Foo::bar directly?
boost::function<void(int)> f = boost::bind(&Foo::bar, some_weak_ptr, _1);
Much simpler in my opinion.
That cannot be done, boost::bind() indeed accepts references, pointers and shared_ptrs for member functions but doesn't accept weak pointers, because a weak pointer cannot be trivially converted to shared_ptr (or anything that leads to the corresponding class instance).
Indeed, I've needed similar functionality in a system designed to support dynamically loaded modules. I have: template <class T> void register_function( boost::shared_ptr<function_directory> const &, name const &, boost::function<T> const &, boost::weak_ptr<void> const & lifetime ); template <class T> boost::function<T> lock_function( boost::shared_ptr<function_directory const> const &, name const & ); The passed lifetime weak_ptr is initialized from a shared_ptr that keeps the loaded module afloat. Locking the function throws if the module was unloaded, otherwise the returned boost::function<T> keeps the module afloat until discarded. Something similar could be implemented as a pair of types, boost::shared_function and boost::weak_function, the semantics being analogous to boost::shared_ptr and boost::weak_ptr. Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode