In hindsight, given make_shared
, would shared_ptr
have a constructor that takes a raw pointer had it been introduced with C++11?
Are there stro
In hindsight, given
make_shared
, wouldshared_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
, a shared_ptr
allocated via make_shared
won't free any memory until all the weak_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 operator delete
. These may make it ill-suited for make_shared
, since those overloads will not be called - and presumably they exist for a reason.