Files
cppdraft_translate/cppdraft/util/smartptr/weak/general.md
2025-10-25 03:02:53 +03:00

3.2 KiB

[util.smartptr.weak.general]

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.