public member function
<memory>

std::unique_ptr::unique_ptr

default (1)
constexpr unique_ptr() noexcept;
from null pointer (2)
constexpr unique_ptr (nullptr_t) noexcept : unique_ptr() {}
from pointer (3)
explicit unique_ptr (pointer p) noexcept;
from pointer + lvalue deleter (4)
unique_ptr (pointer p,    typename conditional<is_reference<D>::value,D,const D&> del) noexcept;
from pointer + rvalue deleter (5)
unique_ptr (pointer p,    typename remove_reference<D>::type&& del) noexcept;
move (6)
unique_ptr (unique_ptr&& x) noexcept;
move-cast (7)
template <class U, class E>  unique_ptr (unique_ptr<U,E>&& x) noexcept;
move from auto_ptr (8)
template <class U>  unique_ptr (auto_ptr<U>&& x) noexcept;
copy (deleted!) (9)
unique_ptr (const unique_ptr&) = delete;
Construct unique_ptr
Constructs a unique_ptr object, depending on the signature used:

default constructor (1), and (2)
The object is empty (owns nothing), with value-initialized stored pointer and stored deleter.
construct from pointer (3)
The object takes ownership of p, initializing its stored pointer to p and value-initializing its stored deleter.
construct from pointer + lvalue deleter (4)
The object takes ownership of p, initializing its stored pointer to p, and using a copy of d as deleter (unless D is a reference type, in which case, d is used directly).
construct from pointer + rvalue deleter (5)
The object takes ownership of p, initializing its stored pointer to p and moving d into the object to be used as deleter.
move constructors (6) and (7)
The object acquires the content managed by x, moving into the object both its stored pointer (of which it takes ownership) and its stored deleter (unless x's deleter type is a reference, in which case the deleter is copied instead of moved).
construct by moving auto_ptr (8)
The object acquires the content managed by x, initializing its stored pointer as if by calling x.release() (taking ownership of the pointer), and value-initializing its stored deleter.
copy construction (9)
Copy construction is disabled for objects of type unique_ptr (see move constructors, 6 and 7).

The specialization of unique_ptr for arrays with runtime length (unique_ptr<T[],D>) does not support constructors (7) and (8), and does not accept types convertible to pointer (except pointer itself) as argument p (most notably pointers to types derived from element_type are not accepted).

Parameters

p
Pointer whose ownership is taken over by the object.
This pointer value shall not be already managed by any other managed pointer (i.e., this value shall not come from calling member get on a managed pointer).
pointer is a member type, defined as the pointer type that points to the type of object managed.
del
Deleter object to be used to release the owned object.
D refers to unique_ptr's second template parameter, which describes its deleter type.
x
A unique_ptr or auto_ptr object.
U* shall be implicitly convertible to T* (where T is unique_ptr's first template parameter).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// unique_ptr constructor example
#include <iostream>
#include <memory>

int main () {
  std::default_delete<int> d;
  std::unique_ptr<int> u1;
  std::unique_ptr<int> u2 (nullptr);
  std::unique_ptr<int> u3 (new int);
  std::unique_ptr<int> u4 (new int, d);
  std::unique_ptr<int> u5 (new int, std::default_delete<int>());
  std::unique_ptr<int> u6 (std::move(u5));
  std::unique_ptr<int> u7 (std::move(u6));
  std::unique_ptr<int> u8 (std::auto_ptr<int>(new int));

  std::cout << "u1: " << (u1?"not null":"null") << '\n';
  std::cout << "u2: " << (u2?"not null":"null") << '\n';
  std::cout << "u3: " << (u3?"not null":"null") << '\n';
  std::cout << "u4: " << (u4?"not null":"null") << '\n';
  std::cout << "u5: " << (u5?"not null":"null") << '\n';
  std::cout << "u6: " << (u6?"not null":"null") << '\n';
  std::cout << "u7: " << (u7?"not null":"null") << '\n';
  std::cout << "u8: " << (u8?"not null":"null") << '\n';

  return 0;
}

Output:
u1: null
u2: null
u3: not null
u4: not null
u5: null
u6: null
u7: not null
u8: not null


See also