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

6.0 KiB

[util.smartptr.shared.general]

20 Memory management library [mem]

20.3 Smart pointers [smartptr]

20.3.2 Shared-ownership pointers [util.sharedptr]

20.3.2.2 Class template shared_ptr [util.smartptr.shared]

20.3.2.2.1 General [util.smartptr.shared.general]

1

#

The shared_ptr class template stores a pointer, usually obtained via new.

shared_ptr implements semantics of shared ownership; the last remaining owner of the pointer is responsible for destroying the object, or otherwise releasing the resources associated with the stored pointer.

Ashared_ptr is said to be empty if it does not own a pointer.

namespace std {template class shared_ptr {public:using element_type = remove_extent_t; using weak_type = weak_ptr; // [util.smartptr.shared.const], constructorsconstexpr shared_ptr() noexcept; constexpr shared_ptr(nullptr_t) noexcept : shared_ptr() { }templateconstexpr explicit shared_ptr(Y* p); template<class Y, class D>constexpr shared_ptr(Y* p, D d); template<class Y, class D, class A>constexpr shared_ptr(Y* p, D d, A a); templateconstexpr shared_ptr(nullptr_t p, D d); template<class D, class A>constexpr shared_ptr(nullptr_t p, D d, A a); templateconstexpr shared_ptr(const shared_ptr& r, element_type* p) noexcept; templateconstexpr shared_ptr(shared_ptr&& r, element_type* p) noexcept; constexpr shared_ptr(const shared_ptr& r) noexcept; templateconstexpr shared_ptr(const shared_ptr& r) noexcept; constexpr shared_ptr(shared_ptr&& r) noexcept; templateconstexpr shared_ptr(shared_ptr&& r) noexcept; templateconstexpr explicit shared_ptr(const weak_ptr& r); template<class Y, class D>constexpr shared_ptr(unique_ptr<Y, D>&& r); // [util.smartptr.shared.dest], destructorconstexpr ~shared_ptr(); // [util.smartptr.shared.assign], assignmentconstexpr shared_ptr& operator=(const shared_ptr& r) noexcept; templateconstexpr shared_ptr& operator=(const shared_ptr& r) noexcept; constexpr shared_ptr& operator=(shared_ptr&& r) noexcept; templateconstexpr shared_ptr& operator=(shared_ptr&& r) noexcept; template<class Y, class D>constexpr shared_ptr& operator=(unique_ptr<Y, D>&& r); // [util.smartptr.shared.mod], modifiersconstexpr void swap(shared_ptr& r) noexcept; constexpr void reset() noexcept; templateconstexpr void reset(Y* p); template<class Y, class D>constexpr void reset(Y* p, D d); template<class Y, class D, class A>constexpr void reset(Y* p, D d, A a); // [util.smartptr.shared.obs], observersconstexpr element_type* get() const noexcept; constexpr T& operator*() const noexcept; constexpr T* operator->() const noexcept; constexpr element_type& operator[](ptrdiff_t i) const; constexpr long use_count() const noexcept; constexpr explicit operator bool() 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 shared_ptr(weak_ptr) -> shared_ptr; template<class T, class D> shared_ptr(unique_ptr<T, D>) -> shared_ptr;}

2

#

Specializations of shared_ptr shall be Cpp17CopyConstructible,Cpp17CopyAssignable, and Cpp17LessThanComparable, allowing their use in standard containers.

Specializations of shared_ptr shall be contextually convertible to bool, allowing their use in boolean expressions and declarations in conditions.

3

#

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

[Note 1:

T can be a function type.

— end note]

4

#

[Example 1: if (shared_ptr px = dynamic_pointer_cast(py)) {// do something with px} — end example]

5

#

For purposes of determining the presence of a data race, member functions shall access and modify only the shared_ptr and weak_ptr objects themselves and not objects they refer to.

Changes in use_count() do not reflect modifications that can introduce data races.

6

#

For the purposes of [smartptr], a pointer type Y* is said to becompatible with a pointer type T* when eitherY* is convertible to T* orY is U[N] and T is cv U[].