Quick Q: was raw-pointer constructor of shared_ptr a mistake?
Quick A: No, its usage is well defined.
Recently on SO:
was raw-pointer constructor of shared_ptr a mistake?
In hindsight, given make_shared, would shared_ptr have a constructor that takes a raw pointer had it been introduced with C++11?
What if you don't control the allocation of the object? What if you need to use a custom deleter? What if you need list-initialization instead of parens?None of these cases is handled by
make_shared
.Additionally, if you're using
weak_ptr
, ashared_ptr
allocated viamake_shared
won't free any memory until all theweak_ptr
s are destroyed as well. So even if you have a normal shared pointer where none of the above apply, it's possible that you may still prefer the raw pointer constructor.Yet another situation would be if your type provides overloads for operator
new
and operatordelete
. These may make it ill-suited formake_shared
, since those overloads will not be called - and presumably they exist for a reason.