Boost logo

Boost Users :

Subject: [Boost-users] enable_shared_from_this::weak_from_this() request
From: Marat Abrarov (abrarov_at_[hidden])
Date: 2011-06-09 23:38:12

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
boost::enable_shared_from_this. For now I 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

So the question is: why there is no weak_from_this() member function in
boost::enable_shared_from_this class?

Marat Abrarov.

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at