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;
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 1: 
The seemingly equivalent expression shared_ptr<T>(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;
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 2: 
The seemingly equivalent expression shared_ptr<T>(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;
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 3: 
The seemingly equivalent expression shared_ptr<T>(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;
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 4: 
The seemingly equivalent expression shared_ptr<T>(reinterpret_cast<T*>(r.get() can result in undefined behavior, attempting to delete the same object twice.
— end note]

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant