This is a question about understanding a design decision, not a complaint about bugs or flaws.
In the C++ standard library, the function to create a shared pointer and its object is a straight function,
template< class T, class... Args> std::shared_ptr<T> make_shared( Args&&... args );
Why is this function not a static
function of shared_ptr<T>
, like this:
template <class... Args> std::shared_ptr<T>::make(Args&&... args);
This would keep the std::
namespace a little tidier, and would also allow for something like this:
using shared_vector = std::shared_ptr<std::vector<int>>;
shared_vector a = shared_vector::make(7);
The only rationale given in the proposal for std::make_shared
, document N2351, is that it makes it possible to add the functionality non-intrusively to any already existing implementation of shared_ptr
.
std::make_shared
was added to the standard draft after std::shared_ptr
itself and there had been prior shared_ptr
implementations, e.g. in boost.