Scott Meyers mentions in his book, Effective Modern C++, that using shared_ptr
to arrays is discouraged because when converted to Base class pointers it creates "holes" in the type system.
However, it is possible to create shared_ptr<T>
from unique_ptr<T[]>
in the following way
std::shared_ptr<D> pDerived = std::shared_ptr<D>(std::make_unique<D[]>(3)); // Create an array of 3 D's
Is the above code potentially dangerous? Are there pitfalls if pDerived
is later copied into a pBase
?
std::shared_ptr<B> pBase = pDerived; // B is the base class for D