I know that std::unique_ptr
and std::shared_ptr
are different classes that address different needs, and therefore asking which one is better is mostly an ill-posed question.
However, as regards their content and performance, without considering the different semantics of the two smart pointers, I have some doubt I want clarify.
My understanding is that a std::unique_ptr
contains the raw pointer as its only member variable, and stores the deleter, if any custom one is given, as part of the type; whereas the std::shared_ptr
stores in member variables the raw as well as the pointer to a dynamically allocated block which contains the custom deleter, and strong and weak counters.
Scott Meyers, in Effective Modern C++, stresses a lot on this difference in the size that the two smart pointers require (and on the difference in performance in general), however I'd be tempted to say that as soon as a std::unique_ptr
is provided with a function pointer as custom deleter it becomes as big as std::shared_ptr
, whose size does not increase with the custom deleter.
From there, I would deduce that using a function pointer as a custom deleter for std::unique_ptr
basically annihilates the advantage this smart pointer has on the other one, in terms of size/performance.
Is this the case?