template <class T, class U> shared_ptr<T> const_pointer_cast (const shared_ptr<U>& sp) noexcept;
1
const_cast<T*>(sp.get())
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// static_pointer_cast example
#include <iostream>
#include <memory>
int main () {
std::shared_ptr<int> foo;
std::shared_ptr<const int> bar;
foo = std::make_shared<int>(10);
bar = std::const_pointer_cast<const int>(foo);
std::cout << "*bar: " << *bar << '\n';
*foo = 20;
std::cout << "*bar: " << *bar << '\n';
return 0;
}
*bar: 10 *bar: 20