I think the custom allocator will be used to allocate space for the shared count object, which stores a copy of the deleocator and reference count.
As for which custom divider can be used to ...
One use was mentioned: make shared_ptr compatible with objects that must be deleted using some special function (for example, FILE , which is deleted by fclose ), without having to bind it to a helper class that takes care of proper deletion.
Another use for custom deletion is pools. The pool can pass shared_ptr<T> , which were initialized by a "special" deleter, which does not actually delete anything, but instead returns the object to the pool.
And one more thing: a debugger is already needed to implement some shared_ptr functions. For instance. the deleted type is always fixed at creation time and does not depend on the initialization type shared_ptr .
Vou can create a shared_ptr<Base> , actually initializing it with Derived . shared_ptr ensures that when an object is deleted, it will be deleted as Derived , even if Base does not have a virtual dtor. To make this possible, shared_ptr should already store some information about how the object should be deleted. Thus, allowing the user to specify a fully customizable deaeter does not cost anything (in terms of performance at runtime) and does not require special additional code.
There are probably dozens of other scenarios in which you can efficiently use a custom deleter, this is what I came up with so far.
source share