Defined in header
template< class T, class Alloc, class... Args >
shared_ptr<T> allocate_shared( const Alloc& alloc, 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
T. The object is constructed as if by the expression ::new (pv) T(std::forward<Args>(args)...), where
pv is an internal
void* pointer to storage suitable to hold an object of type
T. The storage is typically larger than
sizeof(T) in order to use one allocation for both the control block of the shared pointer and the
All memory allocation is done using a copy of alloc, which must satisfy the
std::shared_ptr constructor called by this function enables
shared_from_this with a pointer to the newly constructed object of type
|alloc||-|| The |
|args...||-|| list of arguments with which an instance of |
 Return value
std::shared_ptr of an instance of type
Can throw the exceptions thrown from Alloc::allocate() or from the constructor of
T. If an exception is thrown, this function has no effect.
Like std::make_shared, this function typically performs only one allocation, and places both the
T object and the control block in the allocated memory block (the standard recommends, but does not require this, all known implementations do this). A copy of
alloc is stored as part of the control block so that it can be used to deallocate it once both shared and weak reference counts reach zero.
Unlike the std::shared_ptr constructors, std::allocate_shared does not accept a separate custom deleter: the supplied allocator is used for destruction of the control block and the
T object, and for deallocation of their shared memory block.
A constructor enables
shared_from_this with a pointer
ptr of type
U* means that it determines if
U has an unambiguous and accessible (since C++17) base class that is a specialization of std::enable_shared_from_this, and if so, the constructor evaluates the statement:
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.
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.
 See also
| constructs new |
(public member function)
| creates a shared pointer that manages a new object |