Namespaces
Variants
Views
Actions

std::allocate_shared

From cppreference.com
 
 
 
 
 
Defined in header <memory>
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 using the parameter alloc to allocate memory. alloc shall be an instance of std::allocator.

Contents

[edit] Parameters

a - An std::allocator of T.
args - list of arguments with which an instance of T will be constructed.

[edit] Return value

std::shared_ptr of an instance of type T.

[edit] Complexity

Constant.

[edit] Notes

This function allocates memory for the T object and for the shared_ptr's control block with a single memory allocation. In contrast, the declaration std::shared_ptr<T> p(new T(Args...)) performs two memory allocations, which may incur unnecessary overhead.

[edit] See also

creates a shared pointer that manages a new object
(function template) [edit]