Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | memory‎ | shared ptr
(Created page with "{{cpp/memory/shared_ptr/title | pointer_cast}} {{cpp/memory/shared_ptr/sidebar}} {{ddcl list begin}} {{ddcl list item | num=1 | notes={{mark since c++11}} | 1= template<class T,...")
 
Line 15: Line 15:
 
Will return a new instance of {{cpp|std::shared_ptr}} with a casted managed object type from the {{tt|r}}'s managed objet type. Both smart pointers will share the ownership of the managed object.
 
Will return a new instance of {{cpp|std::shared_ptr}} with a casted managed object type from the {{tt|r}}'s managed objet type. Both smart pointers will share the ownership of the managed object.
  
The result inner pointer will be created by calling (in respective order):
+
The resulting {{cpp|std::shared_ptr}}'s managed object will be obtained by calling (in respective order):
  
 
1) {{tt|static_cast<T*>(r.get())}}.
 
1) {{tt|static_cast<T*>(r.get())}}.

Revision as of 14:11, 8 March 2012

Template:cpp/memory/shared ptr/sidebar Template:ddcl list begin <tr class="t-dcl ">

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

<td > (1) </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">

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

<td > (2) </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">

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

<td > (3) </td> <td > (since C++11) </td> </tr>

Template:ddcl list end Will return a new instance of Template:cpp with a casted managed object type from the r's managed objet type. Both smart pointers will share the ownership of the managed object.

The resulting Template:cpp'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 all case, if the parameter r is an empty Template:cpp the result will be a new empty Template:cpp.

Parameters

Exceptions

noexcept specification:  
noexcept