Difference between revisions of "cpp/memory/unique ptr/make unique"
From cppreference.com
< cpp | memory | unique ptr
m (-) |
m (Text replace - "_default_init" to "_for_overwrite") |
||
Line 1: | Line 1: | ||
− | {{cpp/title|make_unique| | + | {{cpp/title|make_unique|make_unique_for_overwrite}} |
{{cpp/memory/unique_ptr/navbar}} | {{cpp/memory/unique_ptr/navbar}} | ||
{{dcl begin}} | {{dcl begin}} | ||
Line 18: | Line 18: | ||
{{dcl | num=4 | notes={{mark since c++20}}<br/>{{mark|only for non-array types}} | | {{dcl | num=4 | notes={{mark since c++20}}<br/>{{mark|only for non-array types}} | | ||
template< class T > | template< class T > | ||
− | unique_ptr<T> | + | unique_ptr<T> make_unique_for_overwrite( ); |
}} | }} | ||
{{dcl | num=5 | notes={{mark since c++20}}<br/>{{mark|only for array types with unknown bound}} | | {{dcl | num=5 | notes={{mark since c++20}}<br/>{{mark|only for array types with unknown bound}} | | ||
template< class T > | template< class T > | ||
− | unique_ptr<T> | + | unique_ptr<T> make_unique_for_overwrite( std::size_t size ); |
}} | }} | ||
{{dcl | num=6 | {{dcl | num=6 | ||
| notes={{mark since c++20}}<br/>{{mark|only for array types with known bound}} | 1= | | notes={{mark since c++20}}<br/>{{mark|only for array types with known bound}} | 1= | ||
template< class T, class... Args > | template< class T, class... Args > | ||
− | /* unspecified */ | + | /* unspecified */ make_unique_for_overwrite( Args&&... args ) = delete; |
}} | }} | ||
{{dcl end}} | {{dcl end}} |
Revision as of 06:35, 20 February 2020
Defined in header <memory>
|
||
template< class T, class... Args > unique_ptr<T> make_unique( Args&&... args ); |
(1) | (since C++14) (only for non-array types) |
template< class T > unique_ptr<T> make_unique( std::size_t size ); |
(2) | (since C++14) (only for array types with unknown bound) |
template< class T, class... Args > /* unspecified */ make_unique( Args&&... args ) = delete; |
(3) | (since C++14) (only for array types with known bound) |
template< class T > unique_ptr<T> make_unique_for_overwrite( ); |
(4) | (since C++20) (only for non-array types) |
template< class T > unique_ptr<T> make_unique_for_overwrite( std::size_t size ); |
(5) | (since C++20) (only for array types with unknown bound) |
template< class T, class... Args > /* unspecified */ make_unique_for_overwrite( Args&&... args ) = delete; |
(6) | (since C++20) (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 participates in overload resolution only if T
is not an array type. The function is equivalent to:
unique_ptr<T>(new T(std::forward<Args>(args)...))
2) Constructs an array of unknown bound
T
. This overload participates in overload resolution only if T
is an array of unknown bound. The function is equivalent to:
unique_ptr<T>(new typename std::remove_extent<T>::type[size]())
3,6) Construction of arrays of known bound is disallowed.
4) Same as (1), except that the object is default-initialized. This overload participates in overload resolution only if
T
is not an array type. The function is equivalent to:
unique_ptr<T>(new T)
5) Same as (2), except that the array is default-initialized. This overload participates in overload resolution only if
T
is an array of unknown bound. The function is equivalent to:
unique_ptr<T>(new typename std::remove_extent<T>::type[size])
Contents |
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
// note: this implementation does not disable this overload for array types template<typename T, typename... Args> std::unique_ptr<T> make_unique(Args&&... args) { return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); } |
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
#include <iostream> #include <memory> struct Vec3 { int x, y, z; Vec3() : x(0), y(0), z(0) { } Vec3(int x, int y, int z) :x(x), y(y), z(z) { } friend std::ostream& operator<<(std::ostream& os, Vec3& v) { return os << '{' << "x:" << v.x << " y:" << v.y << " z:" << v.z << '}'; } }; int main() { // Use the default constructor. std::unique_ptr<Vec3> v1 = std::make_unique<Vec3>(); // Use the constructor that matches these arguments std::unique_ptr<Vec3> v2 = std::make_unique<Vec3>(0, 1, 2); // Create a unique_ptr to an array of 5 elements std::unique_ptr<Vec3[]> v3 = std::make_unique<Vec3[]>(5); std::cout << "make_unique<Vec3>(): " << *v1 << '\n' << "make_unique<Vec3>(0,1,2): " << *v2 << '\n' << "make_unique<Vec3[]>(5): " << '\n'; for (int i = 0; i < 5; i++) { std::cout << " " << v3[i] << '\n'; } }
Output:
make_unique<Vec3>(): {x:0 y:0 z:0} make_unique<Vec3>(0,1,2): {x:0 y:1 z:2} make_unique<Vec3[]>(5): {x:0 y:0 z:0} {x:0 y:0 z:0} {x:0 y:0 z:0} {x:0 y:0 z:0} {x:0 y:0 z:0}
See also
constructs a new unique_ptr (public member function) | |
creates a shared pointer that manages a new object (function template) |