std::enable_shared_from_this (3) - Linux Man Pages
Defined in header <memory>
template< class T > class enable_shared_from_this; (since C++11)
std::enable_shared_from_this allows an object t that is currently managed by a std::shared_ptr named pt to safely generate additional std::shared_ptr instances pt1, pt2, ... that all share ownership of t with pt.
Publicly inheriting from std::enable_shared_from_this<T> provides the type T with a member function shared_from_this. If an object t of type T is managed by a std::shared_ptr<T> named pt, then calling T::shared_from_this will return a new std::shared_ptr<T> that shares ownership of t with pt.
constructor (protected member function)
destructor (protected member function)
operator= (protected member function)
shared_from_this (public member function)
weak_from_this returns the weak_ptr which shares ownership of *this
Member name Definition
weak_this (private)(C++17) std::weak_ptr object tracking the control block of the first shared owner of *this. Exposition only
A common implementation for enable_shared_from_this is to hold a weak reference (such as std::weak_ptr) to this. The constructors of std::shared_ptr detect the presence of an
unambiguous and accessible
(since C++17) enable_shared_from_this base and assign the newly created std::shared_ptr to the internally stored weak reference
if not already owned by a live std::shared_ptr
(since C++17). Constructing a std::shared_ptr for an object that is already managed by another std::shared_ptr will not consult the internally stored weak reference and thus will lead to undefined behavior.
It is permitted to call shared_from_this only on a previously shared object, i.e. on an object managed by std::shared_ptr<T>. Otherwise
the behavior is undefined
std::bad_weak_ptr is thrown (by the shared_ptr constructor from a default-constructed weak_this)
enable_shared_from_this provides the safe alternative to an expression like std::shared_ptr<T>(this), which is likely to result in this being destructed more than once by multiple owners that are unaware of each other (see example below)
// Run this code
shared_ptr smart pointer with shared object ownership semantics