[util.smartptr.shared.assign]

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

## 20.3 Smart pointers [[smartptr]](smartptr#util.smartptr.shared.assign)

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

#### 20.3.2.2 Class template shared_ptr [[util.smartptr.shared]](util.smartptr.shared#assign)

#### 20.3.2.2.4 Assignment [util.smartptr.shared.assign]

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

`constexpr shared_ptr& operator=(const shared_ptr& r) noexcept;
template<class Y> constexpr shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
`

[1](#1)

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

*Effects*: Equivalent to shared_ptr(r).swap(*this)[.](#1.sentence-1)

[2](#2)

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

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

[3](#3)

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

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

The use count updates caused by the temporary object
construction and destruction are not observable side
effects, so the implementation can meet the effects (and the
implied guarantees) via different means, without creating a
temporary[.](#3.sentence-1)

In particular, in the example:shared_ptr<int> p(new int);
shared_ptr<void> q(p);
p = p;
q = p; both assignments can be no-ops[.](#3.sentence-2)

â *end note*]

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

`constexpr shared_ptr& operator=(shared_ptr&& r) noexcept;
template<class Y> constexpr shared_ptr& operator=(shared_ptr<Y>&& r) noexcept;
`

[4](#4)

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

*Effects*: Equivalent to shared_ptr(std​::​move(r)).swap(*this)[.](#4.sentence-1)

[5](#5)

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

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

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

`template<class Y, class D> constexpr shared_ptr& operator=(unique_ptr<Y, D>&& r);
`

[6](#6)

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

*Effects*: Equivalent to shared_ptr(std​::​move(r)).swap(*this)[.](#6.sentence-1)

[7](#7)

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

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