Defined in header
template< class T, class... Args >
shared_ptr<T> make_shared( Args&&... args );
Constructs an object of type
T and wraps it in a std::shared_ptr using
args as the parameter list for the constructor of
|args||-|| list of arguments with which an instance of |
 Return value
std::shared_ptr of an instance of type
May throw std::bad_alloc or any exception thrown by the constructor of
T. If an exception is thrown, this function has no effect.
This function is typically used to replace the construction std::shared_ptr<T>(new T(args...)) of a shared pointer from the raw pointer returned by a call to
new. In contrast to that expression, std::make_shared<T> typically allocates memory for the
T object and for the std::shared_ptr's control block with a single memory allocation (this is a non-binding requirement in the Standard), where std::shared_ptr<T>(new T(args...)) performs at least two memory allocations.
Moreover, code such as f(std::shared_ptr<int>(new int(42)), g()) can cause a memory leak if
g throws an exception because
g() may be called after
new int(42) and before the constructor of
shared_ptr<int>. This doesn't occur in f(std::make_shared<int>(42), g()), since two function calls are never interleaved.
 See also
| constructs new |
(public member function)
| creates a shared pointer that manages a new object allocated using an allocator |