269 lines
8.8 KiB
Markdown
269 lines
8.8 KiB
Markdown
[unique.ptr.runtime]
|
||
|
||
# 20 Memory management library [[mem]](./#mem)
|
||
|
||
## 20.3 Smart pointers [[smartptr]](smartptr#unique.ptr.runtime)
|
||
|
||
### 20.3.1 Unique-ownership pointers [[unique.ptr]](unique.ptr#runtime)
|
||
|
||
#### 20.3.1.4 unique_ptr for array objects with a runtime length [unique.ptr.runtime]
|
||
|
||
#### [20.3.1.4.1](#general) General [[unique.ptr.runtime.general]](unique.ptr.runtime.general)
|
||
|
||
[ð](#lib:unique_ptr)
|
||
|
||
namespace std {template<class T, class D> class unique_ptr<T[], D> {public:using pointer = *see below*; using element_type = T; using deleter_type = D; // [[unique.ptr.runtime.ctor]](#ctor "20.3.1.4.2 Constructors"), constructorsconstexpr unique_ptr() noexcept; template<class U> constexpr explicit unique_ptr(U p) noexcept; template<class U> constexpr unique_ptr(U p, *see below* d) noexcept; template<class U> constexpr unique_ptr(U p, *see below* d) noexcept; constexpr unique_ptr(unique_ptr&& u) noexcept; template<class U, class E>constexpr unique_ptr(unique_ptr<U, E>&& u) noexcept; constexpr unique_ptr(nullptr_t) noexcept; // destructorconstexpr ~unique_ptr(); // 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.runtime.observers]](#observers "20.3.1.4.4 Observers"), observersconstexpr T& operator[](size_t i) const; 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.runtime.modifiers]](#modifiers "20.3.1.4.5 Modifiers"), modifiersconstexpr pointer release() noexcept; template<class U> constexpr void reset(U p) noexcept; constexpr void reset(nullptr_t = nullptr) 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](#general-1)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2950)
|
||
|
||
A specialization for array types is provided with a slightly altered
|
||
interface[.](#general-1.sentence-1)
|
||
|
||
- [(1.1)](#general-1.1)
|
||
|
||
Conversions between different types ofunique_ptr<T[], D> that would be disallowed for the corresponding pointer-to-array types,
|
||
and conversions to or from the non-array forms ofunique_ptr, produce an ill-formed program[.](#general-1.1.sentence-1)
|
||
|
||
- [(1.2)](#general-1.2)
|
||
|
||
Pointers to types derived from T are
|
||
rejected by the constructors, and by reset[.](#general-1.2.sentence-1)
|
||
|
||
- [(1.3)](#general-1.3)
|
||
|
||
The observers operator* andoperator-> are not provided[.](#general-1.3.sentence-1)
|
||
|
||
- [(1.4)](#general-1.4)
|
||
|
||
The indexing observer operator[] is provided[.](#general-1.4.sentence-1)
|
||
|
||
- [(1.5)](#general-1.5)
|
||
|
||
The default deleter will call delete[][.](#general-1.5.sentence-1)
|
||
|
||
[2](#general-2)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2972)
|
||
|
||
Descriptions are provided below only for members that
|
||
differ from the primary template[.](#general-2.sentence-1)
|
||
|
||
[3](#general-3)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2976)
|
||
|
||
The template argument T shall be a complete type[.](#general-3.sentence-1)
|
||
|
||
#### [20.3.1.4.2](#ctor) Constructors [[unique.ptr.runtime.ctor]](unique.ptr.runtime.ctor)
|
||
|
||
[ð](#lib:unique_ptr,constructor)
|
||
|
||
`template<class U> constexpr explicit unique_ptr(U p) noexcept;
|
||
`
|
||
|
||
[1](#ctor-1)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2987)
|
||
|
||
This constructor behaves the same as
|
||
the constructor in the primary template that
|
||
takes a single parameter of type pointer[.](#ctor-1.sentence-1)
|
||
|
||
[2](#ctor-2)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2992)
|
||
|
||
*Constraints*:
|
||
|
||
- [(2.1)](#ctor-2.1)
|
||
|
||
U is the same type as pointer, or
|
||
|
||
- [(2.2)](#ctor-2.2)
|
||
|
||
pointer is the same type as element_type*,U is a pointer type V*, andV(*)[] is convertible to element_type(*)[][.](#ctor-2.sentence-1)
|
||
|
||
[ð](#lib:unique_ptr,constructor_)
|
||
|
||
`template<class U> constexpr unique_ptr(U p, see below d) noexcept;
|
||
template<class U> constexpr unique_ptr(U p, see below d) noexcept;
|
||
`
|
||
|
||
[3](#ctor-3)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L3009)
|
||
|
||
These constructors behave the same as
|
||
the constructors in the primary template that
|
||
take a parameter of type pointer and a second parameter[.](#ctor-3.sentence-1)
|
||
|
||
[4](#ctor-4)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L3014)
|
||
|
||
*Constraints*:
|
||
|
||
- [(4.1)](#ctor-4.1)
|
||
|
||
U is the same type as pointer,
|
||
|
||
- [(4.2)](#ctor-4.2)
|
||
|
||
U is nullptr_t, or
|
||
|
||
- [(4.3)](#ctor-4.3)
|
||
|
||
pointer is the same type as element_type*, U is a pointer type V*, and V(*)[] is convertible to element_type(*)[][.](#ctor-4.sentence-1)
|
||
|
||
[ð](#lib:unique_ptr,constructor__)
|
||
|
||
`template<class U, class E> constexpr unique_ptr(unique_ptr<U, E>&& u) noexcept;
|
||
`
|
||
|
||
[5](#ctor-5)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L3031)
|
||
|
||
This constructor behaves the same as in the primary template[.](#ctor-5.sentence-1)
|
||
|
||
[6](#ctor-6)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L3034)
|
||
|
||
*Constraints*: Where UP is unique_ptr<U, E>:
|
||
|
||
- [(6.1)](#ctor-6.1)
|
||
|
||
U is an array type, and
|
||
|
||
- [(6.2)](#ctor-6.2)
|
||
|
||
pointer is the same type as element_type*, and
|
||
|
||
- [(6.3)](#ctor-6.3)
|
||
|
||
UP::pointer is the same type as UP::element_type*, and
|
||
|
||
- [(6.4)](#ctor-6.4)
|
||
|
||
UP::element_type(*)[] is convertible to element_type(*)[], and
|
||
|
||
- [(6.5)](#ctor-6.5)
|
||
|
||
either D is a reference type and E is the same type as D,
|
||
or D is not a reference type and E is implicitly convertible to D[.](#ctor-6.sentence-1)
|
||
|
||
[*Note [1](#ctor-note-1)*:
|
||
|
||
This replaces the *Constraints*: specification of the primary template[.](#ctor-6.sentence-2)
|
||
|
||
â *end note*]
|
||
|
||
#### [20.3.1.4.3](#asgn) Assignment [[unique.ptr.runtime.asgn]](unique.ptr.runtime.asgn)
|
||
|
||
[ð](#lib:operator=,unique_ptr)
|
||
|
||
`template<class U, class E> constexpr unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
|
||
`
|
||
|
||
[1](#asgn-1)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L3059)
|
||
|
||
This operator behaves the same as in the primary template[.](#asgn-1.sentence-1)
|
||
|
||
[2](#asgn-2)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L3062)
|
||
|
||
*Constraints*: Where UP is unique_ptr<U, E>:
|
||
|
||
- [(2.1)](#asgn-2.1)
|
||
|
||
U is an array type, and
|
||
|
||
- [(2.2)](#asgn-2.2)
|
||
|
||
pointer is the same type as element_type*, and
|
||
|
||
- [(2.3)](#asgn-2.3)
|
||
|
||
UP::pointer is the same type as UP::element_type*, and
|
||
|
||
- [(2.4)](#asgn-2.4)
|
||
|
||
UP::element_type(*)[] is convertible to element_type(*)[], and
|
||
|
||
- [(2.5)](#asgn-2.5)
|
||
|
||
is_assignable_v<D&, E&&> is true[.](#asgn-2.sentence-1)
|
||
|
||
[*Note [1](#asgn-note-1)*:
|
||
|
||
This replaces the *Constraints*: specification of the primary template[.](#asgn-2.sentence-2)
|
||
|
||
â *end note*]
|
||
|
||
#### [20.3.1.4.4](#observers) Observers [[unique.ptr.runtime.observers]](unique.ptr.runtime.observers)
|
||
|
||
[ð](#lib:operator%5b%5d,unique_ptr)
|
||
|
||
`constexpr T& operator[](size_t i) const;
|
||
`
|
||
|
||
[1](#observers-1)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L3086)
|
||
|
||
*Preconditions*: i < the
|
||
number of elements in the array to which
|
||
the stored pointer points[.](#observers-1.sentence-1)
|
||
|
||
[2](#observers-2)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L3092)
|
||
|
||
*Returns*: get()[i][.](#observers-2.sentence-1)
|
||
|
||
#### [20.3.1.4.5](#modifiers) Modifiers [[unique.ptr.runtime.modifiers]](unique.ptr.runtime.modifiers)
|
||
|
||
[ð](#lib:reset,unique_ptr)
|
||
|
||
`constexpr void reset(nullptr_t p = nullptr) noexcept;
|
||
`
|
||
|
||
[1](#modifiers-1)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L3105)
|
||
|
||
*Effects*: Equivalent to reset(pointer())[.](#modifiers-1.sentence-1)
|
||
|
||
[ð](#lib:reset,unique_ptr_)
|
||
|
||
`template<class U> constexpr void reset(U p) noexcept;
|
||
`
|
||
|
||
[2](#modifiers-2)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L3116)
|
||
|
||
This function behaves the same as
|
||
the reset member of the primary template[.](#modifiers-2.sentence-1)
|
||
|
||
[3](#modifiers-3)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L3120)
|
||
|
||
*Constraints*:
|
||
|
||
- [(3.1)](#modifiers-3.1)
|
||
|
||
U is the same type as pointer, or
|
||
|
||
- [(3.2)](#modifiers-3.2)
|
||
|
||
pointer is the same type as element_type*, U is a pointer type V*, and V(*)[] is convertible to element_type(*)[][.](#modifiers-3.sentence-1)
|