[util.smartptr.weak.obs]

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

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

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

#### 20.3.2.3 Class template weak_ptr [[util.smartptr.weak]](util.smartptr.weak#obs)

#### 20.3.2.3.6 Observers [util.smartptr.weak.obs]

[ð](#lib:use_count,weak_ptr)

`constexpr long use_count() const noexcept;
`

[1](#1)

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

*Returns*: 0 if *this is empty;
otherwise, the number of shared_ptr instances
that share ownership with *this[.](#1.sentence-1)

[ð](#lib:expired,weak_ptr)

`constexpr bool expired() const noexcept;
`

[2](#2)

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

*Returns*: use_count() == 0[.](#2.sentence-1)

[ð](#lib:lock,weak_ptr)

`constexpr shared_ptr<T> lock() const noexcept;
`

[3](#3)

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

*Returns*: expired() ? shared_ptr<T>() : shared_ptr<T>(*this), executed atomically[.](#3.sentence-1)

[ð](#lib:owner_before,weak_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;
`

[4](#4)

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

*Returns*: An unspecified value such that

- [(4.1)](#4.1)

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

- [(4.2)](#4.2)

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

[ð](#lib:owner_hash,weak_ptr)

`size_t owner_hash() const noexcept;
`

[5](#5)

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

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

[ð](#lib:owner_equal,weak_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;
`

[6](#6)

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

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

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

[7](#7)

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

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