[unique.ptr.general]

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

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

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

#### 20.3.1.1 General [unique.ptr.general]

[1](#1)

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

A [*unique pointer*](#def:unique_pointer "20.3.1.1 General [unique.ptr.general]") is an object that owns another object and
manages that other object through a pointer[.](#1.sentence-1)

More precisely, a unique pointer
is an object *u* that stores a pointer to a second object *p* and
will dispose of *p* when *u* is itself destroyed (e.g., when
leaving block scope ([[stmt.dcl]](stmt.dcl "8.10 Declaration statement")))[.](#1.sentence-2)

In this context, *u* is said
to [*own*](#def:own "20.3.1.1 General [unique.ptr.general]") p[.](#1.sentence-3)

[2](#2)

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

The mechanism by which *u* disposes of *p* is known as*p*'s associated [*deleter*](#def:deleter "20.3.1.1 General [unique.ptr.general]"), a function object whose correct
invocation results in *p*'s appropriate disposition (typically its deletion)[.](#2.sentence-1)

[3](#3)

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

Let the notation *u.p* denote the pointer stored by *u*, and
let *u.d* denote the associated deleter[.](#3.sentence-1)

Upon request, *u* can[*reset*](#def:reset "20.3.1.1 General [unique.ptr.general]") (replace) *u.p* and *u.d* with another pointer and
deleter, but properly disposes of its owned object via the associated
deleter before such replacement is considered completed[.](#3.sentence-2)

[4](#4)

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

Each object of a type U instantiated from the unique_ptr template
specified in [[unique.ptr]](unique.ptr "20.3.1 Unique-ownership pointers") has the strict ownership semantics, specified above,
of a unique pointer[.](#4.sentence-1)

In partial satisfaction of these semantics, each such U is [*Cpp17MoveConstructible*](utility.arg.requirements#:Cpp17MoveConstructible "16.4.4.2 Template argument requirements [utility.arg.requirements]") and [*Cpp17MoveAssignable*](utility.arg.requirements#:Cpp17MoveAssignable "16.4.4.2 Template argument requirements [utility.arg.requirements]"), but is not[*Cpp17CopyConstructible*](utility.arg.requirements#:Cpp17CopyConstructible "16.4.4.2 Template argument requirements [utility.arg.requirements]") nor [*Cpp17CopyAssignable*](utility.arg.requirements#:Cpp17CopyAssignable "16.4.4.2 Template argument requirements [utility.arg.requirements]")[.](#4.sentence-2)

The template parameter T of unique_ptr may be an incomplete type[.](#4.sentence-3)

[5](#5)

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

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

The uses
of unique_ptr include providing exception safety for
dynamically allocated memory, passing ownership of dynamically allocated
memory to a function, and returning dynamically allocated memory from a
function[.](#5.sentence-1)

â *end note*]
