std::make_unique,std::make_unique_default_init (3) - Linux Manuals
std::make_unique,std::make_unique_default_init: std::make_unique,std::make_unique_default_init
NAME
std::make_unique,std::make_unique_default_init - std::make_unique,std::make_unique_default_init
Synopsis
Defined in header <memory>
template< class T, class... Args > (1) (since C++14)
unique_ptr<T> make_unique( Args&&... args ); (only for non-array types)
template< class T > (2) (since C++14)
unique_ptr<T> make_unique( std::size_t size ); (only for array types with unknown bound)
template< class T, class... Args > (3) (since C++14)
/* unspecified */ make_unique( Args&&... args ) = delete; (only for array types with known bound)
template< class T > (4) (since C++20)
unique_ptr<T> make_unique_default_init( ); (only for non-array types)
template< class T > (5) (since C++20)
unique_ptr<T> make_unique_default_init( std::size_t size ); (only for array types with unknown bound)
template< class T, class... Args > (6) (since C++20)
/* unspecified */ make_unique_default_init( Args&&... args ) = delete; (only for array types with known bound)
Constructs an object of type T and wraps it in a std::unique_ptr.
1) Constructs a non-array type T. The arguments args are passed to the constructor of T. This overload only participates in overload resolution if T is not an array type. The function is equivalent to:
2) Constructs an array of unknown bound T. This overload only participates in overload resolution if T is an array of unknown bound. The function is equivalent to:
3,6) Construction of arrays of known bound is disallowed.
4) Same as (1), except that the object is default-initialized. This overload only participates in overload resolution if T is not an array type. The function is equivalent to:
5) Same as (2), except that the array is default-initialized. This overload only participates in overload resolution if T is an array of unknown bound. The function is equivalent to:
Parameters
args - list of arguments with which an instance of T will be constructed.
size - the size of the array to construct
Return value
std::unique_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.
Possible Implementation
Notes
Unlike std::make_shared (which has std::allocate_shared), std::make_unique does not have an allocator-aware counterpart. A hypothetical allocate_unique would be required to invent the deleter type D for the unique_ptr<T,D> it returns which would contain an allocator object and invoke both destroy and deallocate in its operator().
Example
// Run this code
Output:
See also
constructor (public member function)
make_shared
make_shared_default_init creates a shared pointer that manages a new object
(C++20)