function template
<memory>

std::dynamic_pointer_cast

template <class T, class U>
  shared_ptr<T> dynamic_pointer_cast (const shared_ptr<U>& sp) noexcept;
Dynamic cast of shared_ptr
Returns a copy of sp of the proper type with its stored pointer casted dynamically from U* to T*.

If sp is not empty, and such a cast would not return a null pointer, the returned object shares ownership over sp's resources, increasing by one the use count.

Otherwise, the returned object is an empty shared_ptr.

The function can only cast types for which the following expression would be valid:
 
dynamic_cast<T*>(sp.get())


Parameters

sp
A shared_ptr.
U* shall be convertible to T* using dynamic_cast.

Return Value

A shared_ptr object that owns the same pointer as sp (if any) and has a shared pointer that points to the same object as sp with a potentially different type.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// static_pointer_cast example
#include <iostream>
#include <memory>

struct A {
  static const char* static_type;
  const char* dynamic_type;
  A() { dynamic_type = static_type; }
};
struct B: A {
  static const char* static_type;
  B() { dynamic_type = static_type; }
};

const char* A::static_type = "class A";
const char* B::static_type = "class B";

int main () {
  std::shared_ptr<A> foo;
  std::shared_ptr<B> bar;

  bar = std::make_shared<B>();

  foo = std::dynamic_pointer_cast<A>(bar);

  std::cout << "foo's static  type: " << foo->static_type << '\n';
  std::cout << "foo's dynamic type: " << foo->dynamic_type << '\n';
  std::cout << "bar's static  type: " << bar->static_type << '\n';
  std::cout << "bar's dynamic type: " << bar->dynamic_type << '\n';

  return 0;
}


Output:
foo's static  type: class A
foo's dynamic type: class B
bar's static  type: class B
bar's dynamic type: class B

See also