Boost logo

Boost :

Subject: Re: [boost] Weak functor: Interested? (How to boost::bind to a weak_ptr)
From: Roman Perepelitsa (roman.perepelitsa_at_[hidden])
Date: 2012-01-11 04:38:08


2012/1/11 Vladimír Třebický <vladimir.trebicky_at_[hidden]>

> On Wed, Jan 11, 2012 at 9:19 AM, Roman Perepelitsa
> <roman.perepelitsa_at_[hidden]> wrote:
>
> > It can be done. In fact, it has already been done. See
> > http://lists.boost.org/boost-users/2011/09/70647.php.
>
> Nice. I like this. However, once you declare the get_pointer (plus
> ptr_adater and invoker) there's no way to specify what should happen
> if lock() returns empty shared_ptr. In the example "invoker" returns
> default value:
>
> R operator()() const
> {
> return ci_.first ? (ci_.first->*ci_.second)() : R();
> }
>
> which may (a) not be possible (if R is not default-constructible) or
> (b) not desired (eg. if user wants an exception to be thrown).
>
> Do you think there would be a way to control the behavior?

 In this case one will have to use something like boost::weak_fn or write
the trampoline function manually.

void do_something(const weak_ptr<foo>& p) {
  // Complete freedom here.
  if (shared_ptr<foo> sp = p.lock())
    sp->do_something();
  else
    throw "expired weak_ptr";
}

bind(do_something, my_weak_ptr)();

In practice, however, I only ever used void-returning functors with
weak_ptr.

Roman Perepelitsa.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk