[util.smartptr.enab] (original) (raw)

20 Memory management library [mem]

20.3 Smart pointers [smartptr]

20.3.2 Shared-ownership pointers [util.sharedptr]

20.3.2.7 Class template enable_shared_from_this [util.smartptr.enab]

A class T can inherit from enable_shared_from_this<T>to inherit the shared_from_this member functions that obtain a shared_ptr instance pointing to *this.

[Example 1: struct X: public enable_shared_from_this<X> { };int main() { shared_ptr<X> p(new X); shared_ptr<X> q = p->shared_from_this(); assert(p == q); assert(p.owner_equal(q)); } — _end example_]

namespace std { template<class T> class enable_shared_from_this { protected: constexpr enable_shared_from_this() noexcept; enable_shared_from_this(const enable_shared_from_this&) noexcept; enable_shared_from_this& operator=(const enable_shared_from_this&) noexcept;~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;private: mutable weak_ptr<T> weak-this; };}

The template parameter T of enable_shared_from_thismay be an incomplete type.

Effects: Value-initializes weak-this.

[Note 1:

weak-this is not changed.

— _end note_]

Returns: shared_ptr<T>(weak-this).