make_shared
is more performant than separately calling new
and creating a shared_ptr
because make_shared
allocates space for the reference count and weak count in the same memory block as the client object instance (effectively giving the shared_ptr
most of the performance benefits of an intrusive_ptr
).
enable_shared_from_this
gives a shared pointer without having a reference to any shared pointer. Therefore things like the reference and weak count have to be somehow accessible from inside the client object. Therefore, it would be sensible for enable_shared_from_this
to cause an intrusive count similar to make_shared
.
However, I have no idea how something like that might be implemented (and I'm not sure I'd follow what was going on in there even if I look at the actual source).
Would it make sense then (for performance reasons) to tag my class with enable_shared_from_this
if I know it's only ever going to be used as a shared_ptr
and never as a raw object?