[unique.ptr.single.ctor]

# 20 Memory management library [[mem]](./#mem)

## 20.3 Smart pointers [[smartptr]](smartptr#unique.ptr.single.ctor)

### 20.3.1 Unique-ownership pointers [[unique.ptr]](unique.ptr#single.ctor)

#### 20.3.1.3 unique_ptr for single objects [[unique.ptr.single]](unique.ptr.single#ctor)

#### 20.3.1.3.2 Constructors [unique.ptr.single.ctor]

[ð](#lib:unique_ptr,constructor)

`constexpr unique_ptr() noexcept;
constexpr unique_ptr(nullptr_t) noexcept;
`

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2456)

*Constraints*: is_pointer_v<deleter_type> is false andis_default_constructible_v<deleter_type> is true[.](#1.sentence-1)

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2461)

*Preconditions*: D meets the [*Cpp17DefaultConstructible*](utility.arg.requirements#:Cpp17DefaultConstructible "16.4.4.2 Template argument requirements [utility.arg.requirements]") requirements (Table [30](utility.arg.requirements#tab:cpp17.defaultconstructible "Table 30: Cpp17DefaultConstructible requirements")),
and that construction does not throw an exception[.](#2.sentence-1)

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2466)

*Effects*: Constructs a unique_ptr object that owns
nothing, value-initializing the stored pointer and the stored deleter[.](#3.sentence-1)

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2471)

*Postconditions*: get() == nullptr[.](#4.sentence-1)

get_deleter() returns a reference to the stored deleter[.](#4.sentence-2)

[ð](#lib:unique_ptr,constructor_)

`constexpr explicit unique_ptr(type_identity_t<pointer> p) noexcept;
`

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2483)

*Constraints*: is_pointer_v<deleter_type> is false andis_default_constructible_v<deleter_type> is true[.](#5.sentence-1)

[6](#6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2488)

*Preconditions*: D meets the [*Cpp17DefaultConstructible*](utility.arg.requirements#:Cpp17DefaultConstructible "16.4.4.2 Template argument requirements [utility.arg.requirements]") requirements (Table [30](utility.arg.requirements#tab:cpp17.defaultconstructible "Table 30: Cpp17DefaultConstructible requirements")),
and that construction does not throw an exception[.](#6.sentence-1)

[7](#7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2493)

*Effects*: Constructs a unique_ptr which ownsp, initializing the stored pointer with p and
value-initializing the stored deleter[.](#7.sentence-1)

[8](#8)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2499)

*Postconditions*: get() == p[.](#8.sentence-1)

get_deleter() returns a reference to the stored deleter[.](#8.sentence-2)

[ð](#lib:unique_ptr,constructor__)

`constexpr unique_ptr(type_identity_t<pointer> p, const D& d) noexcept;
constexpr unique_ptr(type_identity_t<pointer> p, remove_reference_t<D>&& d) noexcept;
`

[9](#9)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2512)

*Constraints*: is_constructible_v<D, decltype(d)> is true[.](#9.sentence-1)

[10](#10)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2516)

*Preconditions*: For the first constructor, if D is not a reference type,D meets the [*Cpp17CopyConstructible*](utility.arg.requirements#:Cpp17CopyConstructible "16.4.4.2 Template argument requirements [utility.arg.requirements]") requirements and
such construction does not exit via an exception[.](#10.sentence-1)

For the second constructor, if D is not a reference type,D meets the [*Cpp17MoveConstructible*](utility.arg.requirements#:Cpp17MoveConstructible "16.4.4.2 Template argument requirements [utility.arg.requirements]") requirements and
such construction does not exit via an exception[.](#10.sentence-2)

[11](#11)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2525)

*Effects*: Constructs a unique_ptr object which owns p, initializing
the stored pointer with p and initializing the deleter
from std​::​forward<decltype(d)>(d)[.](#11.sentence-1)

[12](#12)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2531)

*Postconditions*: get() == p[.](#12.sentence-1)

get_deleter() returns a reference to the stored
deleter[.](#12.sentence-2)

If D is a reference type then get_deleter() returns a reference to the lvalue d[.](#12.sentence-3)

[13](#13)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2538)

*Remarks*: If D is a reference type,
the second constructor is defined as deleted[.](#13.sentence-1)

[14](#14)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2543)

[*Example [1](#example-1)*: D d;
unique_ptr<int, D> p1(new int, D()); // D must be [*Cpp17MoveConstructible*](utility.arg.requirements#:Cpp17MoveConstructible "16.4.4.2 Template argument requirements [utility.arg.requirements]") unique_ptr<int, D> p2(new int, d); // D must be [*Cpp17CopyConstructible*](utility.arg.requirements#:Cpp17CopyConstructible "16.4.4.2 Template argument requirements [utility.arg.requirements]") unique_ptr<int, D&> p3(new int, d); // p3 holds a reference to d unique_ptr<int, const D&> p4(new int, D()); // error: rvalue deleter object combined// with reference deleter type â *end example*]

[ð](#lib:unique_ptr,constructor___)

`constexpr unique_ptr(unique_ptr&& u) noexcept;
`

[15](#15)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2562)

*Constraints*: is_move_constructible_v<D> is true[.](#15.sentence-1)

[16](#16)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2566)

*Preconditions*: If D is not a reference type,D meets the [*Cpp17MoveConstructible*](utility.arg.requirements#:Cpp17MoveConstructible "16.4.4.2 Template argument requirements [utility.arg.requirements]") requirements (Table [31](utility.arg.requirements#tab:cpp17.moveconstructible "Table 31: Cpp17MoveConstructible requirements"))[.](#16.sentence-1)

Construction
of the deleter from an rvalue of type D does not
throw an exception[.](#16.sentence-2)

[17](#17)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2575)

*Effects*: Constructs a unique_ptr fromu[.](#17.sentence-1)

If D is a reference type, this
deleter is copy constructed from u's deleter; otherwise, this
deleter is move constructed from u's deleter[.](#17.sentence-2)

[*Note [1](#note-1)*:

The
construction of the deleter can be implemented with std​::​forward<D>[.](#17.sentence-3)

â *end note*]

[18](#18)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2586)

*Postconditions*: get() yields the value u.get() yielded before the construction[.](#18.sentence-1)

u.get() == nullptr[.](#18.sentence-2)

get_deleter() returns a reference
to the stored deleter that was constructed fromu.get_deleter()[.](#18.sentence-3)

If D is a reference type thenget_deleter() and u.get_deleter() both reference
the same lvalue deleter[.](#18.sentence-4)

[ð](#lib:unique_ptr,constructor____)

`template<class U, class E> constexpr unique_ptr(unique_ptr<U, E>&& u) noexcept;
`

[19](#19)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2603)

*Constraints*: 

- [(19.1)](#19.1)

unique_ptr<U, E>​::​pointer is implicitly convertible to pointer,

- [(19.2)](#19.2)

U is not an array type, and

- [(19.3)](#19.3)

either D is a reference type and E is the same type as D, orD is not a reference type and E is implicitly convertible to D[.](#19.sentence-1)

[20](#20)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2612)

*Preconditions*: If E is not a reference type,
construction of the deleter from an rvalue of type E is well-formed and does not throw an exception[.](#20.sentence-1)

Otherwise, E is a reference type and
construction of the deleter from an lvalue of type E is well-formed and does not throw an exception[.](#20.sentence-2)

[21](#21)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2621)

*Effects*: Constructs a unique_ptr from u[.](#21.sentence-1)

If E is a reference type, this deleter is copy constructed fromu's deleter; otherwise, this deleter is move constructed from u's
deleter[.](#21.sentence-2)

[*Note [2](#note-2)*:

The deleter constructor can be implemented withstd​::​forward<E>[.](#21.sentence-3)

â *end note*]

[22](#22)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2632)

*Postconditions*: get() yields the value u.get() yielded before the construction[.](#22.sentence-1)

u.get() == nullptr[.](#22.sentence-2)

get_deleter() returns a reference
to the stored deleter that was constructed fromu.get_deleter()[.](#22.sentence-3)
