Boost logo

Boost :

Subject: [boost] enable_shared_from_this::weak_from_this() request
From: Marat Abrarov (abrarov_at_[hidden])
Date: 2011-06-11 04:17:51

Hi, boost users and experts.

I often use boost::enable_shared_from_this.
I know - it contains boost::weak_ptr, which is initiated in the details of
boost::shared_ptr's constructor.
Also sometimes I need function weak_from_this() at the classes derived from
I currently use something like:
class my_class
  : public boost::enable_shared_from_this<my_class>
  , private boost::noncopyable
  ...somewhere in my_class details...
  typedef my_class this_type;
  typedef boost::weak_ptr<this_type> this_type_weak_ptr;

  this_type_weak_ptr weak_from_this()
    return this_type_weak_ptr(shared_from_this());
}; // my_class
But as I know restoring shared_ptr from weak_ptr (done in shared_from_this()) is
quite expensive. So my_class::weak_from_this() does unnecessary work instead of
just copy internal weak_ptr stored in enable_shared_from_this.

So the question is - why there is no weak_from_this() member function in
boost::enable_shared_from_this class?
May be it would be useful to add such member function?

Marat Abrarov.

Boost list run by bdawes at, gregod at, cpdaniel at, john at