How can std::unique_ptr have no size overhead?
Solution 1:
The reason is that the typename _Dp = default_delete<_Tp>
is an empty class and the tuple
template employs empty base class optimization.
If you instantiate the unique_ptr
with a non-default delete, you should see the size increase.
Solution 2:
unique_ptr
as specified can have zero overhead because the only thing needed to implement it is to modify the process of copying/moving a raw pointer; no additional information is necessary. Therefore unique_ptr
doesn't need to store anything besides the pointer and can be the same size as a pointer.
As to how your particular implementation, achieves that; Only most derived types need to have a size greater than zero. Empty base classes can take up zero bytes. It's quite common for standard library implementations to take advantage of the so-called 'empty base class' optimization for all kinds of things, from stateless allocators in containers to tuple.