 enable_shared_from_this
enable_shared_from_this
            The header <boost/enable_shared_from_this.hpp> defines
            the class template enable_shared_from_this. It is used as a
            base class that allows a shared_ptr or
            a weak_ptr to the current object to be obtained
            from within a member function.
        
enable_shared_from_this<T> defines two member functions
            called shared_from_this that return a shared_ptr<T>
            and shared_ptr<T const>, depending on constness, to this.
            It also defines two member functions called weak_from_this that return
            a corresponding weak_ptr.
        
#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
#include <cassert>
class Y: public boost::enable_shared_from_this<Y>
{
public:
    boost::shared_ptr<Y> f()
    {
        return shared_from_this();
    }
};
int main()
{
    boost::shared_ptr<Y> p(new Y);
    boost::shared_ptr<Y> q = p->f();
    assert(p == q);
    assert(!(p < q || q < p)); // p and q must share ownership
}
        
namespace boost
{
template<class T> class enable_shared_from_this
{
public:
    shared_ptr<T> shared_from_this();
    shared_ptr<T const> shared_from_this() const;
    weak_ptr<T> weak_from_this() noexcept;
    weak_ptr<T const> weak_from_this() const noexcept;
}
}
        template<class T> shared_ptr<T>
            enable_shared_from_this<T>::shared_from_this();template<class T> shared_ptr<T const>
            enable_shared_from_this<T>::shared_from_this() const;Requires:
enable_shared_from_this<T>must be an accessible base class ofT.*thismust be a subobject of an instancetof typeT.Returns: If a
shared_ptrinstancepthat ownstexists, ashared_ptr<T>instancerthat shares ownership withp.Postconditions:
r.get() == this.Throws:
bad_weak_ptrwhen noshared_ptrowns*this.
template<class T> weak_ptr<T>
            enable_shared_from_this<T>::weak_from_this() noexcept;template<class T> weak_ptr<T const>
            enable_shared_from_this<T>::weak_from_this() const noexcept;Requires:
enable_shared_from_this<T>must be an accessible base class ofT.*thismust be a subobject of an instancetof typeT.Returns: If a
shared_ptrinstancepthat ownstexists or has existed in the past, aweak_ptr<T>instancerthat shares ownership withp. Otherwise, an emptyweak_ptr.
Copyright © 2002, 2003, 2015 by Peter Dimov. Distributed under the Boost Software License, Version 1.0. See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.