
On Sat, Mar 21, 2009 at 2:39 PM, bnv <bnv@nc.rr.com> wrote:
Artyom <artyomtnk <at> yahoo.com> writes:
Hello,
Today boost::bind allows to use shared_ptr to ensure that when the function is called the object exists:
shared_ptr<bar> bar_ptr; function<void()> foo;
foo=bind(&bar::slot,bar_ptr)
Now even if we call bar_ptr.reset() the object is still owned by foo.
However, I seem to recall something like this coming up before and the rationale for not supporting it is was that the behavior to follow when the weak pointer is expired is subjective. That is, in some cases you may want an exception, and in other you may want nothing to happen.
I tried doing what my intuition told me would be the moral equivalent, which would be to use composition to give bind a weak_ptr instead of a smart one: foo = bind( &bar::slot, bind( &weak_ptr<bar>::lock, weak_ptr<bar>( bar_ptr ) ) ); However, one of those binds must be persisting the smart pointer somewhere, because calling foo always has an effect for me, where I would expect it to segfault after the shared_ptr is reset, since lock() would return a null shared_ptr. You might be able to see the problem though, and get where you want to be. HTH, Christopher