How does shared_ptr<void> know which destructor to use?

The shared state co-owned by shared pointers also contains a deleter, a function like object that is fed the managed object at the end of its lifetime in order to release it. We can even specify our own deleter by using the appropriate constructor. How the deleter is stored, as well as any type erasure it undergoes is an implementation detail. But suffice it to say that the shared state contains a function that knows exactly how to free the owned resource.

Now, when we create an object of a concrete type with make_shared<Thing>() and don't provide a deleter, the shared state is set to hold some default deleter that can free a Thing. The implementation can generate one from the template argument alone. And since its stored as part of the shared state, it doesn't depend on the type T of any shared_pointer<T> that may be sharing ownership of the state. It will always know how to free the Thing.

So even when we make voidPtr the only remaining pointer, the deleter remains unchanged, and still knows how to free a Thing. Which is what it does when the voidPtr goes out of scope.


The shared_ptr only knows how to handle a management-object with a known interface. That management-object provides two reference-counts (weak for itself, strong for the managed object), as well as containing the deleter (access aside from calling it is only provided if the type is known) and pointer to be deleted (private).

What type and object the shared_ptr points to is a completely separate concern from the management-object it uses, though for sanity it should not be longer-lived.