std::make_shared

From cppreference.com
< cpp‎ | memory‎ | shared ptr
 
 
 
Dynamic memory management
Uninitialized storage
(C++17)
(deprecated since c++17)
(deprecated since c++17)
(deprecated since c++17)
Garbage collection support
Miscellaneous
(C++11)
(C++11)
C Library
Low level memory management
 
 
Defined in header <memory>
template< class T, class... Args >
shared_ptr<T> make_shared( Args&&... args );
(since C++11)

Constructs an object of type T and wraps it in a std::shared_ptr using args as the parameter list for the constructor of T.

The std::shared_ptr constructor called by this function enables shared_from_this with a pointer to the newly constructed object of type T.

Parameters

args - list of arguments with which an instance of T will be constructed.

Return value

std::shared_ptr of an instance of type T.

Exceptions

May throw std::bad_alloc or any exception thrown by the constructor of T. If an exception is thrown, this function has no effect.

Notes

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.

Although std::shared_ptr supports array types (as of C++17), std::make_shared does not. This functionality is supported by boost::make_shared

A constructor enables shared_from_this with a pointer ptr of type U* means that it determines if U has an unambiguous and accessible base class that is a specialization of std::enable_shared_from_this, and if so, the constructor evaluates the statement:

if (ptr != nullptr && ptr->weak_this.expired())
  ptr->weak_this = std::shared_ptr<std::remove_cv_t<U>>(*this,
                                  const_cast<std::remove_cv_t<U>*>(ptr));

Where weak_this is the hidden mutable std::weak_ptr member of std::shared_from_this. The assignment to the weak_this member is not atomic and conflicts with any potentially concurrent access to the same object. This ensures that future calls to shared_from_this() would share ownership with the shared_ptr created by this raw pointer constructor.

The test ptr->weak_this.expired() in the exposition code above makes sure that weak_this is not reassigned if it already indicates an owner. This test is required as of C++17.

Example

#include <iostream>
#include <memory>
 
void foo(const std::shared_ptr<int>& i)
{
    (*i)++;
}
 
int main()
{
    auto sp = std::make_shared<int>(12);
    foo(sp);
    std::cout << *sp << std::endl;
}

Output:

13

See also

constructs new shared_ptr
(public member function)
creates a shared pointer that manages a new object allocated using an allocator
(function template)
creates a unique pointer that manages a new object
(function template)