23 General utilities library [utilities]

23.11 Smart pointers [smartptr]

23.11.2 Shared-ownership pointers [util.smartptr]

23.11.2.2 Class template shared_Β­ptr [util.smartptr.shared]

23.11.2.2.9 shared_Β­ptr casts [util.smartptr.shared.cast]

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

Requires: The expression static_Β­cast<T*>((U*)0) shall be well formed.

Returns:

shared_ptr<T>(r, static_cast<typename shared_ptr<T>::element_type*>(r.get()))

[ Note: The seemingly equivalent expression shared_Β­ptr<T>(static_Β­cast<T*>(r.get())) will eventually result in undefined behavior, attempting to delete the same object twice.  — end note ]

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

Requires: The expression dynamic_Β­cast<T*>((U*)0) shall be well formed and shall have well defined behavior.

Returns:

  • When dynamic_Β­cast<typename shared_Β­ptr<T>​::​element_Β­type*>(r.get()) returns a nonzero value p, shared_Β­ptr<T>(r, p).

  • Otherwise, shared_Β­ptr<T>().

[ Note: The seemingly equivalent expression shared_Β­ptr<T>(dynamic_Β­cast<T*>(r.get())) will eventually result in undefined behavior, attempting to delete the same object twice.  — end note ]

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

Requires: The expression const_Β­cast<T*>((U*)0) shall be well formed.

Returns:

shared_ptr<T>(r, const_cast<typename shared_ptr<T>::element_type*>(r.get()))

[ Note: The seemingly equivalent expression shared_Β­ptr<T>(const_Β­cast<T*>(r.get())) will eventually 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;

Requires: The expression reinterpret_Β­cast<T*>((U*)0) shall be well formed.

Returns:

shared_ptr<T>(r, reinterpret_cast<typename shared_ptr<T>::element_type*>(r.get()))

[ Note: The seemingly equivalent expression shared_Β­ptr<T>(reinterpret_Β­cast<T*>(r.get())) will eventually result in undefined behavior, attempting to delete the same object twice.  — end note ]