Boost logo

Boost :

Subject: Re: [boost] Weak functor: Interested? (How to boost::bind to a weak_ptr)
From: Vladimír Tøebický (vladimir.trebicky_at_[hidden])
Date: 2012-01-11 04:25:31


On Wed, Jan 11, 2012 at 9:19 AM, Roman Perepelitsa
<roman.perepelitsa_at_[hidden]> wrote:
> 2012/1/10 Vladimír Tøebický <vladimir.trebicky_at_[hidden]>
>
>> On Tue, Jan 10, 2012 at 10:34 PM, Roman Perepelitsa
>> <roman.perepelitsa_at_[hidden]> wrote:
>> > 2012/1/10 Vladimír Tøebický <vladimir.trebicky_at_[hidden]>
>> >
>> >>
>> >> 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).
>
>
> 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?

V.


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