[util.smartptr.enab]

# 20 Memory management library [[mem]](./#mem)

## 20.3 Smart pointers [[smartptr]](smartptr#util.smartptr.enab)

### 20.3.2 Shared-ownership pointers [[util.sharedptr]](util.sharedptr#util.smartptr.enab)

#### 20.3.2.7 Class template enable_shared_from_this [util.smartptr.enab]

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L5289)

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[.](#1.sentence-1)

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L5295)

[*Example [1](#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)); // p and q share ownership} â *end example*]

namespace std {template<class T> class enable_shared_from_this {protected:constexpr enable_shared_from_this() noexcept; constexpr enable_shared_from_this(const enable_shared_from_this&) noexcept; constexpr enable_shared_from_this& operator=(const enable_shared_from_this&) noexcept; constexpr ~enable_shared_from_this(); public:constexpr shared_ptr<T> shared_from_this(); constexpr shared_ptr<T const> shared_from_this() const; constexpr weak_ptr<T> weak_from_this() noexcept; constexpr weak_ptr<T const> weak_from_this() const noexcept; private:mutable weak_ptr<T> *weak-this*; // *exposition only*};}

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L5330)

The template parameter T of enable_shared_from_this may be an incomplete type[.](#3.sentence-1)

[ð](#lib:enable_shared_from_this,constructor)

`constexpr enable_shared_from_this() noexcept;
constexpr enable_shared_from_this(const enable_shared_from_this<T>&) noexcept;
`

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L5341)

*Effects*: Value-initializes *weak-this*[.](#4.sentence-1)

[ð](#lib:operator=,enable_shared_from_this)

`constexpr enable_shared_from_this<T>& operator=(const enable_shared_from_this<T>&) noexcept;
`

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L5352)

*Returns*: *this[.](#5.sentence-1)

[6](#6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L5356)

[*Note [1](#note-1)*:

*weak-this* is not changed[.](#6.sentence-1)

â *end note*]

[ð](#lib:shared_ptr)

`constexpr shared_ptr<T>       shared_from_this();
constexpr shared_ptr<T const> shared_from_this() const;
`

[7](#7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L5370)

*Returns*: shared_ptr<T>(*weak-this*)[.](#7.sentence-1)

[ð](#lib:weak_ptr)

`constexpr weak_ptr<T>       weak_from_this() noexcept;
constexpr weak_ptr<T const> weak_from_this() const noexcept;
`

[8](#8)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L5383)

*Returns*: *weak-this*[.](#8.sentence-1)
