Files
2025-10-25 03:02:53 +03:00

11 KiB
Raw Permalink Blame History

[util.smartptr.weak]

20 Memory management library [mem]

20.3 Smart pointers [smartptr]

20.3.2 Shared-ownership pointers [util.sharedptr]

20.3.2.3 Class template weak_ptr [util.smartptr.weak]

20.3.2.3.1 General [util.smartptr.weak.general]

1

#

The weak_ptr class template stores a weak reference to an object that is already managed by a shared_ptr.

To access the object, aweak_ptr can be converted to a shared_ptr using the member function lock.

namespace std {template class weak_ptr {public:using element_type = remove_extent_t; // [util.smartptr.weak.const], constructorsconstexpr weak_ptr() noexcept; templateconstexpr weak_ptr(const shared_ptr& r) noexcept; constexpr weak_ptr(const weak_ptr& r) noexcept; templateconstexpr weak_ptr(const weak_ptr& r) noexcept; constexpr weak_ptr(weak_ptr&& r) noexcept; templateconstexpr weak_ptr(weak_ptr&& r) noexcept; // [util.smartptr.weak.dest], destructorconstexpr ~weak_ptr(); // [util.smartptr.weak.assign], assignmentconstexpr weak_ptr& operator=(const weak_ptr& r) noexcept; templateconstexpr weak_ptr& operator=(const weak_ptr& r) noexcept; templateconstexpr weak_ptr& operator=(const shared_ptr& r) noexcept; constexpr weak_ptr& operator=(weak_ptr&& r) noexcept; templateconstexpr weak_ptr& operator=(weak_ptr&& r) noexcept; // [util.smartptr.weak.mod], modifiersconstexpr void swap(weak_ptr& r) noexcept; constexpr void reset() noexcept; // [util.smartptr.weak.obs], observersconstexpr long use_count() const noexcept; constexpr bool expired() const noexcept; constexpr shared_ptr lock() const noexcept; templateconstexpr bool owner_before(const shared_ptr& b) const noexcept; templateconstexpr bool owner_before(const weak_ptr& b) const noexcept; size_t owner_hash() const noexcept; templateconstexpr bool owner_equal(const shared_ptr& b) const noexcept; templateconstexpr bool owner_equal(const weak_ptr& b) const noexcept; }; template weak_ptr(shared_ptr) -> weak_ptr;}

2

#

Specializations of weak_ptr shall be Cpp17CopyConstructible andCpp17CopyAssignable, allowing their use in standard containers.

The template parameter T of weak_ptr may be an incomplete type.

20.3.2.3.2 Constructors [util.smartptr.weak.const]

🔗

constexpr weak_ptr() noexcept;

1

#

Effects: Constructs an empty weak_ptr object that stores a null pointer value.

2

#

Postconditions: use_count() == 0.

🔗

constexpr weak_ptr(const weak_ptr& r) noexcept; template<class Y> constexpr weak_ptr(const weak_ptr<Y>& r) noexcept; template<class Y> constexpr weak_ptr(const shared_ptr<Y>& r) noexcept;

3

#

Constraints: For the second and third constructors, Y* is compatible with T*.

4

#

Effects: If r is empty, constructs an empty weak_ptr object that stores a null pointer value; otherwise, constructs a weak_ptr object that shares ownership with r and stores a copy of the pointer stored in r.

5

#

Postconditions: use_count() == r.use_count().

🔗

constexpr weak_ptr(weak_ptr&& r) noexcept; template<class Y> constexpr weak_ptr(weak_ptr<Y>&& r) noexcept;

6

#

Constraints: For the second constructor, Y* is compatible with T*.

7

#

Effects: Move constructs a weak_ptr instance from r.

8

#

Postconditions: *this contains the old value of r.

r is empty, stores a null pointer value, and r.use_count() == 0.

20.3.2.3.3 Destructor [util.smartptr.weak.dest]

🔗

constexpr ~weak_ptr();

1

#

Effects: Destroys this weak_ptr object but has no effect on the object its stored pointer points to.

20.3.2.3.4 Assignment [util.smartptr.weak.assign]

🔗

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

1

#

Effects: Equivalent to weak_ptr(r).swap(*this).

2

#

Returns: *this.

3

#

Remarks: The implementation may meet the effects (and the implied guarantees) via different means, without creating a temporary object.

🔗

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

4

#

Effects: Equivalent to weak_ptr(std::move(r)).swap(*this).

5

#

Returns: *this.

20.3.2.3.5 Modifiers [util.smartptr.weak.mod]

🔗

constexpr void swap(weak_ptr& r) noexcept;

1

#

Effects: Exchanges the contents of *this and r.

🔗

constexpr void reset() noexcept;

2

#

Effects: Equivalent to weak_ptr().swap(*this).

20.3.2.3.6 Observers [util.smartptr.weak.obs]

🔗

constexpr long use_count() const noexcept;

1

#

Returns: 0 if *this is empty; otherwise, the number of shared_ptr instances that share ownership with *this.

🔗

constexpr bool expired() const noexcept;

2

#

Returns: use_count() == 0.

🔗

constexpr shared_ptr<T> lock() const noexcept;

3

#

Returns: expired() ? shared_ptr() : shared_ptr(*this), executed atomically.

🔗

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

#

Returns: An unspecified value such that

owner_before(b) defines a strict weak ordering as defined in [alg.sorting];

!owner_before(b) && !b.owner_before(*this) is true if and only if owner_equal(b) is true.

🔗

size_t owner_hash() const noexcept;

5

#

Returns: An unspecified value such that, for any object x where owner_equal(x) is true,owner_hash() == x.owner_hash() is true.

🔗

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

#

Returns: true if and only if*this and b share ownership or are both empty.

Otherwise returns false.

7

#

Remarks: owner_equal is an equivalence relation.

20.3.2.3.7 Specialized algorithms [util.smartptr.weak.spec]

🔗

template<class T> constexpr void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;

1

#

Effects: Equivalent to a.swap(b).