Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | memory‎ | shared ptr
m (-endl, +plain static_cast variant to constrast)
(this casts the stored pointer, not the managed one)
Line 16: Line 16:
  
 
{{dcl end}}
 
{{dcl end}}
Creates a new instance of {{lc|std::shared_ptr}} whose managed object type is obtained from the {{tt|r}}'s managed object type using a cast expression. Both smart pointers will share the ownership of the managed object.
+
Creates a new instance of {{lc|std::shared_ptr}} whose stored pointer is obtained from the {{tt|r}}'s stored pointer using a cast expression. If {{tt|r}} is empty, so is the new {{tt|shared_ptr}} (but its stored pointer is not necessarily null).
  
The resulting {{lc|std::shared_ptr}}'s managed object will be obtained by calling (in respective order):
+
Otherwise, the new {{tt|shared_ptr}} will share ownership with {{tt|r}}, except that it is empty if the {{tt|dynamic_cast}} performed by {{tt|dynamic_pointer_cast}} returns a null pointer.
 +
 
 +
The resulting {{lc|std::shared_ptr}}'s stored pointer will be obtained by calling (in respective order):
  
 
@1@ {{tt|static_cast<T*>(r.get())}}.
 
@1@ {{tt|static_cast<T*>(r.get())}}.
Line 25: Line 27:
  
 
@3@ {{tt|const_cast<T*>(r.get())}}.
 
@3@ {{tt|const_cast<T*>(r.get())}}.
 
In any case, if the parameter {{tt|r}} is an empty {{lc|std::shared_ptr}} the result will be a new empty {{lc|std::shared_ptr}}.
 
  
 
===Parameters===
 
===Parameters===
Line 37: Line 37:
  
 
===Notes===
 
===Notes===
The expressions {{c|std::shared_ptr<T>(static_cast<T*>(r.get()))}}, {{c|std::shared_ptr<T>(dynamic_cast<T*>(r.get()))}} and {{c|std::shared_ptr<T>(const_cast<T*>(r.get()))}} might seem to have the same effect, but they all will eventually result in undefined behavior, attempting to delete the same object twice!
+
The expressions {{c|std::shared_ptr<T>(static_cast<T*>(r.get()))}}, {{c|std::shared_ptr<T>(dynamic_cast<T*>(r.get()))}} and {{c|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!
  
 
===Possible implementation===
 
===Possible implementation===

Revision as of 19:17, 12 December 2015

 
 
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 >
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)

Creates a new instance of std::shared_ptr whose stored pointer is obtained from the 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.

The resulting std::shared_ptr's stored pointer 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 a null pointer value, the returned shared_ptr will be empty).
3) const_cast<T*>(r.get()).

Contents

Parameters

r - The pointer to convert

Exceptions

noexcept specification:  
noexcept
  

Notes

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!

Possible implementation

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);
}

Example

#include <iostream>
#include <memory>
 
struct BaseClass {};
 
struct DerivedClass : BaseClass
{
    void f() const
    {
        std::cout << "Hello World!\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!

See also

constructs new shared_ptr
(public member function) [edit]