Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/memory/shared ptr/pointer cast"

From cppreference.com
< cpp‎ | memory‎ | shared ptr
m (Shorten template names. Use {{lc}} where appropriate.)
m (Use since= and until= params of {{dcl}} template.)
Line 2: Line 2:
 
{{cpp/memory/shared_ptr/navbar}}
 
{{cpp/memory/shared_ptr/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl | num=1 | notes={{mark since c++11}} | 1=  
+
{{dcl | num=1 | since=c++11 | 1=  
 
template< class T, class U >  
 
template< class T, class U >  
 
shared_ptr<T> static_pointer_cast( const shared_ptr<U>& r );
 
shared_ptr<T> static_pointer_cast( const shared_ptr<U>& r );
 
}}
 
}}
{{dcl | num=2 | notes={{mark since c++11}} | 1=  
+
{{dcl | num=2 | since=c++11 | 1=  
 
template< class T, class U >  
 
template< class T, class U >  
 
shared_ptr<T> dynamic_pointer_cast( const shared_ptr<U>& r );
 
shared_ptr<T> dynamic_pointer_cast( const shared_ptr<U>& r );
 
}}
 
}}
{{dcl | num=3 | notes={{mark since c++11}} | 1=  
+
{{dcl | num=3 | since=c++11 | 1=  
 
template< class T, class U >  
 
template< class T, class U >  
 
shared_ptr<T> const_pointer_cast( const shared_ptr<U>& r );
 
shared_ptr<T> const_pointer_cast( const shared_ptr<U>& r );

Revision as of 15:40, 1 July 2013

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
Dynamic memory management
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Allocators
Garbage collection support
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)



 
 
template< class T, class U >
shared_ptr<T> static_pointer_cast( const shared_ptr<U>& r );
(1) (since C++11)
template< class T, class U >
shared_ptr<T> dynamic_pointer_cast( const shared_ptr<U>& r );
(2) (since C++11)
template< class T, class U >
shared_ptr<T> const_pointer_cast( const shared_ptr<U>& r );
(3) (since C++11)

Will return a new instance of std::shared_ptr with a casted managed object type from the r's managed object type. Both smart pointers will share the ownership of the managed object.

The resulting std::shared_ptr's managed object will be obtained by calling (in respective order):

1) static_cast<T*>(r.get()).
2) dynamic_cast<T*>(r.get()) (If the result of the dynamic_cast is 0, the returned shared_ptr will be empty).
3) const_cast<T*>(r.get()).

In any case, if the parameter r is an empty std::shared_ptr the result will be a new empty std::shared_ptr.

Parameters

r - The pointer to convert

Exceptions

noexcept specification:  
noexcept