[unique.ptr.single.general]

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

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

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

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

#### 20.3.1.3.1 General [unique.ptr.single.general]

[ð](#lib:unique_ptr)

namespace std {template<class T, class D = default_delete<T>> class unique_ptr {public:using pointer = *see below*; using element_type = T; using deleter_type = D; // [[unique.ptr.single.ctor]](unique.ptr.single.ctor "20.3.1.3.2 Constructors"), constructorsconstexpr unique_ptr() noexcept; constexpr explicit unique_ptr(type_identity_t<pointer> p) noexcept; constexpr unique_ptr(type_identity_t<pointer> p, *see below* d1) noexcept; constexpr unique_ptr(type_identity_t<pointer> p, *see below* d2) noexcept; constexpr unique_ptr(unique_ptr&& u) noexcept; constexpr unique_ptr(nullptr_t) noexcept; template<class U, class E>constexpr unique_ptr(unique_ptr<U, E>&& u) noexcept; // [[unique.ptr.single.dtor]](unique.ptr.single.dtor "20.3.1.3.3 Destructor"), destructorconstexpr ~unique_ptr(); // [[unique.ptr.single.asgn]](unique.ptr.single.asgn "20.3.1.3.4 Assignment"), assignmentconstexpr unique_ptr& operator=(unique_ptr&& u) noexcept; template<class U, class E>constexpr unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept; constexpr unique_ptr& operator=(nullptr_t) noexcept; // [[unique.ptr.single.observers]](unique.ptr.single.observers "20.3.1.3.5 Observers"), observersconstexpr add_lvalue_reference_t<T> operator*() const noexcept(*see below*); constexpr pointer operator->() const noexcept; constexpr pointer get() const noexcept; constexpr deleter_type& get_deleter() noexcept; constexpr const deleter_type& get_deleter() const noexcept; constexpr explicit operator bool() const noexcept; // [[unique.ptr.single.modifiers]](unique.ptr.single.modifiers "20.3.1.3.6 Modifiers"), modifiersconstexpr pointer release() noexcept; constexpr void reset(pointer p = pointer()) noexcept; constexpr void swap(unique_ptr& u) noexcept; // disable copy from lvalue unique_ptr(const unique_ptr&) = delete;
 unique_ptr& operator=(const unique_ptr&) = delete; };}

[1](#1)

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

A program that instantiates the definition of unique_ptr<T, D> is ill-formed if T* is an invalid type[.](#1.sentence-1)

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

This prevents the instantiation of specializations such asunique_ptr<T&, D> and unique_ptr<int() const, D>[.](#1.sentence-2)

â *end note*]

[2](#2)

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

The default type for the template parameter D isdefault_delete[.](#2.sentence-1)

A client-supplied template argumentD shall be a function
object type ([[function.objects]](function.objects "22.10 Function objects")), lvalue reference to function, or
lvalue reference to function object type
for which, given
a value d of type D and a valueptr of type unique_ptr<T, D>​::​pointer, the expressiond(ptr) is valid and has the effect of disposing of the
pointer as appropriate for that deleter[.](#2.sentence-2)

[3](#3)

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

If the deleter's type D is not a reference type, D shall meet
the [*Cpp17Destructible*](utility.arg.requirements#:Cpp17Destructible "16.4.4.2 Template argument requirements [utility.arg.requirements]") requirements (Table [35](utility.arg.requirements#tab:cpp17.destructible "Table 35: Cpp17Destructible requirements"))[.](#3.sentence-1)

[4](#4)

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

If the [*qualified-id*](expr.prim.id.qual#nt:qualified-id "7.5.5.3 Qualified names [expr.prim.id.qual]") remove_reference_t<D>​::​pointer is valid and denotes a
type ([[temp.deduct]](temp.deduct "13.10.3 Template argument deduction")), then unique_ptr<T,
D>​::​pointer shall be a synonym for remove_reference_t<D>​::​pointer[.](#4.sentence-1)

Otherwiseunique_ptr<T, D>​::​pointer shall be a synonym for element_type*[.](#4.sentence-2)

The type unique_ptr<T,
D>​::​pointer shall
meet the [*Cpp17NullablePointer*](nullablepointer.requirements#:Cpp17NullablePointer "16.4.4.4 Cpp17NullablePointer requirements [nullablepointer.requirements]") requirements (Table [36](nullablepointer.requirements#tab:cpp17.nullablepointer "Table 36: Cpp17NullablePointer requirements"))[.](#4.sentence-3)

[5](#5)

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

[*Example [1](#example-1)*:

Given an allocator type X ([[allocator.requirements.general]](allocator.requirements.general "16.4.4.6.1 General")) and
letting A be a synonym for allocator_traits<X>, the types A​::​pointer,A​::​const_pointer, A​::​void_pointer, and A​::​const_void_pointer may be used as unique_ptr<T, D>​::​pointer[.](#5.sentence-1)

â *end example*]
