Is there a way to cast shared_ptr<void> to shared_ptr<T>?
问题 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. 回答1: You can use the pointer casts from rob mayoff's answer; but be careful. It is easy to unintentionally trigger undefined behavior here: struct MyClass {}; void* rawPtr = new MyClass; shared_ptr<void> exampleVoid(rawPtr); // Undefined behavior; // calls