19 General utilities library [utilities]

19.11 Smart pointers [smartptr]

19.11.3 Class template shared_­ptr [util.smartptr.shared]

19.11.3.9 Casts [util.smartptr.shared.cast]

template<class T, class U> shared_ptr<T> static_pointer_cast(const shared_ptr<U>& r) noexcept; template<class T, class U> shared_ptr<T> static_pointer_cast(shared_ptr<U>&& r) noexcept;
Requires: The expression static_­cast<T*>((U*)nullptr) shall be well-formed.
Returns:
shared_ptr<T>(R, static_cast<typename shared_ptr<T>::element_type*>(r.get()))
where R is r for the first overload, and std::move(r) for the second.
[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; template<class T, class U> shared_ptr<T> dynamic_pointer_cast(shared_ptr<U>&& r) noexcept;
Requires: The expression dynamic_­cast<T*>((U*)nullptr) shall be well-formed.
The expression dynamic_­cast<typename shared_­ptr<T>::element_­type*>(r.get()) shall be well formed and shall have well-defined behavior.
Returns:
  • When dynamic_­cast<typename shared_­ptr<T>::element_­type*>(r.get()) returns a non-null value p, shared_­ptr<T>(R, p), where R is r for the first overload, and std::move(r) for the second.
  • 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; template<class T, class U> shared_ptr<T> const_pointer_cast(shared_ptr<U>&& r) noexcept;
Requires: The expression const_­cast<T*>((U*)nullptr) shall be well-formed.
Returns:
shared_ptr<T>(R, const_cast<typename shared_ptr<T>::element_type*>(r.get()))
where R is r for the first overload, and std::move(r) for the second.
[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; template<class T, class U> shared_ptr<T> reinterpret_pointer_cast(shared_ptr<U>&& r) noexcept;
Requires: The expression reinterpret_­cast<T*>((U*)nullptr) shall be well-formed.
Returns:
shared_ptr<T>(R, reinterpret_cast<typename shared_ptr<T>::element_type*>(r.get()))
where R is r for the first overload, and std::move(r) for the second.
[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
]