shared-ptr

When would I want to construct a shared pointer from a raw pointer

邮差的信 提交于 2021-01-20 19:06:19
问题 Thanks to std::make_shared , I wonder, whether the constructor for std::shared_ptr , which takes a raw pointer has any value except when interfacing with legacy / library code, e.g. when storing the output of a factory. Are there other legitimate use-cases? Is it reasonable advice to avoid that constructor? Even to the extend of putting code checks for it in place that warn the programmer whenever it is used? Should the same guidelines (whatever they are) apply to shared_ptr<T>::reset(T*) ?

When would I want to construct a shared pointer from a raw pointer

青春壹個敷衍的年華 提交于 2021-01-20 19:03:53
问题 Thanks to std::make_shared , I wonder, whether the constructor for std::shared_ptr , which takes a raw pointer has any value except when interfacing with legacy / library code, e.g. when storing the output of a factory. Are there other legitimate use-cases? Is it reasonable advice to avoid that constructor? Even to the extend of putting code checks for it in place that warn the programmer whenever it is used? Should the same guidelines (whatever they are) apply to shared_ptr<T>::reset(T*) ?