Defined in header <memory> | ||
---|---|---|
template< class T, class U > std::shared_ptr<T> static_pointer_cast( const std::shared_ptr<U>& r ); | (1) | (since C++11) |
template< class T, class U > std::shared_ptr<T> dynamic_pointer_cast( const std::shared_ptr<U>& r ); | (2) | (since C++11) |
template< class T, class U > std::shared_ptr<T> const_pointer_cast( const std::shared_ptr<U>& r ); | (3) | (since C++11) |
template< class T, class U > std::shared_ptr<T> reinterpret_pointer_cast( const std::shared_ptr<U>& r ); | (4) | (since C++17) |
Creates a new instance of std::shared_ptr
whose stored pointer is obtained from r
's stored pointer using a cast expression. If r
is empty, so is the new shared_ptr
(but its stored pointer is not necessarily null).
Otherwise, the new shared_ptr
will share ownership with r
, except that it is empty if the dynamic_cast
performed by dynamic_pointer_cast
returns a null pointer.
Let Y
be typename std::shared_ptr<T>::element_type
, then the resulting std::shared_ptr
's stored pointer will be obtained by calling (in respective order):
static_cast<Y*>(r.get())
.dynamic_cast<Y*>(r.get())
(If the result of the dynamic_cast
is a null pointer value, the returned shared_ptr
will be empty).const_cast<Y*>(r.get())
.reinterpret_cast<Y*>(r.get())
The behavior of these functions is undefined unless the corresponding cast from U*
to T*
is well formed:
static_cast<T*>((U*)nullptr)
is well formed.dynamic_cast<T*>((U*)nullptr)
is well formed.const_cast<T*>((U*)nullptr)
is well formed.reinterpret_cast<T*>((U*)nullptr)
is well formed.r | - | The pointer to convert |
noexcept
specification: noexcept
The expressions std::shared_ptr<T>(static_cast<T*>(r.get()))
, std::shared_ptr<T>(dynamic_cast<T*>(r.get()))
and std::shared_ptr<T>(const_cast<T*>(r.get()))
might seem to have the same effect, but they all will likely result in undefined behavior, attempting to delete the same object twice!
First version |
---|
template< class T, class U > std::shared_ptr<T> static_pointer_cast( const std::shared_ptr<U>& r ) noexcept { auto p = static_cast<typename std::shared_ptr<T>::element_type*>(r.get()); return std::shared_ptr<T>(r, p); } |
Second version |
template< class T, class U > std::shared_ptr<T> dynamic_pointer_cast( const std::shared_ptr<U>& r ) noexcept { if (auto p = dynamic_cast<typename std::shared_ptr<T>::element_type*>(r.get())) { return std::shared_ptr<T>(r, p); } else { return std::shared_ptr<T>(); } } |
Third version |
template< class T, class U > std::shared_ptr<T> const_pointer_cast( const std::shared_ptr<U>& r ) noexcept { auto p = const_cast<typename std::shared_ptr<T>::element_type*>(r.get()); return std::shared_ptr<T>(r, p); } |
#include <iostream> #include <memory> struct BaseClass {}; struct DerivedClass : BaseClass { void f() const { std::cout << "Hello World!\n"; } ~DerivedClass(){ // note, it's not virtual std::cout << "~DerivedClass\n"; } }; int main() { std::shared_ptr<BaseClass> ptr_to_base(std::make_shared<DerivedClass>()); // ptr_to_base->f(); // Error won't compile: BaseClass has no member named 'f' std::static_pointer_cast<DerivedClass>(ptr_to_base)->f(); // OK // (constructs a temporary shared_ptr, then calls operator->) static_cast<DerivedClass*>(ptr_to_base.get())->f(); // also OK // (direct cast, does not construct a temporary shared_ptr) }
Output:
Hello World! Hello World! ~DerivedClass
constructs new shared_ptr (public member function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/memory/shared_ptr/pointer_cast