From Jason Turner

[unique.ptr]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpbes1c021/{from.md → to.md} +224 -139
tmp/tmpbes1c021/{from.md → to.md} RENAMED
@@ -25,24 +25,26 @@ postconditions hold:
25
  - if the pre-transfer *u.d* maintained state, such state has been
26
  transferred to *u2.d*.
27
 
28
  As in the case of a reset, *u2* must properly dispose of its
29
  pre-transfer owned object via the pre-transfer associated deleter before
30
- the ownership transfer is considered complete. A deleter’s state need
31
- never be copied, only moved or swapped as ownership is transferred.
 
 
32
 
33
  Each object of a type `U` instantiated from the `unique_ptr` template
34
  specified in this subclause has the strict ownership semantics,
35
  specified above, of a unique pointer. In partial satisfaction of these
36
  semantics, each such `U` is `MoveConstructible` and `MoveAssignable`,
37
  but is not `CopyConstructible` nor `CopyAssignable`. The template
38
  parameter `T` of `unique_ptr` may be an incomplete type.
39
 
40
- The uses of `unique_ptr` include providing exception safety for
41
- dynamically allocated memory, passing ownership of dynamically allocated
42
- memory to a function, and returning dynamically allocated memory from a
43
- function.
44
 
45
  ``` cpp
46
  namespace std {
47
  template<class T> struct default_delete;
48
  template<class T> struct default_delete<T[]>;
@@ -131,56 +133,65 @@ unless `U*` is implicitly convertible to `T*`.
131
 
132
  ``` cpp
133
  void operator()(T* ptr) const;
134
  ```
135
 
136
- *Effects:* calls `delete` on `ptr`.
137
 
138
  *Remarks:* If `T` is an incomplete type, the program is ill-formed.
139
 
140
  ##### `default_delete<T[]>` <a id="unique.ptr.dltr.dflt1">[[unique.ptr.dltr.dflt1]]</a>
141
 
142
  ``` cpp
143
  namespace std {
144
  template <class T> struct default_delete<T[]> {
145
  constexpr default_delete() noexcept = default;
146
- void operator()(T*) const;
147
- template <class U> void operator()(U*) const = delete;
148
  };
149
  }
150
  ```
151
 
152
  ``` cpp
153
- void operator()(T* ptr) const;
154
  ```
155
 
156
- *Effects:* calls `delete[]` on `ptr`.
 
157
 
158
- *Remarks:* If T is an incomplete type, the program is ill-formed.
 
 
 
 
 
 
 
 
 
 
 
159
 
160
  #### `unique_ptr` for single objects <a id="unique.ptr.single">[[unique.ptr.single]]</a>
161
 
162
  ``` cpp
163
  namespace std {
164
  template <class T, class D = default_delete<T>> class unique_ptr {
165
  public:
166
- typedef see below pointer;
167
- typedef T element_type;
168
- typedef D deleter_type;
169
 
170
  // [unique.ptr.single.ctor], constructors
171
  constexpr unique_ptr() noexcept;
172
  explicit unique_ptr(pointer p) noexcept;
173
  unique_ptr(pointer p, see below d1) noexcept;
174
  unique_ptr(pointer p, see below d2) noexcept;
175
  unique_ptr(unique_ptr&& u) noexcept;
176
- constexpr unique_ptr(nullptr_t) noexcept
177
- : unique_ptr() { }
178
  template <class U, class E>
179
  unique_ptr(unique_ptr<U, E>&& u) noexcept;
180
- template <class U>
181
- unique_ptr(auto_ptr<U>&& u) noexcept;
182
 
183
  // [unique.ptr.single.dtor], destructor
184
  ~unique_ptr();
185
 
186
  // [unique.ptr.single.asgn], assignment
@@ -194,11 +205,11 @@ namespace std {
194
  pointer get() const noexcept;
195
  deleter_type& get_deleter() noexcept;
196
  const deleter_type& get_deleter() const noexcept;
197
  explicit operator bool() const noexcept;
198
 
199
- // [unique.ptr.single.modifiers] modifiers
200
  pointer release() noexcept;
201
  void reset(pointer p = pointer()) noexcept;
202
  void swap(unique_ptr& u) noexcept;
203
 
204
  // disable copy from lvalue
@@ -208,149 +219,148 @@ namespace std {
208
  }
209
  ```
210
 
211
  The default type for the template parameter `D` is `default_delete`. A
212
  client-supplied template argument `D` shall be a function object type (
213
- [[function.objects]]), lvalue-reference to function, or lvalue-reference
214
  to function object type for which, given a value `d` of type `D` and a
215
  value `ptr` of type `unique_ptr<T, D>::pointer`, the expression `d(ptr)`
216
  is valid and has the effect of disposing of the pointer as appropriate
217
  for that deleter.
218
 
219
  If the deleter’s type `D` is not a reference type, `D` shall satisfy the
220
- requirements of `Destructible` (Table  [[destructible]]).
221
 
222
- If the type `remove_reference_t<D>::pointer` exists, then `unique_ptr<T,
 
223
  D>::pointer` shall be a synonym for `remove_reference_t<D>::pointer`.
224
- Otherwise `unique_ptr<T, D>::pointer` shall be a synonym for `T*`. The
225
- type `unique_ptr<T,
226
  D>::pointer` shall satisfy the requirements of `NullablePointer` (
227
  [[nullablepointer.requirements]]).
228
 
229
- Given an allocator type `X` ([[allocator.requirements]]) and letting
230
- `A` be a synonym for `allocator_traits<X>`, the types `A::pointer`,
231
- `A::const_pointer`, `A::void_pointer`, and `A::const_void_pointer` may
232
- be used as `unique_ptr<T, D>::pointer`.
 
233
 
234
  ##### `unique_ptr` constructors <a id="unique.ptr.single.ctor">[[unique.ptr.single.ctor]]</a>
235
 
236
  ``` cpp
237
  constexpr unique_ptr() noexcept;
 
238
  ```
239
 
240
  *Requires:* `D` shall satisfy the requirements of `DefaultConstructible`
241
- (Table  [[defaultconstructible]]), and that construction shall not throw
242
- an exception.
243
 
244
  *Effects:* Constructs a `unique_ptr` object that owns nothing,
245
  value-initializing the stored pointer and the stored deleter.
246
 
247
  *Postconditions:* `get() == nullptr`. `get_deleter()` returns a
248
  reference to the stored deleter.
249
 
250
- *Remarks:* If this constructor is instantiated with a pointer type or
251
- reference type for the template argument `D`, the program is ill-formed.
 
252
 
253
  ``` cpp
254
  explicit unique_ptr(pointer p) noexcept;
255
  ```
256
 
257
  *Requires:* `D` shall satisfy the requirements of `DefaultConstructible`
258
- (Table  [[defaultconstructible]]), and that construction shall not throw
259
- an exception.
260
 
261
  *Effects:* Constructs a `unique_ptr` which owns `p`, initializing the
262
  stored pointer with `p` and value-initializing the stored deleter.
263
 
264
  *Postconditions:* `get() == p`. `get_deleter()` returns a reference to
265
  the stored deleter.
266
 
267
- *Remarks:* If this constructor is instantiated with a pointer type or
268
- reference type for the template argument `D`, the program is ill-formed.
 
 
 
 
269
 
270
  ``` cpp
271
  unique_ptr(pointer p, see below d1) noexcept;
272
  unique_ptr(pointer p, see below d2) noexcept;
273
  ```
274
 
275
  The signature of these constructors depends upon whether `D` is a
276
- reference type. If `D` is non-reference type `A`, then the signatures
277
  are:
278
 
279
  ``` cpp
280
- unique_ptr(pointer p, const A& d);
281
- unique_ptr(pointer p, A&& d);
282
  ```
283
 
284
- If `D` is an lvalue-reference type `A&`, then the signatures are:
285
 
286
  ``` cpp
287
- unique_ptr(pointer p, A& d);
288
- unique_ptr(pointer p, A&& d);
289
  ```
290
 
291
- If `D` is an lvalue-reference type `const A&`, then the signatures are:
292
 
293
  ``` cpp
294
- unique_ptr(pointer p, const A& d);
295
- unique_ptr(pointer p, const A&& d);
296
  ```
297
 
298
- *Requires:*
299
-
300
- - If `D` is not an lvalue-reference type then
301
- - If `d` is an lvalue or `const` rvalue then the first constructor of
302
- this pair will be selected. `D` shall satisfy the requirements of
303
- `CopyConstructible` (Table  [[copyconstructible]]), and the copy
304
- constructor of `D` shall not throw an exception. This `unique_ptr`
305
- will hold a copy of `d`.
306
- - Otherwise, `d` is a non-const rvalue and the second constructor of
307
- this pair will be selected. `D` shall satisfy the requirements of
308
- `MoveConstructible` (Table  [[moveconstructible]]), and the move
309
- constructor of `D` shall not throw an exception. This `unique_ptr`
310
- will hold a value move constructed from `d`.
311
- - Otherwise `D` is an lvalue-reference type. `d` shall be
312
- reference-compatible with one of the constructors. If `d` is an
313
- rvalue, it will bind to the second constructor of this pair and the
314
- program is ill-formed. The diagnostic could be implemented using a
315
- `static_assert` which assures that `D` is not a reference type. Else
316
- `d` is an lvalue and will bind to the first constructor of this pair.
317
- The type which `D` references need not be `CopyConstructible` nor
318
- `MoveConstructible`. This `unique_ptr` will hold a `D` which refers to
319
- the lvalue `d`. `D` may not be an rvalue-reference type.
320
-
321
  *Effects:* Constructs a `unique_ptr` object which owns `p`, initializing
322
- the stored pointer with `p` and initializing the deleter as described
323
- above.
 
 
 
324
 
325
  *Postconditions:* `get() == p`. `get_deleter()` returns a reference to
326
  the stored deleter. If `D` is a reference type then `get_deleter()`
327
  returns a reference to the lvalue `d`.
328
 
 
 
 
 
 
 
 
329
  ``` cpp
330
  D d;
331
  unique_ptr<int, D> p1(new int, D()); // D must be MoveConstructible
332
  unique_ptr<int, D> p2(new int, d); // D must be CopyConstructible
333
  unique_ptr<int, D&> p3(new int, d); // p3 holds a reference to d
334
  unique_ptr<int, const D&> p4(new int, D()); // error: rvalue deleter object combined
335
  // with reference deleter type
336
  ```
337
 
 
 
338
  ``` cpp
339
  unique_ptr(unique_ptr&& u) noexcept;
340
  ```
341
 
342
  *Requires:* If `D` is not a reference type, `D` shall satisfy the
343
- requirements of `MoveConstructible` (Table  [[moveconstructible]]).
344
  Construction of the deleter from an rvalue of type `D` shall not throw
345
  an exception.
346
 
347
  *Effects:* Constructs a `unique_ptr` by transferring ownership from `u`
348
  to `*this`. If `D` is a reference type, this deleter is copy constructed
349
  from `u`’s deleter; otherwise, this deleter is move constructed from
350
- `u`’s deleter. The deleter constructor can be implemented with
351
- `std::forward<D>`.
 
 
352
 
353
  *Postconditions:* `get()` yields the value `u.get()` yielded before the
354
  construction. `get_deleter()` returns a reference to the stored deleter
355
  that was constructed from `u.get_deleter()`. If `D` is a reference type
356
  then `get_deleter()` and `u.get_deleter()` both reference the same
@@ -375,42 +385,30 @@ unless:
375
  is not a reference type and `E` is implicitly convertible to `D`.
376
 
377
  *Effects:* Constructs a `unique_ptr` by transferring ownership from `u`
378
  to `*this`. If `E` is a reference type, this deleter is copy constructed
379
  from `u`’s deleter; otherwise, this deleter is move constructed from
380
- `u`’s deleter. The deleter constructor can be implemented with
381
- `std::forward<E>`.
 
 
382
 
383
  *Postconditions:* `get()` yields the value `u.get()` yielded before the
384
  construction. `get_deleter()` returns a reference to the stored deleter
385
  that was constructed from `u.get_deleter()`.
386
 
387
- ``` cpp
388
- template <class U>
389
- unique_ptr(auto_ptr<U>&& u) noexcept;
390
- ```
391
-
392
- *Effects:* Constructs a `unique_ptr` object, initializing the stored
393
- pointer with `u.release()` and value-initializing the stored deleter.
394
-
395
- *Postconditions:* `get()` yields the value `u.get()` yielded before the
396
- construction. `u.get() == nullptr`. `get_deleter()` returns a reference
397
- to the stored deleter.
398
-
399
- *Remarks:* This constructor shall not participate in overload resolution
400
- unless `U*` is implicitly convertible to `T*` and `D` is the same type
401
- as `default_delete<T>`.
402
-
403
  ##### `unique_ptr` destructor <a id="unique.ptr.single.dtor">[[unique.ptr.single.dtor]]</a>
404
 
405
  ``` cpp
406
  ~unique_ptr();
407
  ```
408
 
409
  *Requires:* The expression `get_deleter()(get())` shall be well formed,
410
- shall have well-defined behavior, and shall not throw exceptions. The
411
- use of `default_delete` requires `T` to be a complete type.
 
 
412
 
413
  *Effects:* If `get() == nullptr` there are no effects. Otherwise
414
  `get_deleter()(get())`.
415
 
416
  ##### `unique_ptr` assignment <a id="unique.ptr.single.asgn">[[unique.ptr.single.asgn]]</a>
@@ -418,11 +416,11 @@ use of `default_delete` requires `T` to be a complete type.
418
  ``` cpp
419
  unique_ptr& operator=(unique_ptr&& u) noexcept;
420
  ```
421
 
422
  *Requires:* If `D` is not a reference type, `D` shall satisfy the
423
- requirements of `MoveAssignable` (Table  [[moveassignable]]) and
424
  assignment of the deleter from an rvalue of type `D` shall not throw an
425
  exception. Otherwise, `D` is a reference type; `remove_reference_t<D>`
426
  shall satisfy the `CopyAssignable` requirements and assignment of the
427
  deleter from an lvalue of type `D` shall not throw an exception.
428
 
@@ -443,12 +441,14 @@ deleter from an lvalue of type `E` shall be well-formed and shall not
443
  throw an exception.
444
 
445
  *Remarks:* This operator shall not participate in overload resolution
446
  unless:
447
 
448
- - `unique_ptr<U, E>::pointer` is implicitly convertible to `pointer` and
449
- - `U` is not an array type.
 
 
450
 
451
  *Effects:* Transfers ownership from `u` to `*this` as if by calling
452
  `reset(u.release())` followed by
453
  `get_deleter() = std::forward<E>(u.get_deleter())`.
454
 
@@ -456,13 +456,13 @@ unless:
456
 
457
  ``` cpp
458
  unique_ptr& operator=(nullptr_t) noexcept;
459
  ```
460
 
461
- *Effects:* `reset()`.
462
 
463
- `get() == nullptr`
464
 
465
  *Returns:* `*this`.
466
 
467
  ##### `unique_ptr` observers <a id="unique.ptr.single.observers">[[unique.ptr.single.observers]]</a>
468
 
@@ -480,11 +480,12 @@ pointer operator->() const noexcept;
480
 
481
  *Requires:* `get() != nullptr`.
482
 
483
  *Returns:* `get()`.
484
 
485
- *Note:* use typically requires that `T` be a complete type.
 
486
 
487
  ``` cpp
488
  pointer get() const noexcept;
489
  ```
490
 
@@ -507,29 +508,33 @@ explicit operator bool() const noexcept;
507
 
508
  ``` cpp
509
  pointer release() noexcept;
510
  ```
511
 
512
- `get() == nullptr`.
513
 
514
  *Returns:* The value `get()` had at the start of the call to `release`.
515
 
516
  ``` cpp
517
  void reset(pointer p = pointer()) noexcept;
518
  ```
519
 
520
  *Requires:* The expression `get_deleter()(get())` shall be well formed,
521
  shall have well-defined behavior, and shall not throw exceptions.
522
 
523
- *Effects:* assigns `p` to the stored pointer, and then if the old value
524
- of the stored pointer, `old_p`, was not equal to `nullptr`, calls
525
- `get_deleter()(old_p)`. The order of these operations is significant
526
- because the call to `get_deleter()` may destroy `*this`.
527
 
528
- *Postconditions:* `get() == p`. The postcondition does not hold if the
529
- call to `get_deleter()` destroys `*this` since `this->get()` is no
530
- longer a valid expression.
 
 
 
 
 
531
 
532
  ``` cpp
533
  void swap(unique_ptr& u) noexcept;
534
  ```
535
 
@@ -544,41 +549,44 @@ deleters of `*this` and `u`.
544
 
545
  ``` cpp
546
  namespace std {
547
  template <class T, class D> class unique_ptr<T[], D> {
548
  public:
549
- typedef see below pointer;
550
- typedef T element_type;
551
- typedef D deleter_type;
552
 
553
  // [unique.ptr.runtime.ctor], constructors
554
  constexpr unique_ptr() noexcept;
555
- explicit unique_ptr(pointer p) noexcept;
556
- unique_ptr(pointer p, see below d) noexcept;
557
- unique_ptr(pointer p, see below d) noexcept;
558
  unique_ptr(unique_ptr&& u) noexcept;
559
- constexpr unique_ptr(nullptr_t) noexcept : unique_ptr() { }
 
 
560
 
561
  // destructor
562
  ~unique_ptr();
563
 
564
  // assignment
565
  unique_ptr& operator=(unique_ptr&& u) noexcept;
 
 
566
  unique_ptr& operator=(nullptr_t) noexcept;
567
 
568
  // [unique.ptr.runtime.observers], observers
569
  T& operator[](size_t i) const;
570
  pointer get() const noexcept;
571
  deleter_type& get_deleter() noexcept;
572
  const deleter_type& get_deleter() const noexcept;
573
  explicit operator bool() const noexcept;
574
 
575
- // [unique.ptr.runtime.modifiers] modifiers
576
  pointer release() noexcept;
577
- void reset(pointer p = pointer()) noexcept;
578
- void reset(nullptr_t) noexcept;
579
- template <class U> void reset(U) = delete;
580
  void swap(unique_ptr& u) noexcept;
581
 
582
  // disable copy from lvalue
583
  unique_ptr(const unique_ptr&) = delete;
584
  unique_ptr& operator=(const unique_ptr&) = delete;
@@ -587,36 +595,92 @@ namespace std {
587
  ```
588
 
589
  A specialization for array types is provided with a slightly altered
590
  interface.
591
 
592
- - Conversions between different types of `unique_ptr<T[], D>` or to or
593
- from the non-array forms of `unique_ptr` produce an ill-formed
594
- program.
 
595
  - Pointers to types derived from `T` are rejected by the constructors,
596
  and by `reset`.
597
  - The observers `operator*` and `operator->` are not provided.
598
  - The indexing observer `operator[]` is provided.
599
  - The default deleter will call `delete[]`.
600
 
601
- Descriptions are provided below only for member functions that have
602
- behavior different from the primary template.
603
 
604
  The template argument `T` shall be a complete type.
605
 
606
  ##### `unique_ptr` constructors <a id="unique.ptr.runtime.ctor">[[unique.ptr.runtime.ctor]]</a>
607
 
608
  ``` cpp
609
- explicit unique_ptr(pointer p) noexcept;
610
- unique_ptr(pointer p, see below d) noexcept;
611
- unique_ptr(pointer p, see below d) noexcept;
612
  ```
613
 
614
- These constructors behave the same as in the primary template except
615
- that they do not accept pointer types which are convertible to
616
- `pointer`. One implementation technique is to create private templated
617
- overloads of these members.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
618
 
619
  ##### `unique_ptr` observers <a id="unique.ptr.runtime.observers">[[unique.ptr.runtime.observers]]</a>
620
 
621
  ``` cpp
622
  T& operator[](size_t i) const;
@@ -628,15 +692,27 @@ stored pointer points.
628
  *Returns:* `get()[i]`.
629
 
630
  ##### `unique_ptr` modifiers <a id="unique.ptr.runtime.modifiers">[[unique.ptr.runtime.modifiers]]</a>
631
 
632
  ``` cpp
633
- void reset(nullptr_t p) noexcept;
634
  ```
635
 
636
  *Effects:* Equivalent to `reset(pointer())`.
637
 
 
 
 
 
 
 
 
 
 
 
 
 
638
  #### `unique_ptr` creation <a id="unique.ptr.create">[[unique.ptr.create]]</a>
639
 
640
  ``` cpp
641
  template <class T, class... Args> unique_ptr<T> make_unique(Args&&... args);
642
  ```
@@ -666,10 +742,13 @@ unless `T` is an array of known bound.
666
 
667
  ``` cpp
668
  template <class T, class D> void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
669
  ```
670
 
 
 
 
671
  *Effects:* Calls `x.swap(y)`.
672
 
673
  ``` cpp
674
  template <class T1, class D1, class T2, class D2>
675
  bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
@@ -687,20 +766,26 @@ template <class T1, class D1, class T2, class D2>
687
  ``` cpp
688
  template <class T1, class D1, class T2, class D2>
689
  bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
690
  ```
691
 
692
- *Requires:* Let `CT` be `common_type<unique_ptr<T1, D1>::pointer,`
693
- `unique_ptr<T2, D2>::pointer>::type`. Then the specialization `less<CT>`
694
- shall be a function object type ([[function.objects]]) that induces a
695
- strict weak ordering ([[alg.sorting]]) on the pointer values.
696
 
697
- *Returns:* `less<CT>()(x.get(), y.get()).`
 
 
 
 
 
 
 
 
 
698
 
699
  *Remarks:* If `unique_ptr<T1, D1>::pointer` is not implicitly
700
- convertible to `CT` or `unique_ptr<T2, D2>::pointer` is not implicitly
701
- convertible to `CT`, the program is ill-formed.
702
 
703
  ``` cpp
704
  template <class T1, class D1, class T2, class D2>
705
  bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
706
  ```
 
25
  - if the pre-transfer *u.d* maintained state, such state has been
26
  transferred to *u2.d*.
27
 
28
  As in the case of a reset, *u2* must properly dispose of its
29
  pre-transfer owned object via the pre-transfer associated deleter before
30
+ the ownership transfer is considered complete.
31
+
32
+ [*Note 1*: A deleter’s state need never be copied, only moved or
33
+ swapped as ownership is transferred. — *end note*]
34
 
35
  Each object of a type `U` instantiated from the `unique_ptr` template
36
  specified in this subclause has the strict ownership semantics,
37
  specified above, of a unique pointer. In partial satisfaction of these
38
  semantics, each such `U` is `MoveConstructible` and `MoveAssignable`,
39
  but is not `CopyConstructible` nor `CopyAssignable`. The template
40
  parameter `T` of `unique_ptr` may be an incomplete type.
41
 
42
+ [*Note 2*: The uses of `unique_ptr` include providing exception safety
43
+ for dynamically allocated memory, passing ownership of dynamically
44
+ allocated memory to a function, and returning dynamically allocated
45
+ memory from a function. — *end note*]
46
 
47
  ``` cpp
48
  namespace std {
49
  template<class T> struct default_delete;
50
  template<class T> struct default_delete<T[]>;
 
133
 
134
  ``` cpp
135
  void operator()(T* ptr) const;
136
  ```
137
 
138
+ *Effects:* Calls `delete` on `ptr`.
139
 
140
  *Remarks:* If `T` is an incomplete type, the program is ill-formed.
141
 
142
  ##### `default_delete<T[]>` <a id="unique.ptr.dltr.dflt1">[[unique.ptr.dltr.dflt1]]</a>
143
 
144
  ``` cpp
145
  namespace std {
146
  template <class T> struct default_delete<T[]> {
147
  constexpr default_delete() noexcept = default;
148
+ template <class U> default_delete(const default_delete<U[]>&) noexcept;
149
+ template <class U> void operator()(U* ptr) const;
150
  };
151
  }
152
  ```
153
 
154
  ``` cpp
155
+ template <class U> default_delete(const default_delete<U[]>& other) noexcept;
156
  ```
157
 
158
+ *Effects:* constructs a `default_delete` object from another
159
+ `default_delete<U[]>` object.
160
 
161
+ *Remarks:* This constructor shall not participate in overload resolution
162
+ unless `U(*)[]` is convertible to `T(*)[]`.
163
+
164
+ ``` cpp
165
+ template <class U> void operator()(U* ptr) const;
166
+ ```
167
+
168
+ *Effects:* Calls `delete[]` on `ptr`.
169
+
170
+ *Remarks:* If `U` is an incomplete type, the program is ill-formed. This
171
+ function shall not participate in overload resolution unless `U(*)[]` is
172
+ convertible to `T(*)[]`.
173
 
174
  #### `unique_ptr` for single objects <a id="unique.ptr.single">[[unique.ptr.single]]</a>
175
 
176
  ``` cpp
177
  namespace std {
178
  template <class T, class D = default_delete<T>> class unique_ptr {
179
  public:
180
+ using pointer = see below;
181
+ using element_type = T;
182
+ using deleter_type = D;
183
 
184
  // [unique.ptr.single.ctor], constructors
185
  constexpr unique_ptr() noexcept;
186
  explicit unique_ptr(pointer p) noexcept;
187
  unique_ptr(pointer p, see below d1) noexcept;
188
  unique_ptr(pointer p, see below d2) noexcept;
189
  unique_ptr(unique_ptr&& u) noexcept;
190
+ constexpr unique_ptr(nullptr_t) noexcept;
 
191
  template <class U, class E>
192
  unique_ptr(unique_ptr<U, E>&& u) noexcept;
 
 
193
 
194
  // [unique.ptr.single.dtor], destructor
195
  ~unique_ptr();
196
 
197
  // [unique.ptr.single.asgn], assignment
 
205
  pointer get() const noexcept;
206
  deleter_type& get_deleter() noexcept;
207
  const deleter_type& get_deleter() const noexcept;
208
  explicit operator bool() const noexcept;
209
 
210
+ // [unique.ptr.single.modifiers], modifiers
211
  pointer release() noexcept;
212
  void reset(pointer p = pointer()) noexcept;
213
  void swap(unique_ptr& u) noexcept;
214
 
215
  // disable copy from lvalue
 
219
  }
220
  ```
221
 
222
  The default type for the template parameter `D` is `default_delete`. A
223
  client-supplied template argument `D` shall be a function object type (
224
+ [[function.objects]]), lvalue reference to function, or lvalue reference
225
  to function object type for which, given a value `d` of type `D` and a
226
  value `ptr` of type `unique_ptr<T, D>::pointer`, the expression `d(ptr)`
227
  is valid and has the effect of disposing of the pointer as appropriate
228
  for that deleter.
229
 
230
  If the deleter’s type `D` is not a reference type, `D` shall satisfy the
231
+ requirements of `Destructible` (Table  [[tab:destructible]]).
232
 
233
+ If the *qualified-id* `remove_reference_t<D>::pointer` is valid and
234
+ denotes a type ([[temp.deduct]]), then `unique_ptr<T,
235
  D>::pointer` shall be a synonym for `remove_reference_t<D>::pointer`.
236
+ Otherwise `unique_ptr<T, D>::pointer` shall be a synonym for
237
+ `element_type*`. The type `unique_ptr<T,
238
  D>::pointer` shall satisfy the requirements of `NullablePointer` (
239
  [[nullablepointer.requirements]]).
240
 
241
+ [*Example 1*: Given an allocator type `X` ([[allocator.requirements]])
242
+ and letting `A` be a synonym for `allocator_traits<X>`, the types
243
+ `A::pointer`, `A::const_pointer`, `A::void_pointer`, and
244
+ `A::const_void_pointer` may be used as
245
+ `unique_ptr<T, D>::pointer`. — *end example*]
246
 
247
  ##### `unique_ptr` constructors <a id="unique.ptr.single.ctor">[[unique.ptr.single.ctor]]</a>
248
 
249
  ``` cpp
250
  constexpr unique_ptr() noexcept;
251
+ constexpr unique_ptr(nullptr_t) noexcept;
252
  ```
253
 
254
  *Requires:* `D` shall satisfy the requirements of `DefaultConstructible`
255
+ (Table  [[tab:defaultconstructible]]), and that construction shall not
256
+ throw an exception.
257
 
258
  *Effects:* Constructs a `unique_ptr` object that owns nothing,
259
  value-initializing the stored pointer and the stored deleter.
260
 
261
  *Postconditions:* `get() == nullptr`. `get_deleter()` returns a
262
  reference to the stored deleter.
263
 
264
+ *Remarks:* If `is_pointer_v<deleter_type>` is `true` or
265
+ `is_default_constructible_v<deleter_type>` is `false`, this constructor
266
+ shall not participate in overload resolution.
267
 
268
  ``` cpp
269
  explicit unique_ptr(pointer p) noexcept;
270
  ```
271
 
272
  *Requires:* `D` shall satisfy the requirements of `DefaultConstructible`
273
+ (Table  [[tab:defaultconstructible]]), and that construction shall not
274
+ throw an exception.
275
 
276
  *Effects:* Constructs a `unique_ptr` which owns `p`, initializing the
277
  stored pointer with `p` and value-initializing the stored deleter.
278
 
279
  *Postconditions:* `get() == p`. `get_deleter()` returns a reference to
280
  the stored deleter.
281
 
282
+ *Remarks:* If `is_pointer_v<deleter_type>` is `true` or
283
+ `is_default_constructible_v<deleter_type>` is `false`, this constructor
284
+ shall not participate in overload resolution. If class template argument
285
+ deduction ([[over.match.class.deduct]]) would select the function
286
+ template corresponding to this constructor, then the program is
287
+ ill-formed.
288
 
289
  ``` cpp
290
  unique_ptr(pointer p, see below d1) noexcept;
291
  unique_ptr(pointer p, see below d2) noexcept;
292
  ```
293
 
294
  The signature of these constructors depends upon whether `D` is a
295
+ reference type. If `D` is a non-reference type `A`, then the signatures
296
  are:
297
 
298
  ``` cpp
299
+ unique_ptr(pointer p, const A& d) noexcept;
300
+ unique_ptr(pointer p, A&& d) noexcept;
301
  ```
302
 
303
+ If `D` is an lvalue reference type `A&`, then the signatures are:
304
 
305
  ``` cpp
306
+ unique_ptr(pointer p, A& d) noexcept;
307
+ unique_ptr(pointer p, A&& d) = delete;
308
  ```
309
 
310
+ If `D` is an lvalue reference type `const A&`, then the signatures are:
311
 
312
  ``` cpp
313
+ unique_ptr(pointer p, const A& d) noexcept;
314
+ unique_ptr(pointer p, const A&& d) = delete;
315
  ```
316
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
317
  *Effects:* Constructs a `unique_ptr` object which owns `p`, initializing
318
+ the stored pointer with `p` and initializing the deleter from
319
+ `std::forward<decltype(d)>(d)`.
320
+
321
+ *Remarks:* These constructors shall not participate in overload
322
+ resolution unless `is_constructible_v<D, decltype(d)>` is `true`.
323
 
324
  *Postconditions:* `get() == p`. `get_deleter()` returns a reference to
325
  the stored deleter. If `D` is a reference type then `get_deleter()`
326
  returns a reference to the lvalue `d`.
327
 
328
+ *Remarks:* If class template argument
329
+ deduction ([[over.match.class.deduct]]) would select a function
330
+ template corresponding to either of these constructors, then the program
331
+ is ill-formed.
332
+
333
+ [*Example 1*:
334
+
335
  ``` cpp
336
  D d;
337
  unique_ptr<int, D> p1(new int, D()); // D must be MoveConstructible
338
  unique_ptr<int, D> p2(new int, d); // D must be CopyConstructible
339
  unique_ptr<int, D&> p3(new int, d); // p3 holds a reference to d
340
  unique_ptr<int, const D&> p4(new int, D()); // error: rvalue deleter object combined
341
  // with reference deleter type
342
  ```
343
 
344
+ — *end example*]
345
+
346
  ``` cpp
347
  unique_ptr(unique_ptr&& u) noexcept;
348
  ```
349
 
350
  *Requires:* If `D` is not a reference type, `D` shall satisfy the
351
+ requirements of `MoveConstructible` (Table  [[tab:moveconstructible]]).
352
  Construction of the deleter from an rvalue of type `D` shall not throw
353
  an exception.
354
 
355
  *Effects:* Constructs a `unique_ptr` by transferring ownership from `u`
356
  to `*this`. If `D` is a reference type, this deleter is copy constructed
357
  from `u`’s deleter; otherwise, this deleter is move constructed from
358
+ `u`’s deleter.
359
+
360
+ [*Note 1*: The deleter constructor can be implemented with
361
+ `std::forward<D>`. — *end note*]
362
 
363
  *Postconditions:* `get()` yields the value `u.get()` yielded before the
364
  construction. `get_deleter()` returns a reference to the stored deleter
365
  that was constructed from `u.get_deleter()`. If `D` is a reference type
366
  then `get_deleter()` and `u.get_deleter()` both reference the same
 
385
  is not a reference type and `E` is implicitly convertible to `D`.
386
 
387
  *Effects:* Constructs a `unique_ptr` by transferring ownership from `u`
388
  to `*this`. If `E` is a reference type, this deleter is copy constructed
389
  from `u`’s deleter; otherwise, this deleter is move constructed from
390
+ `u`’s deleter.
391
+
392
+ [*Note 2*: The deleter constructor can be implemented with
393
+ `std::forward<E>`. — *end note*]
394
 
395
  *Postconditions:* `get()` yields the value `u.get()` yielded before the
396
  construction. `get_deleter()` returns a reference to the stored deleter
397
  that was constructed from `u.get_deleter()`.
398
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
399
  ##### `unique_ptr` destructor <a id="unique.ptr.single.dtor">[[unique.ptr.single.dtor]]</a>
400
 
401
  ``` cpp
402
  ~unique_ptr();
403
  ```
404
 
405
  *Requires:* The expression `get_deleter()(get())` shall be well formed,
406
+ shall have well-defined behavior, and shall not throw exceptions.
407
+
408
+ [*Note 3*: The use of `default_delete` requires `T` to be a complete
409
+ type. — *end note*]
410
 
411
  *Effects:* If `get() == nullptr` there are no effects. Otherwise
412
  `get_deleter()(get())`.
413
 
414
  ##### `unique_ptr` assignment <a id="unique.ptr.single.asgn">[[unique.ptr.single.asgn]]</a>
 
416
  ``` cpp
417
  unique_ptr& operator=(unique_ptr&& u) noexcept;
418
  ```
419
 
420
  *Requires:* If `D` is not a reference type, `D` shall satisfy the
421
+ requirements of `MoveAssignable` (Table  [[tab:moveassignable]]) and
422
  assignment of the deleter from an rvalue of type `D` shall not throw an
423
  exception. Otherwise, `D` is a reference type; `remove_reference_t<D>`
424
  shall satisfy the `CopyAssignable` requirements and assignment of the
425
  deleter from an lvalue of type `D` shall not throw an exception.
426
 
 
441
  throw an exception.
442
 
443
  *Remarks:* This operator shall not participate in overload resolution
444
  unless:
445
 
446
+ - `unique_ptr<U, E>::pointer` is implicitly convertible to `pointer`,
447
+ and
448
+ - `U` is not an array type, and
449
+ - `is_assignable_v<D&, E&&>` is `true`.
450
 
451
  *Effects:* Transfers ownership from `u` to `*this` as if by calling
452
  `reset(u.release())` followed by
453
  `get_deleter() = std::forward<E>(u.get_deleter())`.
454
 
 
456
 
457
  ``` cpp
458
  unique_ptr& operator=(nullptr_t) noexcept;
459
  ```
460
 
461
+ *Effects:* As if by `reset()`.
462
 
463
+ *Postconditions:* `get() == nullptr`.
464
 
465
  *Returns:* `*this`.
466
 
467
  ##### `unique_ptr` observers <a id="unique.ptr.single.observers">[[unique.ptr.single.observers]]</a>
468
 
 
480
 
481
  *Requires:* `get() != nullptr`.
482
 
483
  *Returns:* `get()`.
484
 
485
+ [*Note 4*: The use of this function typically requires that `T` be a
486
+ complete type. — *end note*]
487
 
488
  ``` cpp
489
  pointer get() const noexcept;
490
  ```
491
 
 
508
 
509
  ``` cpp
510
  pointer release() noexcept;
511
  ```
512
 
513
+ *Postconditions:* `get() == nullptr`.
514
 
515
  *Returns:* The value `get()` had at the start of the call to `release`.
516
 
517
  ``` cpp
518
  void reset(pointer p = pointer()) noexcept;
519
  ```
520
 
521
  *Requires:* The expression `get_deleter()(get())` shall be well formed,
522
  shall have well-defined behavior, and shall not throw exceptions.
523
 
524
+ *Effects:* Assigns `p` to the stored pointer, and then if and only if
525
+ the old value of the stored pointer, `old_p`, was not equal to
526
+ `nullptr`, calls `get_deleter()(old_p)`.
 
527
 
528
+ [*Note 5*: The order of these operations is significant because the
529
+ call to `get_deleter()` may destroy `*this`. *end note*]
530
+
531
+ *Postconditions:* `get() == p`.
532
+
533
+ [*Note 6*: The postcondition does not hold if the call to
534
+ `get_deleter()` destroys `*this` since `this->get()` is no longer a
535
+ valid expression. — *end note*]
536
 
537
  ``` cpp
538
  void swap(unique_ptr& u) noexcept;
539
  ```
540
 
 
549
 
550
  ``` cpp
551
  namespace std {
552
  template <class T, class D> class unique_ptr<T[], D> {
553
  public:
554
+ using pointer = see below;
555
+ using element_type = T;
556
+ using deleter_type = D;
557
 
558
  // [unique.ptr.runtime.ctor], constructors
559
  constexpr unique_ptr() noexcept;
560
+ template <class U> explicit unique_ptr(U p) noexcept;
561
+ template <class U> unique_ptr(U p, see below d) noexcept;
562
+ template <class U> unique_ptr(U p, see below d) noexcept;
563
  unique_ptr(unique_ptr&& u) noexcept;
564
+ template <class U, class E>
565
+ unique_ptr(unique_ptr<U, E>&& u) noexcept;
566
+ constexpr unique_ptr(nullptr_t) noexcept;
567
 
568
  // destructor
569
  ~unique_ptr();
570
 
571
  // assignment
572
  unique_ptr& operator=(unique_ptr&& u) noexcept;
573
+ template <class U, class E>
574
+ unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
575
  unique_ptr& operator=(nullptr_t) noexcept;
576
 
577
  // [unique.ptr.runtime.observers], observers
578
  T& operator[](size_t i) const;
579
  pointer get() const noexcept;
580
  deleter_type& get_deleter() noexcept;
581
  const deleter_type& get_deleter() const noexcept;
582
  explicit operator bool() const noexcept;
583
 
584
+ // [unique.ptr.runtime.modifiers], modifiers
585
  pointer release() noexcept;
586
+ template <class U> void reset(U p) noexcept;
587
+ void reset(nullptr_t = nullptr) noexcept;
 
588
  void swap(unique_ptr& u) noexcept;
589
 
590
  // disable copy from lvalue
591
  unique_ptr(const unique_ptr&) = delete;
592
  unique_ptr& operator=(const unique_ptr&) = delete;
 
595
  ```
596
 
597
  A specialization for array types is provided with a slightly altered
598
  interface.
599
 
600
+ - Conversions between different types of `unique_ptr<T[], D>` that would
601
+ be disallowed for the corresponding pointer-to-array types, and
602
+ conversions to or from the non-array forms of `unique_ptr`, produce an
603
+ ill-formed program.
604
  - Pointers to types derived from `T` are rejected by the constructors,
605
  and by `reset`.
606
  - The observers `operator*` and `operator->` are not provided.
607
  - The indexing observer `operator[]` is provided.
608
  - The default deleter will call `delete[]`.
609
 
610
+ Descriptions are provided below only for members that differ from the
611
+ primary template.
612
 
613
  The template argument `T` shall be a complete type.
614
 
615
  ##### `unique_ptr` constructors <a id="unique.ptr.runtime.ctor">[[unique.ptr.runtime.ctor]]</a>
616
 
617
  ``` cpp
618
+ template <class U> explicit unique_ptr(U p) noexcept;
 
 
619
  ```
620
 
621
+ This constructor behaves the same as the constructor in the primary
622
+ template that takes a single parameter of type `pointer` except that it
623
+ additionally shall not participate in overload resolution unless
624
+
625
+ - `U` is the same type as `pointer`, or
626
+ - `pointer` is the same type as `element_type*`, `U` is a pointer type
627
+ `V*`, and `V(*)[]` is convertible to `element_type(*)[]`.
628
+
629
+ ``` cpp
630
+ template <class U> unique_ptr(U p, see below d) noexcept;
631
+ template <class U> unique_ptr(U p, see below d) noexcept;
632
+ ```
633
+
634
+ These constructors behave the same as the constructors in the primary
635
+ template that take a parameter of type `pointer` and a second parameter
636
+ except that they shall not participate in overload resolution unless
637
+ either
638
+
639
+ - `U` is the same type as `pointer`,
640
+ - `U` is `nullptr_t`, or
641
+ - `pointer` is the same type as `element_type*`, `U` is a pointer type
642
+ `V*`, and `V(*)[]` is convertible to `element_type(*)[]`.
643
+
644
+ ``` cpp
645
+ template <class U, class E>
646
+ unique_ptr(unique_ptr<U, E>&& u) noexcept;
647
+ ```
648
+
649
+ This constructor behaves the same as in the primary template, except
650
+ that it shall not participate in overload resolution unless all of the
651
+ following conditions hold, where `UP` is `unique_ptr<U, E>`:
652
+
653
+ - `U` is an array type, and
654
+ - `pointer` is the same type as `element_type*`, and
655
+ - `UP::pointer` is the same type as `UP::element_type*`, and
656
+ - `UP::element_type(*)[]` is convertible to `element_type(*)[]`, and
657
+ - either `D` is a reference type and `E` is the same type as `D`, or `D`
658
+ is not a reference type and `E` is implicitly convertible to `D`.
659
+
660
+ [*Note 1*: This replaces the overload-resolution specification of the
661
+ primary template — *end note*]
662
+
663
+ ##### `unique_ptr` assignment <a id="unique.ptr.runtime.asgn">[[unique.ptr.runtime.asgn]]</a>
664
+
665
+ ``` cpp
666
+ template <class U, class E>
667
+ unique_ptr& operator=(unique_ptr<U, E>&& u)noexcept;
668
+ ```
669
+
670
+ This operator behaves the same as in the primary template, except that
671
+ it shall not participate in overload resolution unless all of the
672
+ following conditions hold, where `UP` is `unique_ptr<U, E>`:
673
+
674
+ - `U` is an array type, and
675
+ - `pointer` is the same type as `element_type*`, and
676
+ - `UP::pointer` is the same type as `UP::element_type*`, and
677
+ - `UP::element_type(*)[]` is convertible to `element_type(*)[]`, and
678
+ - `is_assignable_v<D&, E&&>` is `true`.
679
+
680
+ [*Note 2*: This replaces the overload-resolution specification of the
681
+ primary template — *end note*]
682
 
683
  ##### `unique_ptr` observers <a id="unique.ptr.runtime.observers">[[unique.ptr.runtime.observers]]</a>
684
 
685
  ``` cpp
686
  T& operator[](size_t i) const;
 
692
  *Returns:* `get()[i]`.
693
 
694
  ##### `unique_ptr` modifiers <a id="unique.ptr.runtime.modifiers">[[unique.ptr.runtime.modifiers]]</a>
695
 
696
  ``` cpp
697
+ void reset(nullptr_t p = nullptr) noexcept;
698
  ```
699
 
700
  *Effects:* Equivalent to `reset(pointer())`.
701
 
702
+ ``` cpp
703
+ template <class U> void reset(U p) noexcept;
704
+ ```
705
+
706
+ This function behaves the same as the `reset` member of the primary
707
+ template, except that it shall not participate in overload resolution
708
+ unless either
709
+
710
+ - `U` is the same type as `pointer`, or
711
+ - `pointer` is the same type as `element_type*`, `U` is a pointer type
712
+ `V*`, and `V(*)[]` is convertible to `element_type(*)[]`.
713
+
714
  #### `unique_ptr` creation <a id="unique.ptr.create">[[unique.ptr.create]]</a>
715
 
716
  ``` cpp
717
  template <class T, class... Args> unique_ptr<T> make_unique(Args&&... args);
718
  ```
 
742
 
743
  ``` cpp
744
  template <class T, class D> void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
745
  ```
746
 
747
+ *Remarks:* This function shall not participate in overload resolution
748
+ unless `is_swappable_v<D>` is `true`.
749
+
750
  *Effects:* Calls `x.swap(y)`.
751
 
752
  ``` cpp
753
  template <class T1, class D1, class T2, class D2>
754
  bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
 
766
  ``` cpp
767
  template <class T1, class D1, class T2, class D2>
768
  bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
769
  ```
770
 
771
+ *Requires:* Let *`CT`* denote
 
 
 
772
 
773
+ ``` cpp
774
+ common_type_t<typename unique_ptr<T1, D1>::pointer,
775
+ typename unique_ptr<T2, D2>::pointer>
776
+ ```
777
+
778
+ Then the specialization `less<`*`CT`*`>` shall be a function object
779
+ type ([[function.objects]]) that induces a strict weak
780
+ ordering ([[alg.sorting]]) on the pointer values.
781
+
782
+ *Returns:* `less<`*`CT`*`>()(x.get(), y.get())`.
783
 
784
  *Remarks:* If `unique_ptr<T1, D1>::pointer` is not implicitly
785
+ convertible to *`CT`* or `unique_ptr<T2, D2>::pointer` is not implicitly
786
+ convertible to *`CT`*, the program is ill-formed.
787
 
788
  ``` cpp
789
  template <class T1, class D1, class T2, class D2>
790
  bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
791
  ```