[util.smartptr.shared.obs]

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

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

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

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

#### 20.3.2.2.6 Observers [util.smartptr.shared.obs]

[ð](#lib:get,shared_ptr)

`constexpr element_type* get() const noexcept;
`

[1](#1)

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

*Returns*: The stored pointer[.](#1.sentence-1)

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

`constexpr T& operator*() const noexcept;
`

[2](#2)

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

*Preconditions*: get() != nullptr[.](#2.sentence-1)

[3](#3)

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

*Returns*: *get()[.](#3.sentence-1)

[4](#4)

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

*Remarks*: When T is an array type or cv void,
it is unspecified whether this
member function is declared[.](#4.sentence-1)

If it is declared, it is unspecified what its
return type is, except that the declaration (although not necessarily the
definition) of the function shall be well-formed[.](#4.sentence-2)

[ð](#lib:operator-%3e,shared_ptr)

`constexpr T* operator->() const noexcept;
`

[5](#5)

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

*Preconditions*: get() != nullptr[.](#5.sentence-1)

[6](#6)

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

*Returns*: get()[.](#6.sentence-1)

[7](#7)

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

*Remarks*: When T is an array type,
it is unspecified whether this member function is declared[.](#7.sentence-1)

If it is declared, it is unspecified what its return type is,
except that the declaration (although not necessarily the definition)
of the function shall be well-formed[.](#7.sentence-2)

[ð](#lib:operator%5b%5d,shared_ptr)

`constexpr element_type& operator[](ptrdiff_t i) const;
`

[8](#8)

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

*Preconditions*: get() != nullptr is true[.](#8.sentence-1)

[9](#9)

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

*Hardened preconditions*: i â¥ 0[.](#9.sentence-1)

If T is U[N], i < N[.](#9.sentence-2)

[10](#10)

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

*Returns*: get()[i][.](#10.sentence-1)

[11](#11)

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

*Throws*: Nothing[.](#11.sentence-1)

[12](#12)

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

*Remarks*: When T is not an array type,
it is unspecified whether this member function is declared[.](#12.sentence-1)

If it is declared, it is unspecified what its return type is,
except that the declaration (although not necessarily the definition)
of the function shall be well-formed[.](#12.sentence-2)

[ð](#lib:use_count,shared_ptr)

`constexpr long use_count() const noexcept;
`

[13](#13)

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

*Synchronization*: None[.](#13.sentence-1)

[14](#14)

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

*Returns*: The number of shared_ptr objects, *this included,
that share ownership with *this, or 0 when *this is
empty[.](#14.sentence-1)

[15](#15)

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

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

get() == nullptr does not imply a specific return value of use_count()[.](#15.sentence-1)

â *end note*]

[16](#16)

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

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

weak_ptr<T>​::​lock() can affect the return value of use_count()[.](#16.sentence-1)

â *end note*]

[17](#17)

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

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

When multiple threads
might affect the return value of use_count(),
the result is approximate[.](#17.sentence-1)

In particular, use_count() == 1 does not imply that accesses through
a previously destroyed shared_ptr have in any sense completed[.](#17.sentence-2)

â *end note*]

[ð](#lib:operator_bool,shared_ptr)

`constexpr explicit operator bool() const noexcept;
`

[18](#18)

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

*Returns*: get() != nullptr[.](#18.sentence-1)

[ð](#lib:owner_before,shared_ptr)

`template<class U> constexpr bool owner_before(const shared_ptr<U>& b) const noexcept;
template<class U> constexpr bool owner_before(const weak_ptr<U>& b) const noexcept;
`

[19](#19)

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

*Returns*: An unspecified value such that

- [(19.1)](#19.1)

owner_before(b) defines a strict weak ordering as defined in [[alg.sorting]](alg.sorting "26.8 Sorting and related operations");

- [(19.2)](#19.2)

!owner_before(b) && !b.owner_before(*this) is true if and only if owner_equal(b) is true[.](#19.sentence-1)

[ð](#lib:owner_hash,shared_ptr)

`size_t owner_hash() const noexcept;
`

[20](#20)

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

*Returns*: An unspecified value such that,
for any object x where owner_equal(x) is true,owner_hash() == x.owner_hash() is true[.](#20.sentence-1)

[ð](#lib:owner_equal,shared_ptr)

`template<class U>
  constexpr bool owner_equal(const shared_ptr<U>& b) const noexcept;
template<class U>
  constexpr bool owner_equal(const weak_ptr<U>& b) const noexcept;
`

[21](#21)

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

*Returns*: true if and only if*this and b share ownership or are both empty[.](#21.sentence-1)

Otherwise returns false[.](#21.sentence-2)

[22](#22)

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

*Remarks*: owner_equal is an equivalence relation[.](#22.sentence-1)
