Is there a way to cast shared_ptr<void> to shared_ptr<T>?

danijar picture danijar · Sep 16, 2014 · Viewed 7k times · Source

I want to keep the smart behavior of std::shared_ptr. So is there a way to cast a shared void pointer to another type while without confusing the reference counting? I can't get the raw pointer and create a new shared pointer from it.

Answer

rob mayoff picture rob mayoff · Sep 16, 2014

You can use std::static_pointer_cast or std::dynamic_pointer_cast depending on what kind of cast you want.