std::static_pointer_cast,std::dynamic_pointer_cast,std::const_pointer_cast, (3) Linux Manual Page
std::static_pointer_cast,std::dynamic_pointer_cast,std::const_pointer_cast, – std::static_pointer_cast,std::dynamic_pointer_cast,std::const_pointer_cast,
Synopsis
Defined in header<memory>
template <class T, class U>
std::shared_ptr<T> static_pointer_cast(const std::shared_ptr<U> &(1)(since C++ 11)
r) noexcept;
template <class T, class U>
std::shared_ptr<T> static_pointer_cast(std::shared_ptr<U> &&r)(2)(since C++ 20)
noexcept;
template <class T, class U>
std::shared_ptr<T> dynamic_pointer_cast(const std::shared_ptr<U> &(3)(since C++ 11)
r) noexcept;
template <class T, class U>
std::shared_ptr<T> dynamic_pointer_cast(std::shared_ptr<U> &&r)(4)(since C++ 20)
noexcept;
template <class T, class U>
std::shared_ptr<T> const_pointer_cast(const std::shared_ptr<U> &r(5)(since C++ 11)
) noexcept;
template <class T, class U>
std::shared_ptr<T> const_pointer_cast(std::shared_ptr<U> &&r)(6)(since C++ 20)
noexcept;
template <class T, class U>
std::shared_ptr<T> reinterpret_pointer_cast(const(7)(since C++ 17)
std::shared_ptr<U> &r) noexcept;
template <class T, class U>
std::shared_ptr<T> reinterpret_pointer_cast(std::shared_ptr<U> && (8)(since C++ 20)
r) noexcept;
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
null). Otherwise, the new shared_ptr will share ownership with the initial value of
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 evaluating, respectively:
1-2)
3-4)
value, the returned shared_ptr will be empty.)
5-6)
7-8)
The behavior of these functions is undefined unless the corresponding cast from U*
to T*
1-2)
3-4)
5-6)
7-8)
formed.
After calling the rvalue overloads
==
if the dynamic_cast fails.
Parameters
r – The pointer to convert
Notes
The expressions std::shared_ptr<T>(static_cast<T*>(r.get())),
std::shared_ptr<T>(dynamic_cast<T*>(r.get()))
std::shared_ptr<T>(const_cast<T*>(r.get()))
they all will likely result in undefined behavior, attempting to delete the same
object twice!
Possible implementation
First version
template<
std::shared_ptr<T>
{
}
Second version
template<
std::shared_ptr<T>
{
}
template<
std::shared_ptr<T>
{
}
template<
std::shared_ptr<T>
{
}
Example
// Run this code
#include <iostream>
#include <memory>
struct Base
{
int a;
virtual void f() const
{
std::cout << "I am base!\n";
}
virtual ~Base()
{
}
};
struct Derived : Base
{
void f() const override
{
std::cout << "I am derived!\n";
}
~Derived()
{
}
};
int main()
{
auto basePtr = std::make_shared<Base>();
std::cout << "Base pointer says: ";
basePtr->f();
auto derivedPtr = std::make_shared<Derived>();
std::cout << "Derived pointer says: ";
derivedPtr->f();
// static_pointer_cast to go up class hierarchy
basePtr = std::static_pointer_cast<Base>(derivedPtr);
std::cout << "Base pointer to derived says: ";
basePtr->f();
// dynamic_pointer_cast to go down/across class hierarchy
auto downcastedPtr = std::dynamic_pointer_cast<Derived>(basePtr);
if (downcastedPtr) {
std::cout << "Downcasted pointer says: ";
downcastedPtr->f();
}
// All pointers to derived share ownership
std::cout << "Pointers to underlying derived: "
<< derivedPtr.use_count()
<< "\n";
}
Output:
See also
constructor
