As illustrated in the code here, the size of the object returned from make_shared is two pointers.
However, why doesn\'t make_shared
work like the following
I have a honey::shared_ptr implementation that automatically optimizes to a size of 1 pointer when intrusive. It's conceptually simple -- types that inherit from SharedObj
have an embedded control block, so in that case shared_ptr
is intrusive and can be optimized. It unifies boost::intrusive_ptr
with non-intrusive pointers like std::shared_ptr
and std::weak_ptr
.
This optimization is only possible because I don't support aliasing (see Howard's answer). The result of make_shared
can then have 1 pointer size if T
is known to be intrusive at compile-time. But what if T
is known to be non-intrusive at compile-time? In this case it's impractical to have 1 pointer size as shared_ptr
must behave generically to support control blocks allocated both alongside and separately from their objects. With only 1 pointer the generic behavior would be to point to the control block, so to get at T*
you'd have to first dereference the control block which is impractical.