Someone I worked with once said that shared_ptr was unsafe and would slice when casting from a derived class to a base class (i.e. upcasting). For example if there were 2 cl
Pointers are PODs (just for the record: shared_ptr
s aren't).
The question quotes:
shared_ptr can be implicitly converted to shared_ptr whenever T* can be implicitly converted to U*.
This is about converting from one type to another, which is different from upcasting. There's no inheritance relationship between shared_ptr
and shared_ptr
, whether or not A
derives from B
or viceversa. This is the reason why the shared_ptr
object itself doesn't slice.
Consider a class hierarchy A, B with no virtual destructors.
std::shared_ptr a(new B);
auto a = std::make_shared();
will capture B's deallocator and then will call B's destructor when needed
std::shared_ptr a((A*)(new B));
will do no such thing and will cause slicing problems in the pointed-to object.
For example, using unique_ptr
has different behaviour:
std::unique_ptr a(new B);
std::unique_ptr a((A*)(new B));
will both exhibit slicing problems, while
auto a = std::make_unique();
won't.
Using a plain pointer doesn't help either:
A* a = new B{};
delete a;
is a recipe for disaster.
Example code available here.