Daily bit(e) of C++ | static_pointer_cast, dynamic_pointer_cast, const_pointer_cast, reinterpret_pointer_cast
Daily bit(e) of C++ #470, Four C++17 cast-style functions that can convert the pointed-to type of std::shared_ptr without affecting ownership.
When handling smart pointers, we might need to change the pointed-to type without changing the pointed-to object.
With std::unique_ptr, this is fairly straightforward. However, if you try to do the same with std::shared_ptr, you need to be careful to maintain the shared ownership.
C++17 simplified this process by introducing four cast-style functions:
std::static_pointer_cast
std::dynamic_pointer_cast
std::const_pointer_cast
std::reinterpret_pointer_cast