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

5.2 KiB
Raw Blame History

[util.smartptr.shared.cast]

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.10 Casts [util.smartptr.shared.cast]

🔗

template<class T, class U> constexpr shared_ptr<T> static_pointer_cast(const shared_ptr<U>& r) noexcept; template<class T, class U> constexpr shared_ptr<T> static_pointer_cast(shared_ptr<U>&& r) noexcept;

1

#

Mandates: The expression static_cast<T*>((U*)nullptr) is well-formed.

2

#

Returns: shared_ptr(R, static_cast<typename shared_ptr::element_type*>(r.get())) where R is r for the first overload, andstd::move(r) for the second.

3

#

[Note 1:

The seemingly equivalent expressionshared_ptr(static_cast<T*>(r.get())) can result in undefined behavior, attempting to delete the same object twice.

— end note]

🔗

template<class T, class U> constexpr shared_ptr<T> dynamic_pointer_cast(const shared_ptr<U>& r) noexcept; template<class T, class U> constexpr shared_ptr<T> dynamic_pointer_cast(shared_ptr<U>&& r) noexcept;

4

#

Mandates: The expression dynamic_cast<T*>((U*)nullptr) is well-formed.

The expression dynamic_cast<typename shared_ptr::element_type*>(r.get()) is well-formed.

5

#

Preconditions: The expression dynamic_cast<typename shared_ptr::element_type*>(r.get()) has well-defined behavior.

6

#

Returns:

  • (6.1)

    When dynamic_cast<typename shared_ptr::element_type*>(r.get()) returns a non-null value p, shared_ptr(R, p), where R is r for the first overload, and std::move(r) for the second.

  • (6.2)

    Otherwise, shared_ptr().

7

#

[Note 2:

The seemingly equivalent expressionshared_ptr(dynamic_cast<T*>(r.get())) can result in undefined behavior, attempting to delete the same object twice.

— end note]

🔗

template<class T, class U> constexpr shared_ptr<T> const_pointer_cast(const shared_ptr<U>& r) noexcept; template<class T, class U> constexpr shared_ptr<T> const_pointer_cast(shared_ptr<U>&& r) noexcept;

8

#

Mandates: The expression const_cast<T*>((U*)nullptr) is well-formed.

9

#

Returns: shared_ptr(R, const_cast<typename shared_ptr::element_type*>(r.get())) where R is r for the first overload, andstd::move(r) for the second.

10

#

[Note 3:

The seemingly equivalent expressionshared_ptr(const_cast<T*>(r.get())) can result in undefined behavior, attempting to delete the same object twice.

— end note]

🔗

template<class T, class U> shared_ptr<T> reinterpret_pointer_cast(const shared_ptr<U>& r) noexcept; template<class T, class U> shared_ptr<T> reinterpret_pointer_cast(shared_ptr<U>&& r) noexcept;

11

#

Mandates: The expression reinterpret_cast<T*>((U*)nullptr) is well-formed.

12

#

Returns: shared_ptr(R, reinterpret_cast<typename shared_ptr::element_type*>(r.get())) where R is r for the first overload, andstd::move(r) for the second.

13

#

[Note 4:

The seemingly equivalent expressionshared_ptr(reinterpret_cast<T*>(r.get())) can result in undefined behavior, attempting to delete the same object twice.

— end note]