std::unique_ptr<T,Deleter>::unique_ptr (3) - Linux Manuals
std::unique_ptr<T,Deleter>::unique_ptr: std::unique_ptr<T,Deleter>::unique_ptr
NAME
std::unique_ptr<T,Deleter>::unique_ptr - std::unique_ptr<T,Deleter>::unique_ptr
Synopsis
members of the primary template, unique_ptr<T>
constexpr unique_ptr() noexcept; (1)
constexpr unique_ptr( nullptr_t ) noexcept;
explicit unique_ptr( pointer p ) noexcept; (2)
unique_ptr( pointer p, /* see below */ d1 ) noexcept; (3)
unique_ptr( pointer p, /* see below */ d2 ) noexcept; (4)
unique_ptr( unique_ptr&& u ) noexcept; (5)
template< class U, class E > (6)
unique_ptr( unique_ptr<U, E>&& u ) noexcept;
template< class U > (7) (removed in C++17)
unique_ptr( std::auto_ptr<U>&& u ) noexcept;
members of the specialization for arrays, unique_ptr<T[]>
constexpr unique_ptr() noexcept; (1)
constexpr unique_ptr( nullptr_t ) noexcept;
explicit unique_ptr( pointer p ) noexcept; (2) (until C++17)
template<class U> explicit unique_ptr( U p ) noexcept; (2) (since C++17)
unique_ptr( pointer p, /* see below */ d1 ) noexcept; (3) (until C++17)
template<class U> unique_ptr( U p, /* see below */ d1 ) noexcept; (3) (since C++17)
unique_ptr( pointer p, /* see below */ d2 ) noexcept; (4) (until C++17)
template<class U> unique_ptr( U p, /* see below */ d2 ) noexcept; (4) (since C++17)
unique_ptr( unique_ptr&& u ) noexcept; (5)
template< class U, class E > (6) (since C++17)
unique_ptr( unique_ptr<U, E>&& u ) noexcept;
1) Constructs a std::unique_ptr that owns nothing. Value-initializes the stored pointer and the stored deleter. Requires that Deleter is DefaultConstructible and that construction does not throw an exception.
This constructor is ill-formed if Deleter is of pointer or reference type. (until C++17)
These overloads only participate in overload resolution if std::is_default_constructible<Deleter>::value is true and Deleter is not a pointer type. (since C++17)
2) Constructs a std::unique_ptr which owns p, initializing the stored pointer with p and value-initializing the stored deleter. Requires that Deleter is DefaultConstructible and that construction does not throw an exception.
This constructor is ill-formed if Deleter is of pointer or reference type. (until C++17)
This overload only participates in overload resolution if std::is_default_constructible<Deleter>::value is true and Deleter is not a pointer type. The program is ill-formed if this constructor is selected by class_template_argument_deduction. (since C++17)
3-4) Constructs a std::unique_ptr object which owns p, initializing the stored pointer with p and initializing a deleter D as below (depends upon whether D is a reference type)
a) If D is non-reference type A, then the signatures are:
unique_ptr(pointer p, const A& d) noexcept; (1) (requires that Deleter is nothrow-CopyConstructible)
unique_ptr(pointer p, A&& d) noexcept; (2) (requires that Deleter is nothrow-MoveConstructible)
b) If D is an lvalue-reference type A&, then the signatures are:
unique_ptr(pointer p, A& d) noexcept; (1)
unique_ptr(pointer p, A&& d); (2)
c) If D is an lvalue-reference type const A&, then the signatures are:
unique_ptr(pointer p, const A& d) noexcept; (1)
unique_ptr(pointer p, const A&& d); (2)
In all cases the deleter is initialized from std::forward<decltype(d)>(d).
If D is a reference type and the second overload is chosen, the program is ill-formed. (until C++17)
If D is a reference type, the second overload is defined as deleted. These overloads only participate in overload resolution if std::is_constructible<D, decltype(d)>::value is true. The program is ill-formed if either of these two constructors is selected by class_template_argument_deduction. (since C++17)
2-4) in the specialization for arrays behave the same as the constructors that take a pointer parameter in the primary template except that they additionally do not participate in overload resolution unless one of the following is true:
* U is the same type as pointer, or (since C++17)
* U is std::nullptr_t, or
* pointer is the same type as element_type* and U is some pointer type V* such that V(*)[] is implicitly convertible to element_type(*)[].
5) Constructs a unique_ptr by transferring ownership from u to *this. If Deleter is not a reference type, requires that it is nothrow-MoveConstructible (if Deleter is a reference, get_deleter() and u.get_deleter() after move construction reference the same value)
6) Constructs a unique_ptr by transferring ownership from u to *this, where u is constructed with a specified deleter (E). It depends upon whether E is a reference type, as following:
a) if E is a reference type, this deleter is copy constructed from u's deleter (requires that this construction does not throw)
b) if E is a non-reference type, this deleter is move constructed from u's deleter (requires that this construction does not throw)
This constructor only participates in overload resolution if all of the following is true:
a) unique_ptr<U, E>::pointer is implicitly convertible to pointer
b) U is not an array type
c) Either Deleter is a reference type and E is the same type as D, or Deleter is not a reference type and E is implicitly convertible to D
6) in the specialization for arrays behaves the same as in the primary template, except that it will only participate in overload resolution if all of the following is true
* U is an array type
* pointer is the same type as element_type* (since C++17)
* unique_ptr<U,E>::pointer is the same type as unique_ptr<U,E>::element_type*
* unique_ptr<U,E>::element_type(*)[] is convertible to element_type(*)[]
* either Deleter is a reference type and E is the same type as Deleter, or Deleter is not a reference type and E is implicitly convertible to Deleter.
7) Constructs a unique_ptr where the stored pointer is initialized with u.release() and the stored deleter is value-initialized. This constructor only participates in overload resolution if U* is implicitly convertible to T* and Deleter is the same type as std::default_delete<T>.
Parameters
p - a pointer to an object to manage
d1,d2 - a deleter to use to destroy the object
u - another smart pointer to acquire the ownership from
Notes
Instead of using the overload (2) together with new, it is often a better idea to use std::make_unique<T>.
std::unique_ptr<Derived> is implicitly convertible to std::unique_ptr<Base> through the overload (6) (because both the managed pointer and std::default_delete are implicitly convertible)
Because the default constructor is constexpr, static unique_ptrs are initialized as part of static_non-local_initialization, before any dynamic non-local initialization begins. This makes it safe to use a unique_ptr in a constructor of any static object.
There is no class_template_argument_deduction from pointer type because it is impossible to distinguish a pointer obtained from array and non-array forms of new (since C++17)
Example
// Run this code