- tmp/tmpl4azrdih/{from.md → to.md} +582 -211
tmp/tmpl4azrdih/{from.md → to.md}
RENAMED
|
@@ -1,8 +1,10 @@
|
|
| 1 |
## Smart pointers <a id="smartptr">[[smartptr]]</a>
|
| 2 |
|
| 3 |
-
###
|
|
|
|
|
|
|
| 4 |
|
| 5 |
A *unique pointer* is an object that owns another object and manages
|
| 6 |
that other object through a pointer. More precisely, a unique pointer is
|
| 7 |
an object *u* that stores a pointer to a second object *p* and will
|
| 8 |
dispose of *p* when *u* is itself destroyed (e.g., when leaving block
|
|
@@ -17,11 +19,11 @@ denote the associated deleter. Upon request, *u* can *reset* (replace)
|
|
| 17 |
*u.p* and *u.d* with another pointer and deleter, but properly disposes
|
| 18 |
of its owned object via the associated deleter before such replacement
|
| 19 |
is considered completed.
|
| 20 |
|
| 21 |
Each object of a type `U` instantiated from the `unique_ptr` template
|
| 22 |
-
specified in
|
| 23 |
specified above, of a unique pointer. In partial satisfaction of these
|
| 24 |
semantics, each such `U` is *Cpp17MoveConstructible* and
|
| 25 |
*Cpp17MoveAssignable*, but is not *Cpp17CopyConstructible* nor
|
| 26 |
*Cpp17CopyAssignable*. The template parameter `T` of `unique_ptr` may be
|
| 27 |
an incomplete type.
|
|
@@ -45,27 +47,27 @@ type.
|
|
| 45 |
|
| 46 |
``` cpp
|
| 47 |
namespace std {
|
| 48 |
template<class T> struct default_delete {
|
| 49 |
constexpr default_delete() noexcept = default;
|
| 50 |
-
template<class U> default_delete(const default_delete<U>&) noexcept;
|
| 51 |
-
void operator()(T*) const;
|
| 52 |
};
|
| 53 |
}
|
| 54 |
```
|
| 55 |
|
| 56 |
``` cpp
|
| 57 |
-
template<class U> default_delete(const default_delete<U>& other) noexcept;
|
| 58 |
```
|
| 59 |
|
| 60 |
*Constraints:* `U*` is implicitly convertible to `T*`.
|
| 61 |
|
| 62 |
*Effects:* Constructs a `default_delete` object from another
|
| 63 |
`default_delete<U>` object.
|
| 64 |
|
| 65 |
``` cpp
|
| 66 |
-
void operator()(T* ptr) const;
|
| 67 |
```
|
| 68 |
|
| 69 |
*Mandates:* `T` is a complete type.
|
| 70 |
|
| 71 |
*Effects:* Calls `delete` on `ptr`.
|
|
@@ -74,76 +76,78 @@ void operator()(T* ptr) const;
|
|
| 74 |
|
| 75 |
``` cpp
|
| 76 |
namespace std {
|
| 77 |
template<class T> struct default_delete<T[]> {
|
| 78 |
constexpr default_delete() noexcept = default;
|
| 79 |
-
template<class U> default_delete(const default_delete<U[]>&) noexcept;
|
| 80 |
-
template<class U> void operator()(U* ptr) const;
|
| 81 |
};
|
| 82 |
}
|
| 83 |
```
|
| 84 |
|
| 85 |
``` cpp
|
| 86 |
-
template<class U> default_delete(const default_delete<U[]>& other) noexcept;
|
| 87 |
```
|
| 88 |
|
| 89 |
*Constraints:* `U(*)[]` is convertible to `T(*)[]`.
|
| 90 |
|
| 91 |
*Effects:* Constructs a `default_delete` object from another
|
| 92 |
`default_delete<U[]>` object.
|
| 93 |
|
| 94 |
``` cpp
|
| 95 |
-
template<class U> void operator()(U* ptr) const;
|
| 96 |
```
|
| 97 |
|
|
|
|
|
|
|
| 98 |
*Mandates:* `U` is a complete type.
|
| 99 |
|
| 100 |
-
*Constraints:* `U(*)[]` is convertible to `T(*)[]`.
|
| 101 |
-
|
| 102 |
*Effects:* Calls `delete[]` on `ptr`.
|
| 103 |
|
| 104 |
#### `unique_ptr` for single objects <a id="unique.ptr.single">[[unique.ptr.single]]</a>
|
| 105 |
|
|
|
|
|
|
|
| 106 |
``` cpp
|
| 107 |
namespace std {
|
| 108 |
template<class T, class D = default_delete<T>> class unique_ptr {
|
| 109 |
public:
|
| 110 |
using pointer = see below;
|
| 111 |
using element_type = T;
|
| 112 |
using deleter_type = D;
|
| 113 |
|
| 114 |
// [unique.ptr.single.ctor], constructors
|
| 115 |
constexpr unique_ptr() noexcept;
|
| 116 |
-
explicit unique_ptr(pointer p) noexcept;
|
| 117 |
-
unique_ptr(pointer p, see below d1) noexcept;
|
| 118 |
-
unique_ptr(pointer p, see below d2) noexcept;
|
| 119 |
-
unique_ptr(unique_ptr&& u) noexcept;
|
| 120 |
constexpr unique_ptr(nullptr_t) noexcept;
|
| 121 |
template<class U, class E>
|
| 122 |
-
unique_ptr(unique_ptr<U, E>&& u) noexcept;
|
| 123 |
|
| 124 |
// [unique.ptr.single.dtor], destructor
|
| 125 |
-
~unique_ptr();
|
| 126 |
|
| 127 |
// [unique.ptr.single.asgn], assignment
|
| 128 |
-
unique_ptr& operator=(unique_ptr&& u) noexcept;
|
| 129 |
template<class U, class E>
|
| 130 |
-
unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
|
| 131 |
-
unique_ptr& operator=(nullptr_t) noexcept;
|
| 132 |
|
| 133 |
// [unique.ptr.single.observers], observers
|
| 134 |
-
add_lvalue_reference_t<T> operator*() const;
|
| 135 |
-
pointer operator->() const noexcept;
|
| 136 |
-
pointer get() const noexcept;
|
| 137 |
-
deleter_type& get_deleter() noexcept;
|
| 138 |
-
const deleter_type& get_deleter() const noexcept;
|
| 139 |
-
explicit operator bool() const noexcept;
|
| 140 |
|
| 141 |
// [unique.ptr.single.modifiers], modifiers
|
| 142 |
-
pointer release() noexcept;
|
| 143 |
-
void reset(pointer p = pointer()) noexcept;
|
| 144 |
-
void swap(unique_ptr& u) noexcept;
|
| 145 |
|
| 146 |
// disable copy from lvalue
|
| 147 |
unique_ptr(const unique_ptr&) = delete;
|
| 148 |
unique_ptr& operator=(const unique_ptr&) = delete;
|
| 149 |
};
|
|
@@ -167,46 +171,43 @@ D>::pointer` shall be a synonym for `remove_reference_t<D>::pointer`.
|
|
| 167 |
Otherwise `unique_ptr<T, D>::pointer` shall be a synonym for
|
| 168 |
`element_type*`. The type `unique_ptr<T,
|
| 169 |
D>::pointer` shall meet the *Cpp17NullablePointer* requirements (
|
| 170 |
[[cpp17.nullablepointer]]).
|
| 171 |
|
| 172 |
-
[*Example 1*: Given an allocator type `X`
|
| 173 |
-
letting `A` be a synonym for
|
| 174 |
-
`
|
| 175 |
-
`A::const_void_pointer` may be used as
|
| 176 |
`unique_ptr<T, D>::pointer`. — *end example*]
|
| 177 |
|
| 178 |
##### Constructors <a id="unique.ptr.single.ctor">[[unique.ptr.single.ctor]]</a>
|
| 179 |
|
| 180 |
``` cpp
|
| 181 |
constexpr unique_ptr() noexcept;
|
| 182 |
constexpr unique_ptr(nullptr_t) noexcept;
|
| 183 |
```
|
| 184 |
|
|
|
|
|
|
|
|
|
|
| 185 |
*Preconditions:* `D` meets the *Cpp17DefaultConstructible* requirements
|
| 186 |
([[cpp17.defaultconstructible]]), and that construction does not throw
|
| 187 |
an exception.
|
| 188 |
|
| 189 |
-
*Constraints:* `is_pointer_v<deleter_type>` is `false` and
|
| 190 |
-
`is_default_constructible_v<deleter_type>` is `true`.
|
| 191 |
-
|
| 192 |
*Effects:* Constructs a `unique_ptr` object that owns nothing,
|
| 193 |
value-initializing the stored pointer and the stored deleter.
|
| 194 |
|
| 195 |
*Ensures:* `get() == nullptr`. `get_deleter()` returns a reference to
|
| 196 |
the stored deleter.
|
| 197 |
|
| 198 |
``` cpp
|
| 199 |
-
explicit unique_ptr(pointer p) noexcept;
|
| 200 |
```
|
| 201 |
|
| 202 |
*Constraints:* `is_pointer_v<deleter_type>` is `false` and
|
| 203 |
`is_default_constructible_v<deleter_type>` is `true`.
|
| 204 |
|
| 205 |
-
*Mandates:* This constructor is not selected by class template argument
|
| 206 |
-
deduction [[over.match.class.deduct]].
|
| 207 |
-
|
| 208 |
*Preconditions:* `D` meets the *Cpp17DefaultConstructible* requirements
|
| 209 |
([[cpp17.defaultconstructible]]), and that construction does not throw
|
| 210 |
an exception.
|
| 211 |
|
| 212 |
*Effects:* Constructs a `unique_ptr` which owns `p`, initializing the
|
|
@@ -214,19 +215,16 @@ stored pointer with `p` and value-initializing the stored deleter.
|
|
| 214 |
|
| 215 |
*Ensures:* `get() == p`. `get_deleter()` returns a reference to the
|
| 216 |
stored deleter.
|
| 217 |
|
| 218 |
``` cpp
|
| 219 |
-
unique_ptr(pointer p, const D& d) noexcept;
|
| 220 |
-
unique_ptr(pointer p, remove_reference_t<D>&& d) noexcept;
|
| 221 |
```
|
| 222 |
|
| 223 |
*Constraints:* `is_constructible_v<D, decltype(d)>` is `true`.
|
| 224 |
|
| 225 |
-
*Mandates:* These constructors are not selected by class template
|
| 226 |
-
argument deduction [[over.match.class.deduct]].
|
| 227 |
-
|
| 228 |
*Preconditions:* For the first constructor, if `D` is not a reference
|
| 229 |
type, `D` meets the *Cpp17CopyConstructible* requirements and such
|
| 230 |
construction does not exit via an exception. For the second constructor,
|
| 231 |
if `D` is not a reference type, `D` meets the *Cpp17MoveConstructible*
|
| 232 |
requirements and such construction does not exit via an exception.
|
|
@@ -254,11 +252,11 @@ unique_ptr<int, const D&> p4(new int, D()); // error: rvalue deleter object comb
|
|
| 254 |
```
|
| 255 |
|
| 256 |
— *end example*]
|
| 257 |
|
| 258 |
``` cpp
|
| 259 |
-
unique_ptr(unique_ptr&& u) noexcept;
|
| 260 |
```
|
| 261 |
|
| 262 |
*Constraints:* `is_move_constructible_v<D>` is `true`.
|
| 263 |
|
| 264 |
*Preconditions:* If `D` is not a reference type, `D` meets the
|
|
@@ -278,11 +276,11 @@ construction. `u.get() == nullptr`. `get_deleter()` returns a reference
|
|
| 278 |
to the stored deleter that was constructed from `u.get_deleter()`. If
|
| 279 |
`D` is a reference type then `get_deleter()` and `u.get_deleter()` both
|
| 280 |
reference the same lvalue deleter.
|
| 281 |
|
| 282 |
``` cpp
|
| 283 |
-
template<class U, class E> unique_ptr(unique_ptr<U, E>&& u) noexcept;
|
| 284 |
```
|
| 285 |
|
| 286 |
*Constraints:*
|
| 287 |
|
| 288 |
- `unique_ptr<U, E>::pointer` is implicitly convertible to `pointer`,
|
|
@@ -308,26 +306,25 @@ construction. `u.get() == nullptr`. `get_deleter()` returns a reference
|
|
| 308 |
to the stored deleter that was constructed from `u.get_deleter()`.
|
| 309 |
|
| 310 |
##### Destructor <a id="unique.ptr.single.dtor">[[unique.ptr.single.dtor]]</a>
|
| 311 |
|
| 312 |
``` cpp
|
| 313 |
-
~unique_ptr();
|
| 314 |
```
|
| 315 |
|
| 316 |
-
*
|
| 317 |
-
has well-defined behavior, and does not throw exceptions.
|
| 318 |
|
| 319 |
[*Note 3*: The use of `default_delete` requires `T` to be a complete
|
| 320 |
type. — *end note*]
|
| 321 |
|
| 322 |
-
*
|
| 323 |
-
`get_deleter()(get())`.
|
| 324 |
|
| 325 |
##### Assignment <a id="unique.ptr.single.asgn">[[unique.ptr.single.asgn]]</a>
|
| 326 |
|
| 327 |
``` cpp
|
| 328 |
-
unique_ptr& operator=(unique_ptr&& u) noexcept;
|
| 329 |
```
|
| 330 |
|
| 331 |
*Constraints:* `is_move_assignable_v<D>` is `true`.
|
| 332 |
|
| 333 |
*Preconditions:* If `D` is not a reference type, `D` meets the
|
|
@@ -338,16 +335,17 @@ meets the *Cpp17CopyAssignable* requirements and assignment of the
|
|
| 338 |
deleter from an lvalue of type `D` does not throw an exception.
|
| 339 |
|
| 340 |
*Effects:* Calls `reset(u.release())` followed by
|
| 341 |
`get_deleter() = std::forward<D>(u.get_deleter())`.
|
| 342 |
|
|
|
|
|
|
|
|
|
|
| 343 |
*Returns:* `*this`.
|
| 344 |
|
| 345 |
-
*Ensures:* `u.get() == nullptr`.
|
| 346 |
-
|
| 347 |
``` cpp
|
| 348 |
-
template<class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
|
| 349 |
```
|
| 350 |
|
| 351 |
*Constraints:*
|
| 352 |
|
| 353 |
- `unique_ptr<U, E>::pointer` is implicitly convertible to `pointer`,
|
|
@@ -362,16 +360,16 @@ deleter from an lvalue of type `E` is well-formed and does not throw an
|
|
| 362 |
exception.
|
| 363 |
|
| 364 |
*Effects:* Calls `reset(u.release())` followed by
|
| 365 |
`get_deleter() = std::forward<E>(u.get_deleter())`.
|
| 366 |
|
| 367 |
-
*Returns:* `*this`.
|
| 368 |
-
|
| 369 |
*Ensures:* `u.get() == nullptr`.
|
| 370 |
|
|
|
|
|
|
|
| 371 |
``` cpp
|
| 372 |
-
unique_ptr& operator=(nullptr_t) noexcept;
|
| 373 |
```
|
| 374 |
|
| 375 |
*Effects:* As if by `reset()`.
|
| 376 |
|
| 377 |
*Ensures:* `get() == nullptr`.
|
|
@@ -379,128 +377,130 @@ unique_ptr& operator=(nullptr_t) noexcept;
|
|
| 379 |
*Returns:* `*this`.
|
| 380 |
|
| 381 |
##### Observers <a id="unique.ptr.single.observers">[[unique.ptr.single.observers]]</a>
|
| 382 |
|
| 383 |
``` cpp
|
| 384 |
-
add_lvalue_reference_t<T> operator*() const;
|
| 385 |
```
|
| 386 |
|
| 387 |
*Preconditions:* `get() != nullptr`.
|
| 388 |
|
| 389 |
*Returns:* `*get()`.
|
| 390 |
|
| 391 |
``` cpp
|
| 392 |
-
pointer operator->() const noexcept;
|
| 393 |
```
|
| 394 |
|
| 395 |
*Preconditions:* `get() != nullptr`.
|
| 396 |
|
| 397 |
*Returns:* `get()`.
|
| 398 |
|
| 399 |
[*Note 4*: The use of this function typically requires that `T` be a
|
| 400 |
complete type. — *end note*]
|
| 401 |
|
| 402 |
``` cpp
|
| 403 |
-
pointer get() const noexcept;
|
| 404 |
```
|
| 405 |
|
| 406 |
*Returns:* The stored pointer.
|
| 407 |
|
| 408 |
``` cpp
|
| 409 |
-
deleter_type& get_deleter() noexcept;
|
| 410 |
-
const deleter_type& get_deleter() const noexcept;
|
| 411 |
```
|
| 412 |
|
| 413 |
*Returns:* A reference to the stored deleter.
|
| 414 |
|
| 415 |
``` cpp
|
| 416 |
-
explicit operator bool() const noexcept;
|
| 417 |
```
|
| 418 |
|
| 419 |
*Returns:* `get() != nullptr`.
|
| 420 |
|
| 421 |
##### Modifiers <a id="unique.ptr.single.modifiers">[[unique.ptr.single.modifiers]]</a>
|
| 422 |
|
| 423 |
``` cpp
|
| 424 |
-
pointer release() noexcept;
|
| 425 |
```
|
| 426 |
|
| 427 |
*Ensures:* `get() == nullptr`.
|
| 428 |
|
| 429 |
*Returns:* The value `get()` had at the start of the call to `release`.
|
| 430 |
|
| 431 |
``` cpp
|
| 432 |
-
void reset(pointer p = pointer()) noexcept;
|
| 433 |
```
|
| 434 |
|
| 435 |
-
*
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
*Effects:* Assigns `p` to the stored pointer, and then if and only if
|
| 439 |
-
the old value of the stored pointer, `old_p`, was not equal to
|
| 440 |
-
`nullptr`, calls `get_deleter()(old_p)`.
|
| 441 |
|
| 442 |
[*Note 5*: The order of these operations is significant because the
|
| 443 |
-
call to `get_deleter()`
|
| 444 |
|
| 445 |
*Ensures:* `get() == p`.
|
| 446 |
|
| 447 |
[*Note 6*: The postcondition does not hold if the call to
|
| 448 |
`get_deleter()` destroys `*this` since `this->get()` is no longer a
|
| 449 |
valid expression. — *end note*]
|
| 450 |
|
|
|
|
|
|
|
|
|
|
| 451 |
``` cpp
|
| 452 |
-
void swap(unique_ptr& u) noexcept;
|
| 453 |
```
|
| 454 |
|
| 455 |
*Preconditions:* `get_deleter()` is swappable [[swappable.requirements]]
|
| 456 |
and does not throw an exception under `swap`.
|
| 457 |
|
| 458 |
*Effects:* Invokes `swap` on the stored pointers and on the stored
|
| 459 |
deleters of `*this` and `u`.
|
| 460 |
|
| 461 |
#### `unique_ptr` for array objects with a runtime length <a id="unique.ptr.runtime">[[unique.ptr.runtime]]</a>
|
| 462 |
|
|
|
|
|
|
|
| 463 |
``` cpp
|
| 464 |
namespace std {
|
| 465 |
template<class T, class D> class unique_ptr<T[], D> {
|
| 466 |
public:
|
| 467 |
using pointer = see below;
|
| 468 |
using element_type = T;
|
| 469 |
using deleter_type = D;
|
| 470 |
|
| 471 |
// [unique.ptr.runtime.ctor], constructors
|
| 472 |
constexpr unique_ptr() noexcept;
|
| 473 |
-
template<class U> explicit unique_ptr(U p) noexcept;
|
| 474 |
-
template<class U> unique_ptr(U p, see below d) noexcept;
|
| 475 |
-
template<class U> unique_ptr(U p, see below d) noexcept;
|
| 476 |
-
unique_ptr(unique_ptr&& u) noexcept;
|
| 477 |
template<class U, class E>
|
| 478 |
-
unique_ptr(unique_ptr<U, E>&& u) noexcept;
|
| 479 |
constexpr unique_ptr(nullptr_t) noexcept;
|
| 480 |
|
| 481 |
// destructor
|
| 482 |
-
~unique_ptr();
|
| 483 |
|
| 484 |
// assignment
|
| 485 |
-
unique_ptr& operator=(unique_ptr&& u) noexcept;
|
| 486 |
template<class U, class E>
|
| 487 |
-
unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
|
| 488 |
-
unique_ptr& operator=(nullptr_t) noexcept;
|
| 489 |
|
| 490 |
// [unique.ptr.runtime.observers], observers
|
| 491 |
-
T& operator[](size_t i) const;
|
| 492 |
-
pointer get() const noexcept;
|
| 493 |
-
deleter_type& get_deleter() noexcept;
|
| 494 |
-
const deleter_type& get_deleter() const noexcept;
|
| 495 |
-
explicit operator bool() const noexcept;
|
| 496 |
|
| 497 |
// [unique.ptr.runtime.modifiers], modifiers
|
| 498 |
-
pointer release() noexcept;
|
| 499 |
-
template<class U> void reset(U p) noexcept;
|
| 500 |
-
void reset(nullptr_t = nullptr) noexcept;
|
| 501 |
-
void swap(unique_ptr& u) noexcept;
|
| 502 |
|
| 503 |
// disable copy from lvalue
|
| 504 |
unique_ptr(const unique_ptr&) = delete;
|
| 505 |
unique_ptr& operator=(const unique_ptr&) = delete;
|
| 506 |
};
|
|
@@ -526,11 +526,11 @@ primary template.
|
|
| 526 |
The template argument `T` shall be a complete type.
|
| 527 |
|
| 528 |
##### Constructors <a id="unique.ptr.runtime.ctor">[[unique.ptr.runtime.ctor]]</a>
|
| 529 |
|
| 530 |
``` cpp
|
| 531 |
-
template<class U> explicit unique_ptr(U p) noexcept;
|
| 532 |
```
|
| 533 |
|
| 534 |
This constructor behaves the same as the constructor in the primary
|
| 535 |
template that takes a single parameter of type `pointer`.
|
| 536 |
|
|
@@ -539,12 +539,12 @@ template that takes a single parameter of type `pointer`.
|
|
| 539 |
- `U` is the same type as `pointer`, or
|
| 540 |
- `pointer` is the same type as `element_type*`, `U` is a pointer type
|
| 541 |
`V*`, and `V(*)[]` is convertible to `element_type(*)[]`.
|
| 542 |
|
| 543 |
``` cpp
|
| 544 |
-
template<class U> unique_ptr(U p, see below d) noexcept;
|
| 545 |
-
template<class U> unique_ptr(U p, see below d) noexcept;
|
| 546 |
```
|
| 547 |
|
| 548 |
These constructors behave the same as the constructors in the primary
|
| 549 |
template that take a parameter of type `pointer` and a second parameter.
|
| 550 |
|
|
@@ -554,11 +554,11 @@ template that take a parameter of type `pointer` and a second parameter.
|
|
| 554 |
- `U` is `nullptr_t`, or
|
| 555 |
- `pointer` is the same type as `element_type*`, `U` is a pointer type
|
| 556 |
`V*`, and `V(*)[]` is convertible to `element_type(*)[]`.
|
| 557 |
|
| 558 |
``` cpp
|
| 559 |
-
template<class U, class E> unique_ptr(unique_ptr<U, E>&& u) noexcept;
|
| 560 |
```
|
| 561 |
|
| 562 |
This constructor behaves the same as in the primary template.
|
| 563 |
|
| 564 |
*Constraints:* Where `UP` is `unique_ptr<U, E>`:
|
|
@@ -574,11 +574,11 @@ This constructor behaves the same as in the primary template.
|
|
| 574 |
primary template. — *end note*]
|
| 575 |
|
| 576 |
##### Assignment <a id="unique.ptr.runtime.asgn">[[unique.ptr.runtime.asgn]]</a>
|
| 577 |
|
| 578 |
``` cpp
|
| 579 |
-
template<class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u)noexcept;
|
| 580 |
```
|
| 581 |
|
| 582 |
This operator behaves the same as in the primary template.
|
| 583 |
|
| 584 |
*Constraints:* Where `UP` is `unique_ptr<U, E>`:
|
|
@@ -593,28 +593,28 @@ This operator behaves the same as in the primary template.
|
|
| 593 |
primary template. — *end note*]
|
| 594 |
|
| 595 |
##### Observers <a id="unique.ptr.runtime.observers">[[unique.ptr.runtime.observers]]</a>
|
| 596 |
|
| 597 |
``` cpp
|
| 598 |
-
T& operator[](size_t i) const;
|
| 599 |
```
|
| 600 |
|
| 601 |
*Preconditions:* `i` < the number of elements in the array to which the
|
| 602 |
stored pointer points.
|
| 603 |
|
| 604 |
*Returns:* `get()[i]`.
|
| 605 |
|
| 606 |
##### Modifiers <a id="unique.ptr.runtime.modifiers">[[unique.ptr.runtime.modifiers]]</a>
|
| 607 |
|
| 608 |
``` cpp
|
| 609 |
-
void reset(nullptr_t p = nullptr) noexcept;
|
| 610 |
```
|
| 611 |
|
| 612 |
*Effects:* Equivalent to `reset(pointer())`.
|
| 613 |
|
| 614 |
``` cpp
|
| 615 |
-
template<class U> void reset(U p) noexcept;
|
| 616 |
```
|
| 617 |
|
| 618 |
This function behaves the same as the `reset` member of the primary
|
| 619 |
template.
|
| 620 |
|
|
@@ -625,19 +625,19 @@ template.
|
|
| 625 |
`V*`, and `V(*)[]` is convertible to `element_type(*)[]`.
|
| 626 |
|
| 627 |
#### Creation <a id="unique.ptr.create">[[unique.ptr.create]]</a>
|
| 628 |
|
| 629 |
``` cpp
|
| 630 |
-
template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args);
|
| 631 |
```
|
| 632 |
|
| 633 |
*Constraints:* `T` is not an array type.
|
| 634 |
|
| 635 |
*Returns:* `unique_ptr<T>(new T(std::forward<Args>(args)...))`.
|
| 636 |
|
| 637 |
``` cpp
|
| 638 |
-
template<class T> unique_ptr<T> make_unique(size_t n);
|
| 639 |
```
|
| 640 |
|
| 641 |
*Constraints:* `T` is an array of unknown bound.
|
| 642 |
|
| 643 |
*Returns:* `unique_ptr<T>(new remove_extent_t<T>[n]())`.
|
|
@@ -647,19 +647,19 @@ template<class T, class... Args> unspecified make_unique(Args&&...) = delete;
|
|
| 647 |
```
|
| 648 |
|
| 649 |
*Constraints:* `T` is an array of known bound.
|
| 650 |
|
| 651 |
``` cpp
|
| 652 |
-
template<class T> unique_ptr<T> make_unique_for_overwrite();
|
| 653 |
```
|
| 654 |
|
| 655 |
*Constraints:* `T` is not an array type.
|
| 656 |
|
| 657 |
*Returns:* `unique_ptr<T>(new T)`.
|
| 658 |
|
| 659 |
``` cpp
|
| 660 |
-
template<class T> unique_ptr<T> make_unique_for_overwrite(size_t n);
|
| 661 |
```
|
| 662 |
|
| 663 |
*Constraints:* `T` is an array of unknown bound.
|
| 664 |
|
| 665 |
*Returns:* `unique_ptr<T>(new remove_extent_t<T>[n])`.
|
|
@@ -671,20 +671,20 @@ template<class T, class... Args> unspecified make_unique_for_overwrite(Args&&...
|
|
| 671 |
*Constraints:* `T` is an array of known bound.
|
| 672 |
|
| 673 |
#### Specialized algorithms <a id="unique.ptr.special">[[unique.ptr.special]]</a>
|
| 674 |
|
| 675 |
``` cpp
|
| 676 |
-
template<class T, class D> void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
|
| 677 |
```
|
| 678 |
|
| 679 |
*Constraints:* `is_swappable_v<D>` is `true`.
|
| 680 |
|
| 681 |
*Effects:* Calls `x.swap(y)`.
|
| 682 |
|
| 683 |
``` cpp
|
| 684 |
template<class T1, class D1, class T2, class D2>
|
| 685 |
-
bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
|
| 686 |
```
|
| 687 |
|
| 688 |
*Returns:* `x.get() == y.get()`.
|
| 689 |
|
| 690 |
``` cpp
|
|
@@ -742,20 +742,20 @@ template<class T1, class D1, class T2, class D2>
|
|
| 742 |
|
| 743 |
*Returns:* `compare_three_way()(x.get(), y.get())`.
|
| 744 |
|
| 745 |
``` cpp
|
| 746 |
template<class T, class D>
|
| 747 |
-
bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
|
| 748 |
```
|
| 749 |
|
| 750 |
*Returns:* `!x`.
|
| 751 |
|
| 752 |
``` cpp
|
| 753 |
template<class T, class D>
|
| 754 |
-
bool operator<(const unique_ptr<T, D>& x, nullptr_t);
|
| 755 |
template<class T, class D>
|
| 756 |
-
bool operator<(nullptr_t, const unique_ptr<T, D>& x);
|
| 757 |
```
|
| 758 |
|
| 759 |
*Preconditions:* The specialization `less<unique_ptr<T, D>::pointer>` is
|
| 760 |
a function object type [[function.objects]] that induces a strict weak
|
| 761 |
ordering [[alg.sorting]] on the pointer values.
|
|
@@ -772,46 +772,50 @@ The second function template returns
|
|
| 772 |
less<unique_ptr<T, D>::pointer>()(nullptr, x.get())
|
| 773 |
```
|
| 774 |
|
| 775 |
``` cpp
|
| 776 |
template<class T, class D>
|
| 777 |
-
bool operator>(const unique_ptr<T, D>& x, nullptr_t);
|
| 778 |
template<class T, class D>
|
| 779 |
-
bool operator>(nullptr_t, const unique_ptr<T, D>& x);
|
| 780 |
```
|
| 781 |
|
| 782 |
*Returns:* The first function template returns `nullptr < x`. The second
|
| 783 |
function template returns `x < nullptr`.
|
| 784 |
|
| 785 |
``` cpp
|
| 786 |
template<class T, class D>
|
| 787 |
-
bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
|
| 788 |
template<class T, class D>
|
| 789 |
-
bool operator<=(nullptr_t, const unique_ptr<T, D>& x);
|
| 790 |
```
|
| 791 |
|
| 792 |
*Returns:* The first function template returns `!(nullptr < x)`. The
|
| 793 |
second function template returns `!(x < nullptr)`.
|
| 794 |
|
| 795 |
``` cpp
|
| 796 |
template<class T, class D>
|
| 797 |
-
bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
|
| 798 |
template<class T, class D>
|
| 799 |
-
bool operator>=(nullptr_t, const unique_ptr<T, D>& x);
|
| 800 |
```
|
| 801 |
|
| 802 |
*Returns:* The first function template returns `!(x < nullptr)`. The
|
| 803 |
second function template returns `!(nullptr < x)`.
|
| 804 |
|
| 805 |
``` cpp
|
| 806 |
template<class T, class D>
|
| 807 |
-
requires
|
| 808 |
-
compare_three_way_result_t<typename unique_ptr<T, D>::pointer
|
| 809 |
operator<=>(const unique_ptr<T, D>& x, nullptr_t);
|
| 810 |
```
|
| 811 |
|
| 812 |
-
*Returns:*
|
|
|
|
|
|
|
|
|
|
|
|
|
| 813 |
|
| 814 |
#### I/O <a id="unique.ptr.io">[[unique.ptr.io]]</a>
|
| 815 |
|
| 816 |
``` cpp
|
| 817 |
template<class E, class T, class Y, class D>
|
|
@@ -822,11 +826,13 @@ template<class E, class T, class Y, class D>
|
|
| 822 |
|
| 823 |
*Effects:* Equivalent to: `os << p.get();`
|
| 824 |
|
| 825 |
*Returns:* `os`.
|
| 826 |
|
| 827 |
-
###
|
|
|
|
|
|
|
| 828 |
|
| 829 |
``` cpp
|
| 830 |
namespace std {
|
| 831 |
class bad_weak_ptr : public exception {
|
| 832 |
public:
|
|
@@ -843,11 +849,13 @@ constructor taking a `weak_ptr`.
|
|
| 843 |
const char* what() const noexcept override;
|
| 844 |
```
|
| 845 |
|
| 846 |
*Returns:* An *implementation-defined* NTBS.
|
| 847 |
|
| 848 |
-
### Class template `shared_ptr` <a id="util.smartptr.shared">[[util.smartptr.shared]]</a>
|
|
|
|
|
|
|
| 849 |
|
| 850 |
The `shared_ptr` class template stores a pointer, usually obtained via
|
| 851 |
`new`. `shared_ptr` implements semantics of shared ownership; the last
|
| 852 |
remaining owner of the pointer is responsible for destroying the object,
|
| 853 |
or otherwise releasing the resources associated with the stored pointer.
|
|
@@ -937,11 +945,11 @@ in standard containers. Specializations of `shared_ptr` shall be
|
|
| 937 |
contextually convertible to `bool`, allowing their use in boolean
|
| 938 |
expressions and declarations in conditions.
|
| 939 |
|
| 940 |
The template parameter `T` of `shared_ptr` may be an incomplete type.
|
| 941 |
|
| 942 |
-
[*Note 1*: `T`
|
| 943 |
|
| 944 |
[*Example 1*:
|
| 945 |
|
| 946 |
``` cpp
|
| 947 |
if (shared_ptr<X> px = dynamic_pointer_cast<X>(py)) {
|
|
@@ -959,11 +967,11 @@ races.
|
|
| 959 |
|
| 960 |
For the purposes of subclause [[smartptr]], a pointer type `Y*` is said
|
| 961 |
to be *compatible with* a pointer type `T*` when either `Y*` is
|
| 962 |
convertible to `T*` or `Y` is `U[N]` and `T` is cv `U[]`.
|
| 963 |
|
| 964 |
-
#### Constructors <a id="util.smartptr.shared.const">[[util.smartptr.shared.const]]</a>
|
| 965 |
|
| 966 |
In the constructor definitions below, enables `shared_from_this` with
|
| 967 |
`p`, for a pointer `p` of type `Y*`, means that if `Y` has an
|
| 968 |
unambiguous and accessible base class that is a specialization of
|
| 969 |
`enable_shared_from_this` [[util.smartptr.enab]], then `remove_cv_t<Y>*`
|
|
@@ -987,18 +995,18 @@ constexpr shared_ptr() noexcept;
|
|
| 987 |
|
| 988 |
``` cpp
|
| 989 |
template<class Y> explicit shared_ptr(Y* p);
|
| 990 |
```
|
| 991 |
|
| 992 |
-
*Mandates:* `Y` is a complete type.
|
| 993 |
-
|
| 994 |
*Constraints:* When `T` is an array type, the expression `delete[] p` is
|
| 995 |
well-formed and either `T` is `U[N]` and `Y(*)[N]` is convertible to
|
| 996 |
`T*`, or `T` is `U[]` and `Y(*)[]` is convertible to `T*`. When `T` is
|
| 997 |
not an array type, the expression `delete p` is well-formed and `Y*` is
|
| 998 |
convertible to `T*`.
|
| 999 |
|
|
|
|
|
|
|
| 1000 |
*Preconditions:* The expression `delete[] p`, when `T` is an array type,
|
| 1001 |
or `delete p`, when `T` is not an array type, has well-defined behavior,
|
| 1002 |
and does not throw exceptions.
|
| 1003 |
|
| 1004 |
*Effects:* When `T` is not an array type, constructs a `shared_ptr`
|
|
@@ -1009,11 +1017,11 @@ with `p`. If an exception is thrown, `delete p` is called when `T` is
|
|
| 1009 |
not an array type, `delete[] p` otherwise.
|
| 1010 |
|
| 1011 |
*Ensures:* `use_count() == 1 && get() == p`.
|
| 1012 |
|
| 1013 |
*Throws:* `bad_alloc`, or an *implementation-defined* exception when a
|
| 1014 |
-
resource other than memory
|
| 1015 |
|
| 1016 |
``` cpp
|
| 1017 |
template<class Y, class D> shared_ptr(Y* p, D d);
|
| 1018 |
template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
|
| 1019 |
template<class D> shared_ptr(nullptr_t p, D d);
|
|
@@ -1029,22 +1037,23 @@ well-formed expression. For the first two overloads:
|
|
| 1029 |
- If `T` is not an array type, then `Y*` is convertible to `T*`.
|
| 1030 |
|
| 1031 |
*Preconditions:* Construction of `d` and a deleter of type `D`
|
| 1032 |
initialized with `std::move(d)` do not throw exceptions. The expression
|
| 1033 |
`d(p)` has well-defined behavior and does not throw exceptions. `A`
|
| 1034 |
-
meets the *Cpp17Allocator*
|
|
|
|
| 1035 |
|
| 1036 |
*Effects:* Constructs a `shared_ptr` object that owns the object `p` and
|
| 1037 |
the deleter `d`. When `T` is not an array type, the first and second
|
| 1038 |
constructors enable `shared_from_this` with `p`. The second and fourth
|
| 1039 |
constructors shall use a copy of `a` to allocate memory for internal
|
| 1040 |
use. If an exception is thrown, `d(p)` is called.
|
| 1041 |
|
| 1042 |
*Ensures:* `use_count() == 1 && get() == p`.
|
| 1043 |
|
| 1044 |
*Throws:* `bad_alloc`, or an *implementation-defined* exception when a
|
| 1045 |
-
resource other than memory
|
| 1046 |
|
| 1047 |
``` cpp
|
| 1048 |
template<class Y> shared_ptr(const shared_ptr<Y>& r, element_type* p) noexcept;
|
| 1049 |
template<class Y> shared_ptr(shared_ptr<Y>&& r, element_type* p) noexcept;
|
| 1050 |
```
|
|
@@ -1053,13 +1062,13 @@ template<class Y> shared_ptr(shared_ptr<Y>&& r, element_type* p) noexcept;
|
|
| 1053 |
ownership with the initial value of `r`.
|
| 1054 |
|
| 1055 |
*Ensures:* `get() == p`. For the second overload, `r` is empty and
|
| 1056 |
`r.get() == nullptr`.
|
| 1057 |
|
| 1058 |
-
[*Note 1*:
|
| 1059 |
-
|
| 1060 |
-
|
| 1061 |
|
| 1062 |
[*Note 2*: This constructor allows creation of an empty `shared_ptr`
|
| 1063 |
instance with a non-null stored pointer. — *end note*]
|
| 1064 |
|
| 1065 |
``` cpp
|
|
@@ -1082,12 +1091,12 @@ template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
|
|
| 1082 |
|
| 1083 |
*Constraints:* For the second constructor, `Y*` is compatible with `T*`.
|
| 1084 |
|
| 1085 |
*Effects:* Move constructs a `shared_ptr` instance from `r`.
|
| 1086 |
|
| 1087 |
-
*Ensures:* `*this`
|
| 1088 |
-
|
| 1089 |
|
| 1090 |
``` cpp
|
| 1091 |
template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
|
| 1092 |
```
|
| 1093 |
|
|
@@ -1108,15 +1117,15 @@ template<class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
|
|
| 1108 |
*Constraints:* `Y*` is compatible with `T*` and
|
| 1109 |
`unique_ptr<Y, D>::pointer` is convertible to `element_type*`.
|
| 1110 |
|
| 1111 |
*Effects:* If `r.get() == nullptr`, equivalent to `shared_ptr()`.
|
| 1112 |
Otherwise, if `D` is not a reference type, equivalent to
|
| 1113 |
-
`shared_ptr(r.release(), r.get_deleter())`. Otherwise,
|
| 1114 |
-
`shared_ptr(r.release(), ref(r.get_deleter()))`. If an
|
| 1115 |
-
thrown, the constructor has no effect.
|
| 1116 |
|
| 1117 |
-
#### Destructor <a id="util.smartptr.shared.dest">[[util.smartptr.shared.dest]]</a>
|
| 1118 |
|
| 1119 |
``` cpp
|
| 1120 |
~shared_ptr();
|
| 1121 |
```
|
| 1122 |
|
|
@@ -1126,42 +1135,42 @@ thrown, the constructor has no effect.
|
|
| 1126 |
instance (`use_count() > 1`), there are no side effects.
|
| 1127 |
- Otherwise, if `*this` owns an object `p` and a deleter `d`, `d(p)` is
|
| 1128 |
called.
|
| 1129 |
- Otherwise, `*this` owns a pointer `p`, and `delete p` is called.
|
| 1130 |
|
| 1131 |
-
[*Note
|
| 1132 |
instances that share ownership with `*this` by one, after `*this` has
|
| 1133 |
been destroyed all `shared_ptr` instances that shared ownership with
|
| 1134 |
`*this` will report a `use_count()` that is one less than its previous
|
| 1135 |
value. — *end note*]
|
| 1136 |
|
| 1137 |
-
#### Assignment <a id="util.smartptr.shared.assign">[[util.smartptr.shared.assign]]</a>
|
| 1138 |
|
| 1139 |
``` cpp
|
| 1140 |
shared_ptr& operator=(const shared_ptr& r) noexcept;
|
| 1141 |
template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
|
| 1142 |
```
|
| 1143 |
|
| 1144 |
*Effects:* Equivalent to `shared_ptr(r).swap(*this)`.
|
| 1145 |
|
| 1146 |
*Returns:* `*this`.
|
| 1147 |
|
| 1148 |
-
[*Note
|
| 1149 |
|
| 1150 |
The use count updates caused by the temporary object construction and
|
| 1151 |
-
destruction are not observable side effects, so the implementation
|
| 1152 |
meet the effects (and the implied guarantees) via different means,
|
| 1153 |
without creating a temporary. In particular, in the example:
|
| 1154 |
|
| 1155 |
``` cpp
|
| 1156 |
shared_ptr<int> p(new int);
|
| 1157 |
shared_ptr<void> q(p);
|
| 1158 |
p = p;
|
| 1159 |
q = p;
|
| 1160 |
```
|
| 1161 |
|
| 1162 |
-
both assignments
|
| 1163 |
|
| 1164 |
— *end note*]
|
| 1165 |
|
| 1166 |
``` cpp
|
| 1167 |
shared_ptr& operator=(shared_ptr&& r) noexcept;
|
|
@@ -1178,11 +1187,11 @@ template<class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
|
|
| 1178 |
|
| 1179 |
*Effects:* Equivalent to `shared_ptr(std::move(r)).swap(*this)`.
|
| 1180 |
|
| 1181 |
*Returns:* `*this`.
|
| 1182 |
|
| 1183 |
-
#### Modifiers <a id="util.smartptr.shared.mod">[[util.smartptr.shared.mod]]</a>
|
| 1184 |
|
| 1185 |
``` cpp
|
| 1186 |
void swap(shared_ptr& r) noexcept;
|
| 1187 |
```
|
| 1188 |
|
|
@@ -1210,11 +1219,11 @@ template<class Y, class D> void reset(Y* p, D d);
|
|
| 1210 |
template<class Y, class D, class A> void reset(Y* p, D d, A a);
|
| 1211 |
```
|
| 1212 |
|
| 1213 |
*Effects:* Equivalent to `shared_ptr(p, d, a).swap(*this)`.
|
| 1214 |
|
| 1215 |
-
#### Observers <a id="util.smartptr.shared.obs">[[util.smartptr.shared.obs]]</a>
|
| 1216 |
|
| 1217 |
``` cpp
|
| 1218 |
element_type* get() const noexcept;
|
| 1219 |
```
|
| 1220 |
|
|
@@ -1222,11 +1231,11 @@ element_type* get() const noexcept;
|
|
| 1222 |
|
| 1223 |
``` cpp
|
| 1224 |
T& operator*() const noexcept;
|
| 1225 |
```
|
| 1226 |
|
| 1227 |
-
*Preconditions:* `get() !=
|
| 1228 |
|
| 1229 |
*Returns:* `*get()`.
|
| 1230 |
|
| 1231 |
*Remarks:* When `T` is an array type or cv `void`, it is unspecified
|
| 1232 |
whether this member function is declared. If it is declared, it is
|
|
@@ -1236,11 +1245,11 @@ well-formed.
|
|
| 1236 |
|
| 1237 |
``` cpp
|
| 1238 |
T* operator->() const noexcept;
|
| 1239 |
```
|
| 1240 |
|
| 1241 |
-
*Preconditions:* `get() !=
|
| 1242 |
|
| 1243 |
*Returns:* `get()`.
|
| 1244 |
|
| 1245 |
*Remarks:* When `T` is an array type, it is unspecified whether this
|
| 1246 |
member function is declared. If it is declared, it is unspecified what
|
|
@@ -1249,47 +1258,47 @@ necessarily the definition) of the function shall be well-formed.
|
|
| 1249 |
|
| 1250 |
``` cpp
|
| 1251 |
element_type& operator[](ptrdiff_t i) const;
|
| 1252 |
```
|
| 1253 |
|
| 1254 |
-
*Preconditions:* `get() !=
|
|
|
|
| 1255 |
|
| 1256 |
*Returns:* `get()[i]`.
|
| 1257 |
|
| 1258 |
-
*Remarks:* When `T` is not an array type, it is unspecified whether this
|
| 1259 |
-
member function is declared. If it is declared, it is unspecified what
|
| 1260 |
-
its return type is, except that the declaration (although not
|
| 1261 |
-
necessarily the definition) of the function shall be well-formed.
|
| 1262 |
-
|
| 1263 |
*Throws:* Nothing.
|
| 1264 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1265 |
``` cpp
|
| 1266 |
long use_count() const noexcept;
|
| 1267 |
```
|
| 1268 |
|
|
|
|
|
|
|
| 1269 |
*Returns:* The number of `shared_ptr` objects, `*this` included, that
|
| 1270 |
share ownership with `*this`, or `0` when `*this` is empty.
|
| 1271 |
|
| 1272 |
-
*
|
| 1273 |
-
|
| 1274 |
-
[*Note 1*: `get() == nullptr` does not imply a specific return value of
|
| 1275 |
`use_count()`. — *end note*]
|
| 1276 |
|
| 1277 |
-
[*Note
|
| 1278 |
`use_count()`. — *end note*]
|
| 1279 |
|
| 1280 |
-
[*Note
|
| 1281 |
-
`use_count()`, the result
|
| 1282 |
-
|
| 1283 |
-
|
| 1284 |
-
completed. — *end note*]
|
| 1285 |
|
| 1286 |
``` cpp
|
| 1287 |
explicit operator bool() const noexcept;
|
| 1288 |
```
|
| 1289 |
|
| 1290 |
-
*Returns:* `get() !=
|
| 1291 |
|
| 1292 |
``` cpp
|
| 1293 |
template<class U> bool owner_before(const shared_ptr<U>& b) const noexcept;
|
| 1294 |
template<class U> bool owner_before(const weak_ptr<U>& b) const noexcept;
|
| 1295 |
```
|
|
@@ -1301,11 +1310,11 @@ template<class U> bool owner_before(const weak_ptr<U>& b) const noexcept;
|
|
| 1301 |
- under the equivalence relation defined by `owner_before`,
|
| 1302 |
`!a.owner_before(b) && !b.owner_before(a)`, two `shared_ptr` or
|
| 1303 |
`weak_ptr` instances are equivalent if and only if they share
|
| 1304 |
ownership or are both empty.
|
| 1305 |
|
| 1306 |
-
#### Creation <a id="util.smartptr.shared.create">[[util.smartptr.shared.create]]</a>
|
| 1307 |
|
| 1308 |
The common requirements that apply to all `make_shared`,
|
| 1309 |
`allocate_shared`, `make_shared_for_overwrite`, and
|
| 1310 |
`allocate_shared_for_overwrite` overloads, unless specified otherwise,
|
| 1311 |
are described below.
|
|
@@ -1319,34 +1328,34 @@ template<class T, ...>
|
|
| 1319 |
shared_ptr<T> make_shared_for_overwrite(args);
|
| 1320 |
template<class T, class A, ...>
|
| 1321 |
shared_ptr<T> allocate_shared_for_overwrite(const A& a, args);
|
| 1322 |
```
|
| 1323 |
|
| 1324 |
-
*Preconditions:* `A` meets the *Cpp17Allocator*
|
| 1325 |
-
|
| 1326 |
|
| 1327 |
*Effects:* Allocates memory for an object of type `T` (or `U[N]` when
|
| 1328 |
`T` is `U[]`, where `N` is determined from *args* as specified by the
|
| 1329 |
concrete overload). The object is initialized from *args* as specified
|
| 1330 |
by the concrete overload. The `allocate_shared` and
|
| 1331 |
`allocate_shared_for_overwrite` templates use a copy of `a` (rebound for
|
| 1332 |
an unspecified `value_type`) to allocate memory. If an exception is
|
| 1333 |
thrown, the functions have no effect.
|
| 1334 |
|
|
|
|
|
|
|
|
|
|
| 1335 |
*Returns:* A `shared_ptr` instance that stores and owns the address of
|
| 1336 |
the newly constructed object.
|
| 1337 |
|
| 1338 |
-
*Ensures:* `r.get() != 0 && r.use_count() == 1`, where `r` is the return
|
| 1339 |
-
value.
|
| 1340 |
-
|
| 1341 |
*Throws:* `bad_alloc`, or an exception thrown from `allocate` or from
|
| 1342 |
the initialization of the object.
|
| 1343 |
|
| 1344 |
*Remarks:*
|
| 1345 |
|
| 1346 |
- Implementations should perform no more than one memory allocation.
|
| 1347 |
-
\[*Note
|
| 1348 |
pointer. — *end note*]
|
| 1349 |
- When an object of an array type `U` is specified to have an initial
|
| 1350 |
value of `u` (of the same type), this shall be interpreted to mean
|
| 1351 |
that each array element of the object has as its initial value the
|
| 1352 |
corresponding element from `u`.
|
|
@@ -1398,11 +1407,11 @@ the initialization of the object.
|
|
| 1398 |
expression `allocator_traits<A2>::destroy(a2, pv)` where `pv` points
|
| 1399 |
to that object of type `remove_cv_t<U>` and `a2` of type `A2` is a
|
| 1400 |
rebound copy of the allocator `a` passed to `allocate_shared` such
|
| 1401 |
that its `value_type` is `remove_cv_t<U>`.
|
| 1402 |
|
| 1403 |
-
[*Note
|
| 1404 |
`sizeof(T)` to allow for internal bookkeeping structures such as
|
| 1405 |
reference counts. — *end note*]
|
| 1406 |
|
| 1407 |
``` cpp
|
| 1408 |
template<class T, class... Args>
|
|
@@ -1412,11 +1421,11 @@ template<class T, class A, class... Args>
|
|
| 1412 |
```
|
| 1413 |
|
| 1414 |
*Constraints:* `T` is not an array type.
|
| 1415 |
|
| 1416 |
*Returns:* A `shared_ptr` to an object of type `T` with an initial value
|
| 1417 |
-
`T(forward<Args>(args)...)`.
|
| 1418 |
|
| 1419 |
*Remarks:* The `shared_ptr` constructors called by these functions
|
| 1420 |
enable `shared_from_this` with the address of the newly constructed
|
| 1421 |
object of type `T`.
|
| 1422 |
|
|
@@ -1572,11 +1581,11 @@ shared_ptr<double[]> p = make_shared_for_overwrite<double[]>(1024);
|
|
| 1572 |
// shared_ptr to a default-initialized double[1024], where each element has an indeterminate value
|
| 1573 |
```
|
| 1574 |
|
| 1575 |
— *end example*]
|
| 1576 |
|
| 1577 |
-
#### Comparison <a id="util.smartptr.shared.cmp">[[util.smartptr.shared.cmp]]</a>
|
| 1578 |
|
| 1579 |
``` cpp
|
| 1580 |
template<class T, class U>
|
| 1581 |
bool operator==(const shared_ptr<T>& a, const shared_ptr<U>& b) noexcept;
|
| 1582 |
```
|
|
@@ -1595,30 +1604,34 @@ template<class T, class U>
|
|
| 1595 |
strong_ordering operator<=>(const shared_ptr<T>& a, const shared_ptr<U>& b) noexcept;
|
| 1596 |
```
|
| 1597 |
|
| 1598 |
*Returns:* `compare_three_way()(a.get(), b.get())`.
|
| 1599 |
|
| 1600 |
-
[*Note
|
| 1601 |
-
to be used as keys in associative containers. — *end note*]
|
| 1602 |
|
| 1603 |
``` cpp
|
| 1604 |
template<class T>
|
| 1605 |
strong_ordering operator<=>(const shared_ptr<T>& a, nullptr_t) noexcept;
|
| 1606 |
```
|
| 1607 |
|
| 1608 |
-
*Returns:*
|
| 1609 |
|
| 1610 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1611 |
|
| 1612 |
``` cpp
|
| 1613 |
template<class T>
|
| 1614 |
void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
|
| 1615 |
```
|
| 1616 |
|
| 1617 |
*Effects:* Equivalent to `a.swap(b)`.
|
| 1618 |
|
| 1619 |
-
#### Casts <a id="util.smartptr.shared.cast">[[util.smartptr.shared.cast]]</a>
|
| 1620 |
|
| 1621 |
``` cpp
|
| 1622 |
template<class T, class U>
|
| 1623 |
shared_ptr<T> static_pointer_cast(const shared_ptr<U>& r) noexcept;
|
| 1624 |
template<class T, class U>
|
|
@@ -1635,11 +1648,11 @@ shared_ptr<T>(R, static_cast<typename shared_ptr<T>::element_type*>(r.get()))
|
|
| 1635 |
```
|
| 1636 |
|
| 1637 |
where *`R`* is `r` for the first overload, and `std::move(r)` for the
|
| 1638 |
second.
|
| 1639 |
|
| 1640 |
-
[*Note
|
| 1641 |
`shared_ptr<T>(static_cast<T*>(r.get()))` will eventually result in
|
| 1642 |
undefined behavior, attempting to delete the same object
|
| 1643 |
twice. — *end note*]
|
| 1644 |
|
| 1645 |
``` cpp
|
|
@@ -1649,12 +1662,12 @@ template<class T, class U>
|
|
| 1649 |
shared_ptr<T> dynamic_pointer_cast(shared_ptr<U>&& r) noexcept;
|
| 1650 |
```
|
| 1651 |
|
| 1652 |
*Mandates:* The expression `dynamic_cast<T*>((U*)nullptr)` is
|
| 1653 |
well-formed. The expression
|
| 1654 |
-
`dynamic_cast<typename shared_ptr<T>::element_type*>(r.get())` is
|
| 1655 |
-
formed.
|
| 1656 |
|
| 1657 |
*Preconditions:* The expression
|
| 1658 |
`dynamic_cast<typename shared_ptr<T>::element_type*>(r.get())` has
|
| 1659 |
well-defined behavior.
|
| 1660 |
|
|
@@ -1663,11 +1676,11 @@ well-defined behavior.
|
|
| 1663 |
- When `dynamic_cast<typename shared_ptr<T>::element_type*>(r.get())`
|
| 1664 |
returns a non-null value `p`, `shared_ptr<T>(`*`R`*`, p)`, where *`R`*
|
| 1665 |
is `r` for the first overload, and `std::move(r)` for the second.
|
| 1666 |
- Otherwise, `shared_ptr<T>()`.
|
| 1667 |
|
| 1668 |
-
[*Note
|
| 1669 |
`shared_ptr<T>(dynamic_cast<T*>(r.get()))` will eventually result in
|
| 1670 |
undefined behavior, attempting to delete the same object
|
| 1671 |
twice. — *end note*]
|
| 1672 |
|
| 1673 |
``` cpp
|
|
@@ -1686,11 +1699,11 @@ shared_ptr<T>(R, const_cast<typename shared_ptr<T>::element_type*>(r.get()))
|
|
| 1686 |
```
|
| 1687 |
|
| 1688 |
where *`R`* is `r` for the first overload, and `std::move(r)` for the
|
| 1689 |
second.
|
| 1690 |
|
| 1691 |
-
[*Note
|
| 1692 |
`shared_ptr<T>(const_cast<T*>(r.get()))` will eventually result in
|
| 1693 |
undefined behavior, attempting to delete the same object
|
| 1694 |
twice. — *end note*]
|
| 1695 |
|
| 1696 |
``` cpp
|
|
@@ -1710,16 +1723,16 @@ shared_ptr<T>(R, reinterpret_cast<typename shared_ptr<T>::element_type*>(r.get()
|
|
| 1710 |
```
|
| 1711 |
|
| 1712 |
where *`R`* is `r` for the first overload, and `std::move(r)` for the
|
| 1713 |
second.
|
| 1714 |
|
| 1715 |
-
[*Note
|
| 1716 |
`shared_ptr<T>(reinterpret_cast<T*>(r.get()))` will eventually result in
|
| 1717 |
undefined behavior, attempting to delete the same object
|
| 1718 |
twice. — *end note*]
|
| 1719 |
|
| 1720 |
-
#### `get_deleter` <a id="util.smartptr.getdeleter">[[util.smartptr.getdeleter]]</a>
|
| 1721 |
|
| 1722 |
``` cpp
|
| 1723 |
template<class D, class T>
|
| 1724 |
D* get_deleter(const shared_ptr<T>& p) noexcept;
|
| 1725 |
```
|
|
@@ -1727,27 +1740,29 @@ template<class D, class T>
|
|
| 1727 |
*Returns:* If `p` owns a deleter `d` of type cv-unqualified `D`, returns
|
| 1728 |
`addressof(d)`; otherwise returns `nullptr`. The returned pointer
|
| 1729 |
remains valid as long as there exists a `shared_ptr` instance that owns
|
| 1730 |
`d`.
|
| 1731 |
|
| 1732 |
-
[*Note
|
| 1733 |
than that. This can happen if the implementation doesn’t destroy the
|
| 1734 |
deleter until all `weak_ptr` instances that share ownership with `p`
|
| 1735 |
have been destroyed. — *end note*]
|
| 1736 |
|
| 1737 |
-
#### I/O <a id="util.smartptr.shared.io">[[util.smartptr.shared.io]]</a>
|
| 1738 |
|
| 1739 |
``` cpp
|
| 1740 |
template<class E, class T, class Y>
|
| 1741 |
basic_ostream<E, T>& operator<<(basic_ostream<E, T>& os, const shared_ptr<Y>& p);
|
| 1742 |
```
|
| 1743 |
|
| 1744 |
*Effects:* As if by: `os << p.get();`
|
| 1745 |
|
| 1746 |
*Returns:* `os`.
|
| 1747 |
|
| 1748 |
-
### Class template `weak_ptr` <a id="util.smartptr.weak">[[util.smartptr.weak]]</a>
|
|
|
|
|
|
|
| 1749 |
|
| 1750 |
The `weak_ptr` class template stores a weak reference to an object that
|
| 1751 |
is already managed by a `shared_ptr`. To access the object, a `weak_ptr`
|
| 1752 |
can be converted to a `shared_ptr` using the member function `lock`.
|
| 1753 |
|
|
@@ -1795,28 +1810,25 @@ namespace std {
|
|
| 1795 |
bool owner_before(const weak_ptr<U>& b) const noexcept;
|
| 1796 |
};
|
| 1797 |
|
| 1798 |
template<class T>
|
| 1799 |
weak_ptr(shared_ptr<T>) -> weak_ptr<T>;
|
| 1800 |
-
|
| 1801 |
-
// [util.smartptr.weak.spec], specialized algorithms
|
| 1802 |
-
template<class T>
|
| 1803 |
-
void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
|
| 1804 |
}
|
| 1805 |
```
|
| 1806 |
|
| 1807 |
Specializations of `weak_ptr` shall be *Cpp17CopyConstructible* and
|
| 1808 |
*Cpp17CopyAssignable*, allowing their use in standard containers. The
|
| 1809 |
template parameter `T` of `weak_ptr` may be an incomplete type.
|
| 1810 |
|
| 1811 |
-
#### Constructors <a id="util.smartptr.weak.const">[[util.smartptr.weak.const]]</a>
|
| 1812 |
|
| 1813 |
``` cpp
|
| 1814 |
constexpr weak_ptr() noexcept;
|
| 1815 |
```
|
| 1816 |
|
| 1817 |
-
*Effects:* Constructs an empty `weak_ptr` object
|
|
|
|
| 1818 |
|
| 1819 |
*Ensures:* `use_count() == 0`.
|
| 1820 |
|
| 1821 |
``` cpp
|
| 1822 |
weak_ptr(const weak_ptr& r) noexcept;
|
|
@@ -1825,13 +1837,14 @@ template<class Y> weak_ptr(const shared_ptr<Y>& r) noexcept;
|
|
| 1825 |
```
|
| 1826 |
|
| 1827 |
*Constraints:* For the second and third constructors, `Y*` is compatible
|
| 1828 |
with `T*`.
|
| 1829 |
|
| 1830 |
-
*Effects:* If `r` is empty, constructs an empty `weak_ptr` object
|
| 1831 |
-
otherwise, constructs a `weak_ptr` object
|
| 1832 |
-
and stores a copy of the pointer stored
|
|
|
|
| 1833 |
|
| 1834 |
*Ensures:* `use_count() == r.use_count()`.
|
| 1835 |
|
| 1836 |
``` cpp
|
| 1837 |
weak_ptr(weak_ptr&& r) noexcept;
|
|
@@ -1840,47 +1853,47 @@ template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept;
|
|
| 1840 |
|
| 1841 |
*Constraints:* For the second constructor, `Y*` is compatible with `T*`.
|
| 1842 |
|
| 1843 |
*Effects:* Move constructs a `weak_ptr` instance from `r`.
|
| 1844 |
|
| 1845 |
-
*Ensures:* `*this`
|
| 1846 |
-
|
| 1847 |
|
| 1848 |
-
#### Destructor <a id="util.smartptr.weak.dest">[[util.smartptr.weak.dest]]</a>
|
| 1849 |
|
| 1850 |
``` cpp
|
| 1851 |
~weak_ptr();
|
| 1852 |
```
|
| 1853 |
|
| 1854 |
*Effects:* Destroys this `weak_ptr` object but has no effect on the
|
| 1855 |
object its stored pointer points to.
|
| 1856 |
|
| 1857 |
-
#### Assignment <a id="util.smartptr.weak.assign">[[util.smartptr.weak.assign]]</a>
|
| 1858 |
|
| 1859 |
``` cpp
|
| 1860 |
weak_ptr& operator=(const weak_ptr& r) noexcept;
|
| 1861 |
template<class Y> weak_ptr& operator=(const weak_ptr<Y>& r) noexcept;
|
| 1862 |
template<class Y> weak_ptr& operator=(const shared_ptr<Y>& r) noexcept;
|
| 1863 |
```
|
| 1864 |
|
| 1865 |
*Effects:* Equivalent to `weak_ptr(r).swap(*this)`.
|
| 1866 |
|
|
|
|
|
|
|
| 1867 |
*Remarks:* The implementation may meet the effects (and the implied
|
| 1868 |
guarantees) via different means, without creating a temporary object.
|
| 1869 |
|
| 1870 |
-
*Returns:* `*this`.
|
| 1871 |
-
|
| 1872 |
``` cpp
|
| 1873 |
weak_ptr& operator=(weak_ptr&& r) noexcept;
|
| 1874 |
template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept;
|
| 1875 |
```
|
| 1876 |
|
| 1877 |
*Effects:* Equivalent to `weak_ptr(std::move(r)).swap(*this)`.
|
| 1878 |
|
| 1879 |
*Returns:* `*this`.
|
| 1880 |
|
| 1881 |
-
#### Modifiers <a id="util.smartptr.weak.mod">[[util.smartptr.weak.mod]]</a>
|
| 1882 |
|
| 1883 |
``` cpp
|
| 1884 |
void swap(weak_ptr& r) noexcept;
|
| 1885 |
```
|
| 1886 |
|
|
@@ -1890,11 +1903,11 @@ void swap(weak_ptr& r) noexcept;
|
|
| 1890 |
void reset() noexcept;
|
| 1891 |
```
|
| 1892 |
|
| 1893 |
*Effects:* Equivalent to `weak_ptr().swap(*this)`.
|
| 1894 |
|
| 1895 |
-
#### Observers <a id="util.smartptr.weak.obs">[[util.smartptr.weak.obs]]</a>
|
| 1896 |
|
| 1897 |
``` cpp
|
| 1898 |
long use_count() const noexcept;
|
| 1899 |
```
|
| 1900 |
|
|
@@ -1926,20 +1939,20 @@ template<class U> bool owner_before(const weak_ptr<U>& b) const noexcept;
|
|
| 1926 |
- under the equivalence relation defined by `owner_before`,
|
| 1927 |
`!a.owner_before(b) && !b.owner_before(a)`, two `shared_ptr` or
|
| 1928 |
`weak_ptr` instances are equivalent if and only if they share
|
| 1929 |
ownership or are both empty.
|
| 1930 |
|
| 1931 |
-
#### Specialized algorithms <a id="util.smartptr.weak.spec">[[util.smartptr.weak.spec]]</a>
|
| 1932 |
|
| 1933 |
``` cpp
|
| 1934 |
template<class T>
|
| 1935 |
void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
|
| 1936 |
```
|
| 1937 |
|
| 1938 |
*Effects:* Equivalent to `a.swap(b)`.
|
| 1939 |
|
| 1940 |
-
### Class template `owner_less` <a id="util.smartptr.ownerless">[[util.smartptr.ownerless]]</a>
|
| 1941 |
|
| 1942 |
The class template `owner_less` allows ownership-based mixed comparisons
|
| 1943 |
of shared and weak pointers.
|
| 1944 |
|
| 1945 |
``` cpp
|
|
@@ -1979,18 +1992,18 @@ namespace std {
|
|
| 1979 |
|
| 1980 |
Note that
|
| 1981 |
|
| 1982 |
- `operator()` defines a strict weak ordering as defined in
|
| 1983 |
[[alg.sorting]];
|
| 1984 |
-
-
|
| 1985 |
-
|
| 1986 |
-
`
|
| 1987 |
ownership or are both empty.
|
| 1988 |
|
| 1989 |
— *end note*]
|
| 1990 |
|
| 1991 |
-
### Class template `enable_shared_from_this` <a id="util.smartptr.enab">[[util.smartptr.enab]]</a>
|
| 1992 |
|
| 1993 |
A class `T` can inherit from `enable_shared_from_this<T>` to inherit the
|
| 1994 |
`shared_from_this` member functions that obtain a `shared_ptr` instance
|
| 1995 |
pointing to `*this`.
|
| 1996 |
|
|
@@ -2080,5 +2093,363 @@ template<class T> struct hash<shared_ptr<T>>;
|
|
| 2080 |
|
| 2081 |
For an object `p` of type `shared_ptr<T>`, `hash<shared_ptr<T>>()(p)`
|
| 2082 |
evaluates to the same value as
|
| 2083 |
`hash<typename shared_ptr<T>::element_type*>()(p.get())`.
|
| 2084 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
## Smart pointers <a id="smartptr">[[smartptr]]</a>
|
| 2 |
|
| 3 |
+
### Unique-ownership pointers <a id="unique.ptr">[[unique.ptr]]</a>
|
| 4 |
+
|
| 5 |
+
#### General <a id="unique.ptr.general">[[unique.ptr.general]]</a>
|
| 6 |
|
| 7 |
A *unique pointer* is an object that owns another object and manages
|
| 8 |
that other object through a pointer. More precisely, a unique pointer is
|
| 9 |
an object *u* that stores a pointer to a second object *p* and will
|
| 10 |
dispose of *p* when *u* is itself destroyed (e.g., when leaving block
|
|
|
|
| 19 |
*u.p* and *u.d* with another pointer and deleter, but properly disposes
|
| 20 |
of its owned object via the associated deleter before such replacement
|
| 21 |
is considered completed.
|
| 22 |
|
| 23 |
Each object of a type `U` instantiated from the `unique_ptr` template
|
| 24 |
+
specified in [[unique.ptr]] has the strict ownership semantics,
|
| 25 |
specified above, of a unique pointer. In partial satisfaction of these
|
| 26 |
semantics, each such `U` is *Cpp17MoveConstructible* and
|
| 27 |
*Cpp17MoveAssignable*, but is not *Cpp17CopyConstructible* nor
|
| 28 |
*Cpp17CopyAssignable*. The template parameter `T` of `unique_ptr` may be
|
| 29 |
an incomplete type.
|
|
|
|
| 47 |
|
| 48 |
``` cpp
|
| 49 |
namespace std {
|
| 50 |
template<class T> struct default_delete {
|
| 51 |
constexpr default_delete() noexcept = default;
|
| 52 |
+
template<class U> constexpr default_delete(const default_delete<U>&) noexcept;
|
| 53 |
+
constexpr void operator()(T*) const;
|
| 54 |
};
|
| 55 |
}
|
| 56 |
```
|
| 57 |
|
| 58 |
``` cpp
|
| 59 |
+
template<class U> constexpr default_delete(const default_delete<U>& other) noexcept;
|
| 60 |
```
|
| 61 |
|
| 62 |
*Constraints:* `U*` is implicitly convertible to `T*`.
|
| 63 |
|
| 64 |
*Effects:* Constructs a `default_delete` object from another
|
| 65 |
`default_delete<U>` object.
|
| 66 |
|
| 67 |
``` cpp
|
| 68 |
+
constexpr void operator()(T* ptr) const;
|
| 69 |
```
|
| 70 |
|
| 71 |
*Mandates:* `T` is a complete type.
|
| 72 |
|
| 73 |
*Effects:* Calls `delete` on `ptr`.
|
|
|
|
| 76 |
|
| 77 |
``` cpp
|
| 78 |
namespace std {
|
| 79 |
template<class T> struct default_delete<T[]> {
|
| 80 |
constexpr default_delete() noexcept = default;
|
| 81 |
+
template<class U> constexpr default_delete(const default_delete<U[]>&) noexcept;
|
| 82 |
+
template<class U> constexpr void operator()(U* ptr) const;
|
| 83 |
};
|
| 84 |
}
|
| 85 |
```
|
| 86 |
|
| 87 |
``` cpp
|
| 88 |
+
template<class U> constexpr default_delete(const default_delete<U[]>& other) noexcept;
|
| 89 |
```
|
| 90 |
|
| 91 |
*Constraints:* `U(*)[]` is convertible to `T(*)[]`.
|
| 92 |
|
| 93 |
*Effects:* Constructs a `default_delete` object from another
|
| 94 |
`default_delete<U[]>` object.
|
| 95 |
|
| 96 |
``` cpp
|
| 97 |
+
template<class U> constexpr void operator()(U* ptr) const;
|
| 98 |
```
|
| 99 |
|
| 100 |
+
*Constraints:* `U(*)[]` is convertible to `T(*)[]`.
|
| 101 |
+
|
| 102 |
*Mandates:* `U` is a complete type.
|
| 103 |
|
|
|
|
|
|
|
| 104 |
*Effects:* Calls `delete[]` on `ptr`.
|
| 105 |
|
| 106 |
#### `unique_ptr` for single objects <a id="unique.ptr.single">[[unique.ptr.single]]</a>
|
| 107 |
|
| 108 |
+
##### General <a id="unique.ptr.single.general">[[unique.ptr.single.general]]</a>
|
| 109 |
+
|
| 110 |
``` cpp
|
| 111 |
namespace std {
|
| 112 |
template<class T, class D = default_delete<T>> class unique_ptr {
|
| 113 |
public:
|
| 114 |
using pointer = see below;
|
| 115 |
using element_type = T;
|
| 116 |
using deleter_type = D;
|
| 117 |
|
| 118 |
// [unique.ptr.single.ctor], constructors
|
| 119 |
constexpr unique_ptr() noexcept;
|
| 120 |
+
constexpr explicit unique_ptr(type_identity_t<pointer> p) noexcept;
|
| 121 |
+
constexpr unique_ptr(type_identity_t<pointer> p, see below d1) noexcept;
|
| 122 |
+
constexpr unique_ptr(type_identity_t<pointer> p, see below d2) noexcept;
|
| 123 |
+
constexpr unique_ptr(unique_ptr&& u) noexcept;
|
| 124 |
constexpr unique_ptr(nullptr_t) noexcept;
|
| 125 |
template<class U, class E>
|
| 126 |
+
constexpr unique_ptr(unique_ptr<U, E>&& u) noexcept;
|
| 127 |
|
| 128 |
// [unique.ptr.single.dtor], destructor
|
| 129 |
+
constexpr ~unique_ptr();
|
| 130 |
|
| 131 |
// [unique.ptr.single.asgn], assignment
|
| 132 |
+
constexpr unique_ptr& operator=(unique_ptr&& u) noexcept;
|
| 133 |
template<class U, class E>
|
| 134 |
+
constexpr unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
|
| 135 |
+
constexpr unique_ptr& operator=(nullptr_t) noexcept;
|
| 136 |
|
| 137 |
// [unique.ptr.single.observers], observers
|
| 138 |
+
constexpr add_lvalue_reference_t<T> operator*() const noexcept(see below);
|
| 139 |
+
constexpr pointer operator->() const noexcept;
|
| 140 |
+
constexpr pointer get() const noexcept;
|
| 141 |
+
constexpr deleter_type& get_deleter() noexcept;
|
| 142 |
+
constexpr const deleter_type& get_deleter() const noexcept;
|
| 143 |
+
constexpr explicit operator bool() const noexcept;
|
| 144 |
|
| 145 |
// [unique.ptr.single.modifiers], modifiers
|
| 146 |
+
constexpr pointer release() noexcept;
|
| 147 |
+
constexpr void reset(pointer p = pointer()) noexcept;
|
| 148 |
+
constexpr void swap(unique_ptr& u) noexcept;
|
| 149 |
|
| 150 |
// disable copy from lvalue
|
| 151 |
unique_ptr(const unique_ptr&) = delete;
|
| 152 |
unique_ptr& operator=(const unique_ptr&) = delete;
|
| 153 |
};
|
|
|
|
| 171 |
Otherwise `unique_ptr<T, D>::pointer` shall be a synonym for
|
| 172 |
`element_type*`. The type `unique_ptr<T,
|
| 173 |
D>::pointer` shall meet the *Cpp17NullablePointer* requirements (
|
| 174 |
[[cpp17.nullablepointer]]).
|
| 175 |
|
| 176 |
+
[*Example 1*: Given an allocator type `X`
|
| 177 |
+
[[allocator.requirements.general]] and letting `A` be a synonym for
|
| 178 |
+
`allocator_traits<X>`, the types `A::pointer`, `A::const_pointer`,
|
| 179 |
+
`A::void_pointer`, and `A::const_void_pointer` may be used as
|
| 180 |
`unique_ptr<T, D>::pointer`. — *end example*]
|
| 181 |
|
| 182 |
##### Constructors <a id="unique.ptr.single.ctor">[[unique.ptr.single.ctor]]</a>
|
| 183 |
|
| 184 |
``` cpp
|
| 185 |
constexpr unique_ptr() noexcept;
|
| 186 |
constexpr unique_ptr(nullptr_t) noexcept;
|
| 187 |
```
|
| 188 |
|
| 189 |
+
*Constraints:* `is_pointer_v<deleter_type>` is `false` and
|
| 190 |
+
`is_default_constructible_v<deleter_type>` is `true`.
|
| 191 |
+
|
| 192 |
*Preconditions:* `D` meets the *Cpp17DefaultConstructible* requirements
|
| 193 |
([[cpp17.defaultconstructible]]), and that construction does not throw
|
| 194 |
an exception.
|
| 195 |
|
|
|
|
|
|
|
|
|
|
| 196 |
*Effects:* Constructs a `unique_ptr` object that owns nothing,
|
| 197 |
value-initializing the stored pointer and the stored deleter.
|
| 198 |
|
| 199 |
*Ensures:* `get() == nullptr`. `get_deleter()` returns a reference to
|
| 200 |
the stored deleter.
|
| 201 |
|
| 202 |
``` cpp
|
| 203 |
+
constexpr explicit unique_ptr(type_identity_t<pointer> p) noexcept;
|
| 204 |
```
|
| 205 |
|
| 206 |
*Constraints:* `is_pointer_v<deleter_type>` is `false` and
|
| 207 |
`is_default_constructible_v<deleter_type>` is `true`.
|
| 208 |
|
|
|
|
|
|
|
|
|
|
| 209 |
*Preconditions:* `D` meets the *Cpp17DefaultConstructible* requirements
|
| 210 |
([[cpp17.defaultconstructible]]), and that construction does not throw
|
| 211 |
an exception.
|
| 212 |
|
| 213 |
*Effects:* Constructs a `unique_ptr` which owns `p`, initializing the
|
|
|
|
| 215 |
|
| 216 |
*Ensures:* `get() == p`. `get_deleter()` returns a reference to the
|
| 217 |
stored deleter.
|
| 218 |
|
| 219 |
``` cpp
|
| 220 |
+
constexpr unique_ptr(type_identity_t<pointer> p, const D& d) noexcept;
|
| 221 |
+
constexpr unique_ptr(type_identity_t<pointer> p, remove_reference_t<D>&& d) noexcept;
|
| 222 |
```
|
| 223 |
|
| 224 |
*Constraints:* `is_constructible_v<D, decltype(d)>` is `true`.
|
| 225 |
|
|
|
|
|
|
|
|
|
|
| 226 |
*Preconditions:* For the first constructor, if `D` is not a reference
|
| 227 |
type, `D` meets the *Cpp17CopyConstructible* requirements and such
|
| 228 |
construction does not exit via an exception. For the second constructor,
|
| 229 |
if `D` is not a reference type, `D` meets the *Cpp17MoveConstructible*
|
| 230 |
requirements and such construction does not exit via an exception.
|
|
|
|
| 252 |
```
|
| 253 |
|
| 254 |
— *end example*]
|
| 255 |
|
| 256 |
``` cpp
|
| 257 |
+
constexpr unique_ptr(unique_ptr&& u) noexcept;
|
| 258 |
```
|
| 259 |
|
| 260 |
*Constraints:* `is_move_constructible_v<D>` is `true`.
|
| 261 |
|
| 262 |
*Preconditions:* If `D` is not a reference type, `D` meets the
|
|
|
|
| 276 |
to the stored deleter that was constructed from `u.get_deleter()`. If
|
| 277 |
`D` is a reference type then `get_deleter()` and `u.get_deleter()` both
|
| 278 |
reference the same lvalue deleter.
|
| 279 |
|
| 280 |
``` cpp
|
| 281 |
+
template<class U, class E> constexpr unique_ptr(unique_ptr<U, E>&& u) noexcept;
|
| 282 |
```
|
| 283 |
|
| 284 |
*Constraints:*
|
| 285 |
|
| 286 |
- `unique_ptr<U, E>::pointer` is implicitly convertible to `pointer`,
|
|
|
|
| 306 |
to the stored deleter that was constructed from `u.get_deleter()`.
|
| 307 |
|
| 308 |
##### Destructor <a id="unique.ptr.single.dtor">[[unique.ptr.single.dtor]]</a>
|
| 309 |
|
| 310 |
``` cpp
|
| 311 |
+
constexpr ~unique_ptr();
|
| 312 |
```
|
| 313 |
|
| 314 |
+
*Effects:* Equivalent to: `if (get()) get_deleter()(get());`
|
|
|
|
| 315 |
|
| 316 |
[*Note 3*: The use of `default_delete` requires `T` to be a complete
|
| 317 |
type. — *end note*]
|
| 318 |
|
| 319 |
+
*Remarks:* The behavior is undefined if the evaluation of
|
| 320 |
+
`get_deleter()(get())` throws an exception.
|
| 321 |
|
| 322 |
##### Assignment <a id="unique.ptr.single.asgn">[[unique.ptr.single.asgn]]</a>
|
| 323 |
|
| 324 |
``` cpp
|
| 325 |
+
constexpr unique_ptr& operator=(unique_ptr&& u) noexcept;
|
| 326 |
```
|
| 327 |
|
| 328 |
*Constraints:* `is_move_assignable_v<D>` is `true`.
|
| 329 |
|
| 330 |
*Preconditions:* If `D` is not a reference type, `D` meets the
|
|
|
|
| 335 |
deleter from an lvalue of type `D` does not throw an exception.
|
| 336 |
|
| 337 |
*Effects:* Calls `reset(u.release())` followed by
|
| 338 |
`get_deleter() = std::forward<D>(u.get_deleter())`.
|
| 339 |
|
| 340 |
+
*Ensures:* If `this != addressof(u)`, `u.get() == nullptr`, otherwise
|
| 341 |
+
`u.get()` is unchanged.
|
| 342 |
+
|
| 343 |
*Returns:* `*this`.
|
| 344 |
|
|
|
|
|
|
|
| 345 |
``` cpp
|
| 346 |
+
template<class U, class E> constexpr unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
|
| 347 |
```
|
| 348 |
|
| 349 |
*Constraints:*
|
| 350 |
|
| 351 |
- `unique_ptr<U, E>::pointer` is implicitly convertible to `pointer`,
|
|
|
|
| 360 |
exception.
|
| 361 |
|
| 362 |
*Effects:* Calls `reset(u.release())` followed by
|
| 363 |
`get_deleter() = std::forward<E>(u.get_deleter())`.
|
| 364 |
|
|
|
|
|
|
|
| 365 |
*Ensures:* `u.get() == nullptr`.
|
| 366 |
|
| 367 |
+
*Returns:* `*this`.
|
| 368 |
+
|
| 369 |
``` cpp
|
| 370 |
+
constexpr unique_ptr& operator=(nullptr_t) noexcept;
|
| 371 |
```
|
| 372 |
|
| 373 |
*Effects:* As if by `reset()`.
|
| 374 |
|
| 375 |
*Ensures:* `get() == nullptr`.
|
|
|
|
| 377 |
*Returns:* `*this`.
|
| 378 |
|
| 379 |
##### Observers <a id="unique.ptr.single.observers">[[unique.ptr.single.observers]]</a>
|
| 380 |
|
| 381 |
``` cpp
|
| 382 |
+
constexpr add_lvalue_reference_t<T> operator*() const noexcept(noexcept(*declval<pointer>()));
|
| 383 |
```
|
| 384 |
|
| 385 |
*Preconditions:* `get() != nullptr`.
|
| 386 |
|
| 387 |
*Returns:* `*get()`.
|
| 388 |
|
| 389 |
``` cpp
|
| 390 |
+
constexpr pointer operator->() const noexcept;
|
| 391 |
```
|
| 392 |
|
| 393 |
*Preconditions:* `get() != nullptr`.
|
| 394 |
|
| 395 |
*Returns:* `get()`.
|
| 396 |
|
| 397 |
[*Note 4*: The use of this function typically requires that `T` be a
|
| 398 |
complete type. — *end note*]
|
| 399 |
|
| 400 |
``` cpp
|
| 401 |
+
constexpr pointer get() const noexcept;
|
| 402 |
```
|
| 403 |
|
| 404 |
*Returns:* The stored pointer.
|
| 405 |
|
| 406 |
``` cpp
|
| 407 |
+
constexpr deleter_type& get_deleter() noexcept;
|
| 408 |
+
constexpr const deleter_type& get_deleter() const noexcept;
|
| 409 |
```
|
| 410 |
|
| 411 |
*Returns:* A reference to the stored deleter.
|
| 412 |
|
| 413 |
``` cpp
|
| 414 |
+
constexpr explicit operator bool() const noexcept;
|
| 415 |
```
|
| 416 |
|
| 417 |
*Returns:* `get() != nullptr`.
|
| 418 |
|
| 419 |
##### Modifiers <a id="unique.ptr.single.modifiers">[[unique.ptr.single.modifiers]]</a>
|
| 420 |
|
| 421 |
``` cpp
|
| 422 |
+
constexpr pointer release() noexcept;
|
| 423 |
```
|
| 424 |
|
| 425 |
*Ensures:* `get() == nullptr`.
|
| 426 |
|
| 427 |
*Returns:* The value `get()` had at the start of the call to `release`.
|
| 428 |
|
| 429 |
``` cpp
|
| 430 |
+
constexpr void reset(pointer p = pointer()) noexcept;
|
| 431 |
```
|
| 432 |
|
| 433 |
+
*Effects:* Assigns `p` to the stored pointer, and then, with the old
|
| 434 |
+
value of the stored pointer, `old_p`, evaluates
|
| 435 |
+
`if (old_p) get_deleter()(old_p);`
|
|
|
|
|
|
|
|
|
|
| 436 |
|
| 437 |
[*Note 5*: The order of these operations is significant because the
|
| 438 |
+
call to `get_deleter()` might destroy `*this`. — *end note*]
|
| 439 |
|
| 440 |
*Ensures:* `get() == p`.
|
| 441 |
|
| 442 |
[*Note 6*: The postcondition does not hold if the call to
|
| 443 |
`get_deleter()` destroys `*this` since `this->get()` is no longer a
|
| 444 |
valid expression. — *end note*]
|
| 445 |
|
| 446 |
+
*Remarks:* The behavior is undefined if the evaluation of
|
| 447 |
+
`get_deleter()(old_p)` throws an exception.
|
| 448 |
+
|
| 449 |
``` cpp
|
| 450 |
+
constexpr void swap(unique_ptr& u) noexcept;
|
| 451 |
```
|
| 452 |
|
| 453 |
*Preconditions:* `get_deleter()` is swappable [[swappable.requirements]]
|
| 454 |
and does not throw an exception under `swap`.
|
| 455 |
|
| 456 |
*Effects:* Invokes `swap` on the stored pointers and on the stored
|
| 457 |
deleters of `*this` and `u`.
|
| 458 |
|
| 459 |
#### `unique_ptr` for array objects with a runtime length <a id="unique.ptr.runtime">[[unique.ptr.runtime]]</a>
|
| 460 |
|
| 461 |
+
##### General <a id="unique.ptr.runtime.general">[[unique.ptr.runtime.general]]</a>
|
| 462 |
+
|
| 463 |
``` cpp
|
| 464 |
namespace std {
|
| 465 |
template<class T, class D> class unique_ptr<T[], D> {
|
| 466 |
public:
|
| 467 |
using pointer = see below;
|
| 468 |
using element_type = T;
|
| 469 |
using deleter_type = D;
|
| 470 |
|
| 471 |
// [unique.ptr.runtime.ctor], constructors
|
| 472 |
constexpr unique_ptr() noexcept;
|
| 473 |
+
template<class U> constexpr explicit unique_ptr(U p) noexcept;
|
| 474 |
+
template<class U> constexpr unique_ptr(U p, see below d) noexcept;
|
| 475 |
+
template<class U> constexpr unique_ptr(U p, see below d) noexcept;
|
| 476 |
+
constexpr unique_ptr(unique_ptr&& u) noexcept;
|
| 477 |
template<class U, class E>
|
| 478 |
+
constexpr unique_ptr(unique_ptr<U, E>&& u) noexcept;
|
| 479 |
constexpr unique_ptr(nullptr_t) noexcept;
|
| 480 |
|
| 481 |
// destructor
|
| 482 |
+
constexpr ~unique_ptr();
|
| 483 |
|
| 484 |
// assignment
|
| 485 |
+
constexpr unique_ptr& operator=(unique_ptr&& u) noexcept;
|
| 486 |
template<class U, class E>
|
| 487 |
+
constexpr unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
|
| 488 |
+
constexpr unique_ptr& operator=(nullptr_t) noexcept;
|
| 489 |
|
| 490 |
// [unique.ptr.runtime.observers], observers
|
| 491 |
+
constexpr T& operator[](size_t i) const;
|
| 492 |
+
constexpr pointer get() const noexcept;
|
| 493 |
+
constexpr deleter_type& get_deleter() noexcept;
|
| 494 |
+
constexpr const deleter_type& get_deleter() const noexcept;
|
| 495 |
+
constexpr explicit operator bool() const noexcept;
|
| 496 |
|
| 497 |
// [unique.ptr.runtime.modifiers], modifiers
|
| 498 |
+
constexpr pointer release() noexcept;
|
| 499 |
+
template<class U> constexpr void reset(U p) noexcept;
|
| 500 |
+
constexpr void reset(nullptr_t = nullptr) noexcept;
|
| 501 |
+
constexpr void swap(unique_ptr& u) noexcept;
|
| 502 |
|
| 503 |
// disable copy from lvalue
|
| 504 |
unique_ptr(const unique_ptr&) = delete;
|
| 505 |
unique_ptr& operator=(const unique_ptr&) = delete;
|
| 506 |
};
|
|
|
|
| 526 |
The template argument `T` shall be a complete type.
|
| 527 |
|
| 528 |
##### Constructors <a id="unique.ptr.runtime.ctor">[[unique.ptr.runtime.ctor]]</a>
|
| 529 |
|
| 530 |
``` cpp
|
| 531 |
+
template<class U> constexpr explicit unique_ptr(U p) noexcept;
|
| 532 |
```
|
| 533 |
|
| 534 |
This constructor behaves the same as the constructor in the primary
|
| 535 |
template that takes a single parameter of type `pointer`.
|
| 536 |
|
|
|
|
| 539 |
- `U` is the same type as `pointer`, or
|
| 540 |
- `pointer` is the same type as `element_type*`, `U` is a pointer type
|
| 541 |
`V*`, and `V(*)[]` is convertible to `element_type(*)[]`.
|
| 542 |
|
| 543 |
``` cpp
|
| 544 |
+
template<class U> constexpr unique_ptr(U p, see below d) noexcept;
|
| 545 |
+
template<class U> constexpr unique_ptr(U p, see below d) noexcept;
|
| 546 |
```
|
| 547 |
|
| 548 |
These constructors behave the same as the constructors in the primary
|
| 549 |
template that take a parameter of type `pointer` and a second parameter.
|
| 550 |
|
|
|
|
| 554 |
- `U` is `nullptr_t`, or
|
| 555 |
- `pointer` is the same type as `element_type*`, `U` is a pointer type
|
| 556 |
`V*`, and `V(*)[]` is convertible to `element_type(*)[]`.
|
| 557 |
|
| 558 |
``` cpp
|
| 559 |
+
template<class U, class E> constexpr unique_ptr(unique_ptr<U, E>&& u) noexcept;
|
| 560 |
```
|
| 561 |
|
| 562 |
This constructor behaves the same as in the primary template.
|
| 563 |
|
| 564 |
*Constraints:* Where `UP` is `unique_ptr<U, E>`:
|
|
|
|
| 574 |
primary template. — *end note*]
|
| 575 |
|
| 576 |
##### Assignment <a id="unique.ptr.runtime.asgn">[[unique.ptr.runtime.asgn]]</a>
|
| 577 |
|
| 578 |
``` cpp
|
| 579 |
+
template<class U, class E> constexpr unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
|
| 580 |
```
|
| 581 |
|
| 582 |
This operator behaves the same as in the primary template.
|
| 583 |
|
| 584 |
*Constraints:* Where `UP` is `unique_ptr<U, E>`:
|
|
|
|
| 593 |
primary template. — *end note*]
|
| 594 |
|
| 595 |
##### Observers <a id="unique.ptr.runtime.observers">[[unique.ptr.runtime.observers]]</a>
|
| 596 |
|
| 597 |
``` cpp
|
| 598 |
+
constexpr T& operator[](size_t i) const;
|
| 599 |
```
|
| 600 |
|
| 601 |
*Preconditions:* `i` < the number of elements in the array to which the
|
| 602 |
stored pointer points.
|
| 603 |
|
| 604 |
*Returns:* `get()[i]`.
|
| 605 |
|
| 606 |
##### Modifiers <a id="unique.ptr.runtime.modifiers">[[unique.ptr.runtime.modifiers]]</a>
|
| 607 |
|
| 608 |
``` cpp
|
| 609 |
+
constexpr void reset(nullptr_t p = nullptr) noexcept;
|
| 610 |
```
|
| 611 |
|
| 612 |
*Effects:* Equivalent to `reset(pointer())`.
|
| 613 |
|
| 614 |
``` cpp
|
| 615 |
+
constexpr template<class U> void reset(U p) noexcept;
|
| 616 |
```
|
| 617 |
|
| 618 |
This function behaves the same as the `reset` member of the primary
|
| 619 |
template.
|
| 620 |
|
|
|
|
| 625 |
`V*`, and `V(*)[]` is convertible to `element_type(*)[]`.
|
| 626 |
|
| 627 |
#### Creation <a id="unique.ptr.create">[[unique.ptr.create]]</a>
|
| 628 |
|
| 629 |
``` cpp
|
| 630 |
+
template<class T, class... Args> constexpr unique_ptr<T> make_unique(Args&&... args);
|
| 631 |
```
|
| 632 |
|
| 633 |
*Constraints:* `T` is not an array type.
|
| 634 |
|
| 635 |
*Returns:* `unique_ptr<T>(new T(std::forward<Args>(args)...))`.
|
| 636 |
|
| 637 |
``` cpp
|
| 638 |
+
template<class T> constexpr unique_ptr<T> make_unique(size_t n);
|
| 639 |
```
|
| 640 |
|
| 641 |
*Constraints:* `T` is an array of unknown bound.
|
| 642 |
|
| 643 |
*Returns:* `unique_ptr<T>(new remove_extent_t<T>[n]())`.
|
|
|
|
| 647 |
```
|
| 648 |
|
| 649 |
*Constraints:* `T` is an array of known bound.
|
| 650 |
|
| 651 |
``` cpp
|
| 652 |
+
template<class T> constexpr unique_ptr<T> make_unique_for_overwrite();
|
| 653 |
```
|
| 654 |
|
| 655 |
*Constraints:* `T` is not an array type.
|
| 656 |
|
| 657 |
*Returns:* `unique_ptr<T>(new T)`.
|
| 658 |
|
| 659 |
``` cpp
|
| 660 |
+
template<class T> constexpr unique_ptr<T> make_unique_for_overwrite(size_t n);
|
| 661 |
```
|
| 662 |
|
| 663 |
*Constraints:* `T` is an array of unknown bound.
|
| 664 |
|
| 665 |
*Returns:* `unique_ptr<T>(new remove_extent_t<T>[n])`.
|
|
|
|
| 671 |
*Constraints:* `T` is an array of known bound.
|
| 672 |
|
| 673 |
#### Specialized algorithms <a id="unique.ptr.special">[[unique.ptr.special]]</a>
|
| 674 |
|
| 675 |
``` cpp
|
| 676 |
+
template<class T, class D> constexpr void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
|
| 677 |
```
|
| 678 |
|
| 679 |
*Constraints:* `is_swappable_v<D>` is `true`.
|
| 680 |
|
| 681 |
*Effects:* Calls `x.swap(y)`.
|
| 682 |
|
| 683 |
``` cpp
|
| 684 |
template<class T1, class D1, class T2, class D2>
|
| 685 |
+
constexpr bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
|
| 686 |
```
|
| 687 |
|
| 688 |
*Returns:* `x.get() == y.get()`.
|
| 689 |
|
| 690 |
``` cpp
|
|
|
|
| 742 |
|
| 743 |
*Returns:* `compare_three_way()(x.get(), y.get())`.
|
| 744 |
|
| 745 |
``` cpp
|
| 746 |
template<class T, class D>
|
| 747 |
+
constexpr bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
|
| 748 |
```
|
| 749 |
|
| 750 |
*Returns:* `!x`.
|
| 751 |
|
| 752 |
``` cpp
|
| 753 |
template<class T, class D>
|
| 754 |
+
constexpr bool operator<(const unique_ptr<T, D>& x, nullptr_t);
|
| 755 |
template<class T, class D>
|
| 756 |
+
constexpr bool operator<(nullptr_t, const unique_ptr<T, D>& x);
|
| 757 |
```
|
| 758 |
|
| 759 |
*Preconditions:* The specialization `less<unique_ptr<T, D>::pointer>` is
|
| 760 |
a function object type [[function.objects]] that induces a strict weak
|
| 761 |
ordering [[alg.sorting]] on the pointer values.
|
|
|
|
| 772 |
less<unique_ptr<T, D>::pointer>()(nullptr, x.get())
|
| 773 |
```
|
| 774 |
|
| 775 |
``` cpp
|
| 776 |
template<class T, class D>
|
| 777 |
+
constexpr bool operator>(const unique_ptr<T, D>& x, nullptr_t);
|
| 778 |
template<class T, class D>
|
| 779 |
+
constexpr bool operator>(nullptr_t, const unique_ptr<T, D>& x);
|
| 780 |
```
|
| 781 |
|
| 782 |
*Returns:* The first function template returns `nullptr < x`. The second
|
| 783 |
function template returns `x < nullptr`.
|
| 784 |
|
| 785 |
``` cpp
|
| 786 |
template<class T, class D>
|
| 787 |
+
constexpr bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
|
| 788 |
template<class T, class D>
|
| 789 |
+
constexpr bool operator<=(nullptr_t, const unique_ptr<T, D>& x);
|
| 790 |
```
|
| 791 |
|
| 792 |
*Returns:* The first function template returns `!(nullptr < x)`. The
|
| 793 |
second function template returns `!(x < nullptr)`.
|
| 794 |
|
| 795 |
``` cpp
|
| 796 |
template<class T, class D>
|
| 797 |
+
constexpr bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
|
| 798 |
template<class T, class D>
|
| 799 |
+
constexpr bool operator>=(nullptr_t, const unique_ptr<T, D>& x);
|
| 800 |
```
|
| 801 |
|
| 802 |
*Returns:* The first function template returns `!(x < nullptr)`. The
|
| 803 |
second function template returns `!(nullptr < x)`.
|
| 804 |
|
| 805 |
``` cpp
|
| 806 |
template<class T, class D>
|
| 807 |
+
requires three_way_comparable<typename unique_ptr<T, D>::pointer>
|
| 808 |
+
constexpr compare_three_way_result_t<typename unique_ptr<T, D>::pointer>
|
| 809 |
operator<=>(const unique_ptr<T, D>& x, nullptr_t);
|
| 810 |
```
|
| 811 |
|
| 812 |
+
*Returns:*
|
| 813 |
+
|
| 814 |
+
``` cpp
|
| 815 |
+
compare_three_way()(x.get(), static_cast<typename unique_ptr<T, D>::pointer>(nullptr)).
|
| 816 |
+
```
|
| 817 |
|
| 818 |
#### I/O <a id="unique.ptr.io">[[unique.ptr.io]]</a>
|
| 819 |
|
| 820 |
``` cpp
|
| 821 |
template<class E, class T, class Y, class D>
|
|
|
|
| 826 |
|
| 827 |
*Effects:* Equivalent to: `os << p.get();`
|
| 828 |
|
| 829 |
*Returns:* `os`.
|
| 830 |
|
| 831 |
+
### Shared-ownership pointers <a id="util.sharedptr">[[util.sharedptr]]</a>
|
| 832 |
+
|
| 833 |
+
#### Class `bad_weak_ptr` <a id="util.smartptr.weak.bad">[[util.smartptr.weak.bad]]</a>
|
| 834 |
|
| 835 |
``` cpp
|
| 836 |
namespace std {
|
| 837 |
class bad_weak_ptr : public exception {
|
| 838 |
public:
|
|
|
|
| 849 |
const char* what() const noexcept override;
|
| 850 |
```
|
| 851 |
|
| 852 |
*Returns:* An *implementation-defined* NTBS.
|
| 853 |
|
| 854 |
+
#### Class template `shared_ptr` <a id="util.smartptr.shared">[[util.smartptr.shared]]</a>
|
| 855 |
+
|
| 856 |
+
##### General <a id="util.smartptr.shared.general">[[util.smartptr.shared.general]]</a>
|
| 857 |
|
| 858 |
The `shared_ptr` class template stores a pointer, usually obtained via
|
| 859 |
`new`. `shared_ptr` implements semantics of shared ownership; the last
|
| 860 |
remaining owner of the pointer is responsible for destroying the object,
|
| 861 |
or otherwise releasing the resources associated with the stored pointer.
|
|
|
|
| 945 |
contextually convertible to `bool`, allowing their use in boolean
|
| 946 |
expressions and declarations in conditions.
|
| 947 |
|
| 948 |
The template parameter `T` of `shared_ptr` may be an incomplete type.
|
| 949 |
|
| 950 |
+
[*Note 1*: `T` can be a function type. — *end note*]
|
| 951 |
|
| 952 |
[*Example 1*:
|
| 953 |
|
| 954 |
``` cpp
|
| 955 |
if (shared_ptr<X> px = dynamic_pointer_cast<X>(py)) {
|
|
|
|
| 967 |
|
| 968 |
For the purposes of subclause [[smartptr]], a pointer type `Y*` is said
|
| 969 |
to be *compatible with* a pointer type `T*` when either `Y*` is
|
| 970 |
convertible to `T*` or `Y` is `U[N]` and `T` is cv `U[]`.
|
| 971 |
|
| 972 |
+
##### Constructors <a id="util.smartptr.shared.const">[[util.smartptr.shared.const]]</a>
|
| 973 |
|
| 974 |
In the constructor definitions below, enables `shared_from_this` with
|
| 975 |
`p`, for a pointer `p` of type `Y*`, means that if `Y` has an
|
| 976 |
unambiguous and accessible base class that is a specialization of
|
| 977 |
`enable_shared_from_this` [[util.smartptr.enab]], then `remove_cv_t<Y>*`
|
|
|
|
| 995 |
|
| 996 |
``` cpp
|
| 997 |
template<class Y> explicit shared_ptr(Y* p);
|
| 998 |
```
|
| 999 |
|
|
|
|
|
|
|
| 1000 |
*Constraints:* When `T` is an array type, the expression `delete[] p` is
|
| 1001 |
well-formed and either `T` is `U[N]` and `Y(*)[N]` is convertible to
|
| 1002 |
`T*`, or `T` is `U[]` and `Y(*)[]` is convertible to `T*`. When `T` is
|
| 1003 |
not an array type, the expression `delete p` is well-formed and `Y*` is
|
| 1004 |
convertible to `T*`.
|
| 1005 |
|
| 1006 |
+
*Mandates:* `Y` is a complete type.
|
| 1007 |
+
|
| 1008 |
*Preconditions:* The expression `delete[] p`, when `T` is an array type,
|
| 1009 |
or `delete p`, when `T` is not an array type, has well-defined behavior,
|
| 1010 |
and does not throw exceptions.
|
| 1011 |
|
| 1012 |
*Effects:* When `T` is not an array type, constructs a `shared_ptr`
|
|
|
|
| 1017 |
not an array type, `delete[] p` otherwise.
|
| 1018 |
|
| 1019 |
*Ensures:* `use_count() == 1 && get() == p`.
|
| 1020 |
|
| 1021 |
*Throws:* `bad_alloc`, or an *implementation-defined* exception when a
|
| 1022 |
+
resource other than memory cannot be obtained.
|
| 1023 |
|
| 1024 |
``` cpp
|
| 1025 |
template<class Y, class D> shared_ptr(Y* p, D d);
|
| 1026 |
template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
|
| 1027 |
template<class D> shared_ptr(nullptr_t p, D d);
|
|
|
|
| 1037 |
- If `T` is not an array type, then `Y*` is convertible to `T*`.
|
| 1038 |
|
| 1039 |
*Preconditions:* Construction of `d` and a deleter of type `D`
|
| 1040 |
initialized with `std::move(d)` do not throw exceptions. The expression
|
| 1041 |
`d(p)` has well-defined behavior and does not throw exceptions. `A`
|
| 1042 |
+
meets the *Cpp17Allocator*
|
| 1043 |
+
requirements [[allocator.requirements.general]].
|
| 1044 |
|
| 1045 |
*Effects:* Constructs a `shared_ptr` object that owns the object `p` and
|
| 1046 |
the deleter `d`. When `T` is not an array type, the first and second
|
| 1047 |
constructors enable `shared_from_this` with `p`. The second and fourth
|
| 1048 |
constructors shall use a copy of `a` to allocate memory for internal
|
| 1049 |
use. If an exception is thrown, `d(p)` is called.
|
| 1050 |
|
| 1051 |
*Ensures:* `use_count() == 1 && get() == p`.
|
| 1052 |
|
| 1053 |
*Throws:* `bad_alloc`, or an *implementation-defined* exception when a
|
| 1054 |
+
resource other than memory cannot be obtained.
|
| 1055 |
|
| 1056 |
``` cpp
|
| 1057 |
template<class Y> shared_ptr(const shared_ptr<Y>& r, element_type* p) noexcept;
|
| 1058 |
template<class Y> shared_ptr(shared_ptr<Y>&& r, element_type* p) noexcept;
|
| 1059 |
```
|
|
|
|
| 1062 |
ownership with the initial value of `r`.
|
| 1063 |
|
| 1064 |
*Ensures:* `get() == p`. For the second overload, `r` is empty and
|
| 1065 |
`r.get() == nullptr`.
|
| 1066 |
|
| 1067 |
+
[*Note 1*: Use of this constructor leads to a dangling pointer unless
|
| 1068 |
+
`p` remains valid at least until the ownership group of `r` is
|
| 1069 |
+
destroyed. — *end note*]
|
| 1070 |
|
| 1071 |
[*Note 2*: This constructor allows creation of an empty `shared_ptr`
|
| 1072 |
instance with a non-null stored pointer. — *end note*]
|
| 1073 |
|
| 1074 |
``` cpp
|
|
|
|
| 1091 |
|
| 1092 |
*Constraints:* For the second constructor, `Y*` is compatible with `T*`.
|
| 1093 |
|
| 1094 |
*Effects:* Move constructs a `shared_ptr` instance from `r`.
|
| 1095 |
|
| 1096 |
+
*Ensures:* `*this` contains the old value of `r`. `r` is empty, and
|
| 1097 |
+
`r.get() == nullptr`.
|
| 1098 |
|
| 1099 |
``` cpp
|
| 1100 |
template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
|
| 1101 |
```
|
| 1102 |
|
|
|
|
| 1117 |
*Constraints:* `Y*` is compatible with `T*` and
|
| 1118 |
`unique_ptr<Y, D>::pointer` is convertible to `element_type*`.
|
| 1119 |
|
| 1120 |
*Effects:* If `r.get() == nullptr`, equivalent to `shared_ptr()`.
|
| 1121 |
Otherwise, if `D` is not a reference type, equivalent to
|
| 1122 |
+
`shared_ptr(r.release(), std::move(r.get_deleter()))`. Otherwise,
|
| 1123 |
+
equivalent to `shared_ptr(r.release(), ref(r.get_deleter()))`. If an
|
| 1124 |
+
exception is thrown, the constructor has no effect.
|
| 1125 |
|
| 1126 |
+
##### Destructor <a id="util.smartptr.shared.dest">[[util.smartptr.shared.dest]]</a>
|
| 1127 |
|
| 1128 |
``` cpp
|
| 1129 |
~shared_ptr();
|
| 1130 |
```
|
| 1131 |
|
|
|
|
| 1135 |
instance (`use_count() > 1`), there are no side effects.
|
| 1136 |
- Otherwise, if `*this` owns an object `p` and a deleter `d`, `d(p)` is
|
| 1137 |
called.
|
| 1138 |
- Otherwise, `*this` owns a pointer `p`, and `delete p` is called.
|
| 1139 |
|
| 1140 |
+
[*Note 2*: Since the destruction of `*this` decreases the number of
|
| 1141 |
instances that share ownership with `*this` by one, after `*this` has
|
| 1142 |
been destroyed all `shared_ptr` instances that shared ownership with
|
| 1143 |
`*this` will report a `use_count()` that is one less than its previous
|
| 1144 |
value. — *end note*]
|
| 1145 |
|
| 1146 |
+
##### Assignment <a id="util.smartptr.shared.assign">[[util.smartptr.shared.assign]]</a>
|
| 1147 |
|
| 1148 |
``` cpp
|
| 1149 |
shared_ptr& operator=(const shared_ptr& r) noexcept;
|
| 1150 |
template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
|
| 1151 |
```
|
| 1152 |
|
| 1153 |
*Effects:* Equivalent to `shared_ptr(r).swap(*this)`.
|
| 1154 |
|
| 1155 |
*Returns:* `*this`.
|
| 1156 |
|
| 1157 |
+
[*Note 3*:
|
| 1158 |
|
| 1159 |
The use count updates caused by the temporary object construction and
|
| 1160 |
+
destruction are not observable side effects, so the implementation can
|
| 1161 |
meet the effects (and the implied guarantees) via different means,
|
| 1162 |
without creating a temporary. In particular, in the example:
|
| 1163 |
|
| 1164 |
``` cpp
|
| 1165 |
shared_ptr<int> p(new int);
|
| 1166 |
shared_ptr<void> q(p);
|
| 1167 |
p = p;
|
| 1168 |
q = p;
|
| 1169 |
```
|
| 1170 |
|
| 1171 |
+
both assignments can be no-ops.
|
| 1172 |
|
| 1173 |
— *end note*]
|
| 1174 |
|
| 1175 |
``` cpp
|
| 1176 |
shared_ptr& operator=(shared_ptr&& r) noexcept;
|
|
|
|
| 1187 |
|
| 1188 |
*Effects:* Equivalent to `shared_ptr(std::move(r)).swap(*this)`.
|
| 1189 |
|
| 1190 |
*Returns:* `*this`.
|
| 1191 |
|
| 1192 |
+
##### Modifiers <a id="util.smartptr.shared.mod">[[util.smartptr.shared.mod]]</a>
|
| 1193 |
|
| 1194 |
``` cpp
|
| 1195 |
void swap(shared_ptr& r) noexcept;
|
| 1196 |
```
|
| 1197 |
|
|
|
|
| 1219 |
template<class Y, class D, class A> void reset(Y* p, D d, A a);
|
| 1220 |
```
|
| 1221 |
|
| 1222 |
*Effects:* Equivalent to `shared_ptr(p, d, a).swap(*this)`.
|
| 1223 |
|
| 1224 |
+
##### Observers <a id="util.smartptr.shared.obs">[[util.smartptr.shared.obs]]</a>
|
| 1225 |
|
| 1226 |
``` cpp
|
| 1227 |
element_type* get() const noexcept;
|
| 1228 |
```
|
| 1229 |
|
|
|
|
| 1231 |
|
| 1232 |
``` cpp
|
| 1233 |
T& operator*() const noexcept;
|
| 1234 |
```
|
| 1235 |
|
| 1236 |
+
*Preconditions:* `get() != nullptr`.
|
| 1237 |
|
| 1238 |
*Returns:* `*get()`.
|
| 1239 |
|
| 1240 |
*Remarks:* When `T` is an array type or cv `void`, it is unspecified
|
| 1241 |
whether this member function is declared. If it is declared, it is
|
|
|
|
| 1245 |
|
| 1246 |
``` cpp
|
| 1247 |
T* operator->() const noexcept;
|
| 1248 |
```
|
| 1249 |
|
| 1250 |
+
*Preconditions:* `get() != nullptr`.
|
| 1251 |
|
| 1252 |
*Returns:* `get()`.
|
| 1253 |
|
| 1254 |
*Remarks:* When `T` is an array type, it is unspecified whether this
|
| 1255 |
member function is declared. If it is declared, it is unspecified what
|
|
|
|
| 1258 |
|
| 1259 |
``` cpp
|
| 1260 |
element_type& operator[](ptrdiff_t i) const;
|
| 1261 |
```
|
| 1262 |
|
| 1263 |
+
*Preconditions:* `get() != nullptr && i >= 0`. If `T` is `U[N]`,
|
| 1264 |
+
`i < N`.
|
| 1265 |
|
| 1266 |
*Returns:* `get()[i]`.
|
| 1267 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1268 |
*Throws:* Nothing.
|
| 1269 |
|
| 1270 |
+
*Remarks:* When `T` is not an array type, it is unspecified whether this
|
| 1271 |
+
member function is declared. If it is declared, it is unspecified what
|
| 1272 |
+
its return type is, except that the declaration (although not
|
| 1273 |
+
necessarily the definition) of the function shall be well-formed.
|
| 1274 |
+
|
| 1275 |
``` cpp
|
| 1276 |
long use_count() const noexcept;
|
| 1277 |
```
|
| 1278 |
|
| 1279 |
+
*Synchronization:* None.
|
| 1280 |
+
|
| 1281 |
*Returns:* The number of `shared_ptr` objects, `*this` included, that
|
| 1282 |
share ownership with `*this`, or `0` when `*this` is empty.
|
| 1283 |
|
| 1284 |
+
[*Note 4*: `get() == nullptr` does not imply a specific return value of
|
|
|
|
|
|
|
| 1285 |
`use_count()`. — *end note*]
|
| 1286 |
|
| 1287 |
+
[*Note 5*: `weak_ptr<T>::lock()` can affect the return value of
|
| 1288 |
`use_count()`. — *end note*]
|
| 1289 |
|
| 1290 |
+
[*Note 6*: When multiple threads might affect the return value of
|
| 1291 |
+
`use_count()`, the result is approximate. In particular,
|
| 1292 |
+
`use_count() == 1` does not imply that accesses through a previously
|
| 1293 |
+
destroyed `shared_ptr` have in any sense completed. — *end note*]
|
|
|
|
| 1294 |
|
| 1295 |
``` cpp
|
| 1296 |
explicit operator bool() const noexcept;
|
| 1297 |
```
|
| 1298 |
|
| 1299 |
+
*Returns:* `get() != nullptr`.
|
| 1300 |
|
| 1301 |
``` cpp
|
| 1302 |
template<class U> bool owner_before(const shared_ptr<U>& b) const noexcept;
|
| 1303 |
template<class U> bool owner_before(const weak_ptr<U>& b) const noexcept;
|
| 1304 |
```
|
|
|
|
| 1310 |
- under the equivalence relation defined by `owner_before`,
|
| 1311 |
`!a.owner_before(b) && !b.owner_before(a)`, two `shared_ptr` or
|
| 1312 |
`weak_ptr` instances are equivalent if and only if they share
|
| 1313 |
ownership or are both empty.
|
| 1314 |
|
| 1315 |
+
##### Creation <a id="util.smartptr.shared.create">[[util.smartptr.shared.create]]</a>
|
| 1316 |
|
| 1317 |
The common requirements that apply to all `make_shared`,
|
| 1318 |
`allocate_shared`, `make_shared_for_overwrite`, and
|
| 1319 |
`allocate_shared_for_overwrite` overloads, unless specified otherwise,
|
| 1320 |
are described below.
|
|
|
|
| 1328 |
shared_ptr<T> make_shared_for_overwrite(args);
|
| 1329 |
template<class T, class A, ...>
|
| 1330 |
shared_ptr<T> allocate_shared_for_overwrite(const A& a, args);
|
| 1331 |
```
|
| 1332 |
|
| 1333 |
+
*Preconditions:* `A` meets the *Cpp17Allocator*
|
| 1334 |
+
requirements [[allocator.requirements.general]].
|
| 1335 |
|
| 1336 |
*Effects:* Allocates memory for an object of type `T` (or `U[N]` when
|
| 1337 |
`T` is `U[]`, where `N` is determined from *args* as specified by the
|
| 1338 |
concrete overload). The object is initialized from *args* as specified
|
| 1339 |
by the concrete overload. The `allocate_shared` and
|
| 1340 |
`allocate_shared_for_overwrite` templates use a copy of `a` (rebound for
|
| 1341 |
an unspecified `value_type`) to allocate memory. If an exception is
|
| 1342 |
thrown, the functions have no effect.
|
| 1343 |
|
| 1344 |
+
*Ensures:* `r.get() != nullptr && r.use_count() == 1`, where `r` is the
|
| 1345 |
+
return value.
|
| 1346 |
+
|
| 1347 |
*Returns:* A `shared_ptr` instance that stores and owns the address of
|
| 1348 |
the newly constructed object.
|
| 1349 |
|
|
|
|
|
|
|
|
|
|
| 1350 |
*Throws:* `bad_alloc`, or an exception thrown from `allocate` or from
|
| 1351 |
the initialization of the object.
|
| 1352 |
|
| 1353 |
*Remarks:*
|
| 1354 |
|
| 1355 |
- Implementations should perform no more than one memory allocation.
|
| 1356 |
+
\[*Note 3*: This provides efficiency equivalent to an intrusive smart
|
| 1357 |
pointer. — *end note*]
|
| 1358 |
- When an object of an array type `U` is specified to have an initial
|
| 1359 |
value of `u` (of the same type), this shall be interpreted to mean
|
| 1360 |
that each array element of the object has as its initial value the
|
| 1361 |
corresponding element from `u`.
|
|
|
|
| 1407 |
expression `allocator_traits<A2>::destroy(a2, pv)` where `pv` points
|
| 1408 |
to that object of type `remove_cv_t<U>` and `a2` of type `A2` is a
|
| 1409 |
rebound copy of the allocator `a` passed to `allocate_shared` such
|
| 1410 |
that its `value_type` is `remove_cv_t<U>`.
|
| 1411 |
|
| 1412 |
+
[*Note 7*: These functions will typically allocate more memory than
|
| 1413 |
`sizeof(T)` to allow for internal bookkeeping structures such as
|
| 1414 |
reference counts. — *end note*]
|
| 1415 |
|
| 1416 |
``` cpp
|
| 1417 |
template<class T, class... Args>
|
|
|
|
| 1421 |
```
|
| 1422 |
|
| 1423 |
*Constraints:* `T` is not an array type.
|
| 1424 |
|
| 1425 |
*Returns:* A `shared_ptr` to an object of type `T` with an initial value
|
| 1426 |
+
`T(std::forward<Args>(args)...)`.
|
| 1427 |
|
| 1428 |
*Remarks:* The `shared_ptr` constructors called by these functions
|
| 1429 |
enable `shared_from_this` with the address of the newly constructed
|
| 1430 |
object of type `T`.
|
| 1431 |
|
|
|
|
| 1581 |
// shared_ptr to a default-initialized double[1024], where each element has an indeterminate value
|
| 1582 |
```
|
| 1583 |
|
| 1584 |
— *end example*]
|
| 1585 |
|
| 1586 |
+
##### Comparison <a id="util.smartptr.shared.cmp">[[util.smartptr.shared.cmp]]</a>
|
| 1587 |
|
| 1588 |
``` cpp
|
| 1589 |
template<class T, class U>
|
| 1590 |
bool operator==(const shared_ptr<T>& a, const shared_ptr<U>& b) noexcept;
|
| 1591 |
```
|
|
|
|
| 1604 |
strong_ordering operator<=>(const shared_ptr<T>& a, const shared_ptr<U>& b) noexcept;
|
| 1605 |
```
|
| 1606 |
|
| 1607 |
*Returns:* `compare_three_way()(a.get(), b.get())`.
|
| 1608 |
|
| 1609 |
+
[*Note 8*: Defining a comparison operator function allows `shared_ptr`
|
| 1610 |
+
objects to be used as keys in associative containers. — *end note*]
|
| 1611 |
|
| 1612 |
``` cpp
|
| 1613 |
template<class T>
|
| 1614 |
strong_ordering operator<=>(const shared_ptr<T>& a, nullptr_t) noexcept;
|
| 1615 |
```
|
| 1616 |
|
| 1617 |
+
*Returns:*
|
| 1618 |
|
| 1619 |
+
``` cpp
|
| 1620 |
+
compare_three_way()(a.get(), static_cast<typename shared_ptr<T>::element_type*>(nullptr).
|
| 1621 |
+
```
|
| 1622 |
+
|
| 1623 |
+
##### Specialized algorithms <a id="util.smartptr.shared.spec">[[util.smartptr.shared.spec]]</a>
|
| 1624 |
|
| 1625 |
``` cpp
|
| 1626 |
template<class T>
|
| 1627 |
void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
|
| 1628 |
```
|
| 1629 |
|
| 1630 |
*Effects:* Equivalent to `a.swap(b)`.
|
| 1631 |
|
| 1632 |
+
##### Casts <a id="util.smartptr.shared.cast">[[util.smartptr.shared.cast]]</a>
|
| 1633 |
|
| 1634 |
``` cpp
|
| 1635 |
template<class T, class U>
|
| 1636 |
shared_ptr<T> static_pointer_cast(const shared_ptr<U>& r) noexcept;
|
| 1637 |
template<class T, class U>
|
|
|
|
| 1648 |
```
|
| 1649 |
|
| 1650 |
where *`R`* is `r` for the first overload, and `std::move(r)` for the
|
| 1651 |
second.
|
| 1652 |
|
| 1653 |
+
[*Note 9*: The seemingly equivalent expression
|
| 1654 |
`shared_ptr<T>(static_cast<T*>(r.get()))` will eventually result in
|
| 1655 |
undefined behavior, attempting to delete the same object
|
| 1656 |
twice. — *end note*]
|
| 1657 |
|
| 1658 |
``` cpp
|
|
|
|
| 1662 |
shared_ptr<T> dynamic_pointer_cast(shared_ptr<U>&& r) noexcept;
|
| 1663 |
```
|
| 1664 |
|
| 1665 |
*Mandates:* The expression `dynamic_cast<T*>((U*)nullptr)` is
|
| 1666 |
well-formed. The expression
|
| 1667 |
+
`dynamic_cast<typename shared_ptr<T>::element_type*>(r.get())` is
|
| 1668 |
+
well-formed.
|
| 1669 |
|
| 1670 |
*Preconditions:* The expression
|
| 1671 |
`dynamic_cast<typename shared_ptr<T>::element_type*>(r.get())` has
|
| 1672 |
well-defined behavior.
|
| 1673 |
|
|
|
|
| 1676 |
- When `dynamic_cast<typename shared_ptr<T>::element_type*>(r.get())`
|
| 1677 |
returns a non-null value `p`, `shared_ptr<T>(`*`R`*`, p)`, where *`R`*
|
| 1678 |
is `r` for the first overload, and `std::move(r)` for the second.
|
| 1679 |
- Otherwise, `shared_ptr<T>()`.
|
| 1680 |
|
| 1681 |
+
[*Note 10*: The seemingly equivalent expression
|
| 1682 |
`shared_ptr<T>(dynamic_cast<T*>(r.get()))` will eventually result in
|
| 1683 |
undefined behavior, attempting to delete the same object
|
| 1684 |
twice. — *end note*]
|
| 1685 |
|
| 1686 |
``` cpp
|
|
|
|
| 1699 |
```
|
| 1700 |
|
| 1701 |
where *`R`* is `r` for the first overload, and `std::move(r)` for the
|
| 1702 |
second.
|
| 1703 |
|
| 1704 |
+
[*Note 11*: The seemingly equivalent expression
|
| 1705 |
`shared_ptr<T>(const_cast<T*>(r.get()))` will eventually result in
|
| 1706 |
undefined behavior, attempting to delete the same object
|
| 1707 |
twice. — *end note*]
|
| 1708 |
|
| 1709 |
``` cpp
|
|
|
|
| 1723 |
```
|
| 1724 |
|
| 1725 |
where *`R`* is `r` for the first overload, and `std::move(r)` for the
|
| 1726 |
second.
|
| 1727 |
|
| 1728 |
+
[*Note 12*: The seemingly equivalent expression
|
| 1729 |
`shared_ptr<T>(reinterpret_cast<T*>(r.get()))` will eventually result in
|
| 1730 |
undefined behavior, attempting to delete the same object
|
| 1731 |
twice. — *end note*]
|
| 1732 |
|
| 1733 |
+
##### `get_deleter` <a id="util.smartptr.getdeleter">[[util.smartptr.getdeleter]]</a>
|
| 1734 |
|
| 1735 |
``` cpp
|
| 1736 |
template<class D, class T>
|
| 1737 |
D* get_deleter(const shared_ptr<T>& p) noexcept;
|
| 1738 |
```
|
|
|
|
| 1740 |
*Returns:* If `p` owns a deleter `d` of type cv-unqualified `D`, returns
|
| 1741 |
`addressof(d)`; otherwise returns `nullptr`. The returned pointer
|
| 1742 |
remains valid as long as there exists a `shared_ptr` instance that owns
|
| 1743 |
`d`.
|
| 1744 |
|
| 1745 |
+
[*Note 13*: It is unspecified whether the pointer remains valid longer
|
| 1746 |
than that. This can happen if the implementation doesn’t destroy the
|
| 1747 |
deleter until all `weak_ptr` instances that share ownership with `p`
|
| 1748 |
have been destroyed. — *end note*]
|
| 1749 |
|
| 1750 |
+
##### I/O <a id="util.smartptr.shared.io">[[util.smartptr.shared.io]]</a>
|
| 1751 |
|
| 1752 |
``` cpp
|
| 1753 |
template<class E, class T, class Y>
|
| 1754 |
basic_ostream<E, T>& operator<<(basic_ostream<E, T>& os, const shared_ptr<Y>& p);
|
| 1755 |
```
|
| 1756 |
|
| 1757 |
*Effects:* As if by: `os << p.get();`
|
| 1758 |
|
| 1759 |
*Returns:* `os`.
|
| 1760 |
|
| 1761 |
+
#### Class template `weak_ptr` <a id="util.smartptr.weak">[[util.smartptr.weak]]</a>
|
| 1762 |
+
|
| 1763 |
+
##### General <a id="util.smartptr.weak.general">[[util.smartptr.weak.general]]</a>
|
| 1764 |
|
| 1765 |
The `weak_ptr` class template stores a weak reference to an object that
|
| 1766 |
is already managed by a `shared_ptr`. To access the object, a `weak_ptr`
|
| 1767 |
can be converted to a `shared_ptr` using the member function `lock`.
|
| 1768 |
|
|
|
|
| 1810 |
bool owner_before(const weak_ptr<U>& b) const noexcept;
|
| 1811 |
};
|
| 1812 |
|
| 1813 |
template<class T>
|
| 1814 |
weak_ptr(shared_ptr<T>) -> weak_ptr<T>;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1815 |
}
|
| 1816 |
```
|
| 1817 |
|
| 1818 |
Specializations of `weak_ptr` shall be *Cpp17CopyConstructible* and
|
| 1819 |
*Cpp17CopyAssignable*, allowing their use in standard containers. The
|
| 1820 |
template parameter `T` of `weak_ptr` may be an incomplete type.
|
| 1821 |
|
| 1822 |
+
##### Constructors <a id="util.smartptr.weak.const">[[util.smartptr.weak.const]]</a>
|
| 1823 |
|
| 1824 |
``` cpp
|
| 1825 |
constexpr weak_ptr() noexcept;
|
| 1826 |
```
|
| 1827 |
|
| 1828 |
+
*Effects:* Constructs an empty `weak_ptr` object that stores a null
|
| 1829 |
+
pointer value.
|
| 1830 |
|
| 1831 |
*Ensures:* `use_count() == 0`.
|
| 1832 |
|
| 1833 |
``` cpp
|
| 1834 |
weak_ptr(const weak_ptr& r) noexcept;
|
|
|
|
| 1837 |
```
|
| 1838 |
|
| 1839 |
*Constraints:* For the second and third constructors, `Y*` is compatible
|
| 1840 |
with `T*`.
|
| 1841 |
|
| 1842 |
+
*Effects:* If `r` is empty, constructs an empty `weak_ptr` object that
|
| 1843 |
+
stores a null pointer value; otherwise, constructs a `weak_ptr` object
|
| 1844 |
+
that shares ownership with `r` and stores a copy of the pointer stored
|
| 1845 |
+
in `r`.
|
| 1846 |
|
| 1847 |
*Ensures:* `use_count() == r.use_count()`.
|
| 1848 |
|
| 1849 |
``` cpp
|
| 1850 |
weak_ptr(weak_ptr&& r) noexcept;
|
|
|
|
| 1853 |
|
| 1854 |
*Constraints:* For the second constructor, `Y*` is compatible with `T*`.
|
| 1855 |
|
| 1856 |
*Effects:* Move constructs a `weak_ptr` instance from `r`.
|
| 1857 |
|
| 1858 |
+
*Ensures:* `*this` contains the old value of `r`. `r` is empty, stores a
|
| 1859 |
+
null pointer value, and `r.use_count() == 0`.
|
| 1860 |
|
| 1861 |
+
##### Destructor <a id="util.smartptr.weak.dest">[[util.smartptr.weak.dest]]</a>
|
| 1862 |
|
| 1863 |
``` cpp
|
| 1864 |
~weak_ptr();
|
| 1865 |
```
|
| 1866 |
|
| 1867 |
*Effects:* Destroys this `weak_ptr` object but has no effect on the
|
| 1868 |
object its stored pointer points to.
|
| 1869 |
|
| 1870 |
+
##### Assignment <a id="util.smartptr.weak.assign">[[util.smartptr.weak.assign]]</a>
|
| 1871 |
|
| 1872 |
``` cpp
|
| 1873 |
weak_ptr& operator=(const weak_ptr& r) noexcept;
|
| 1874 |
template<class Y> weak_ptr& operator=(const weak_ptr<Y>& r) noexcept;
|
| 1875 |
template<class Y> weak_ptr& operator=(const shared_ptr<Y>& r) noexcept;
|
| 1876 |
```
|
| 1877 |
|
| 1878 |
*Effects:* Equivalent to `weak_ptr(r).swap(*this)`.
|
| 1879 |
|
| 1880 |
+
*Returns:* `*this`.
|
| 1881 |
+
|
| 1882 |
*Remarks:* The implementation may meet the effects (and the implied
|
| 1883 |
guarantees) via different means, without creating a temporary object.
|
| 1884 |
|
|
|
|
|
|
|
| 1885 |
``` cpp
|
| 1886 |
weak_ptr& operator=(weak_ptr&& r) noexcept;
|
| 1887 |
template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept;
|
| 1888 |
```
|
| 1889 |
|
| 1890 |
*Effects:* Equivalent to `weak_ptr(std::move(r)).swap(*this)`.
|
| 1891 |
|
| 1892 |
*Returns:* `*this`.
|
| 1893 |
|
| 1894 |
+
##### Modifiers <a id="util.smartptr.weak.mod">[[util.smartptr.weak.mod]]</a>
|
| 1895 |
|
| 1896 |
``` cpp
|
| 1897 |
void swap(weak_ptr& r) noexcept;
|
| 1898 |
```
|
| 1899 |
|
|
|
|
| 1903 |
void reset() noexcept;
|
| 1904 |
```
|
| 1905 |
|
| 1906 |
*Effects:* Equivalent to `weak_ptr().swap(*this)`.
|
| 1907 |
|
| 1908 |
+
##### Observers <a id="util.smartptr.weak.obs">[[util.smartptr.weak.obs]]</a>
|
| 1909 |
|
| 1910 |
``` cpp
|
| 1911 |
long use_count() const noexcept;
|
| 1912 |
```
|
| 1913 |
|
|
|
|
| 1939 |
- under the equivalence relation defined by `owner_before`,
|
| 1940 |
`!a.owner_before(b) && !b.owner_before(a)`, two `shared_ptr` or
|
| 1941 |
`weak_ptr` instances are equivalent if and only if they share
|
| 1942 |
ownership or are both empty.
|
| 1943 |
|
| 1944 |
+
##### Specialized algorithms <a id="util.smartptr.weak.spec">[[util.smartptr.weak.spec]]</a>
|
| 1945 |
|
| 1946 |
``` cpp
|
| 1947 |
template<class T>
|
| 1948 |
void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
|
| 1949 |
```
|
| 1950 |
|
| 1951 |
*Effects:* Equivalent to `a.swap(b)`.
|
| 1952 |
|
| 1953 |
+
#### Class template `owner_less` <a id="util.smartptr.ownerless">[[util.smartptr.ownerless]]</a>
|
| 1954 |
|
| 1955 |
The class template `owner_less` allows ownership-based mixed comparisons
|
| 1956 |
of shared and weak pointers.
|
| 1957 |
|
| 1958 |
``` cpp
|
|
|
|
| 1992 |
|
| 1993 |
Note that
|
| 1994 |
|
| 1995 |
- `operator()` defines a strict weak ordering as defined in
|
| 1996 |
[[alg.sorting]];
|
| 1997 |
+
- two `shared_ptr` or `weak_ptr` instances are equivalent under the
|
| 1998 |
+
equivalence relation defined by `operator()`,
|
| 1999 |
+
`!operator()(a, b) && !operator()(b, a)`, if and only if they share
|
| 2000 |
ownership or are both empty.
|
| 2001 |
|
| 2002 |
— *end note*]
|
| 2003 |
|
| 2004 |
+
#### Class template `enable_shared_from_this` <a id="util.smartptr.enab">[[util.smartptr.enab]]</a>
|
| 2005 |
|
| 2006 |
A class `T` can inherit from `enable_shared_from_this<T>` to inherit the
|
| 2007 |
`shared_from_this` member functions that obtain a `shared_ptr` instance
|
| 2008 |
pointing to `*this`.
|
| 2009 |
|
|
|
|
| 2093 |
|
| 2094 |
For an object `p` of type `shared_ptr<T>`, `hash<shared_ptr<T>>()(p)`
|
| 2095 |
evaluates to the same value as
|
| 2096 |
`hash<typename shared_ptr<T>::element_type*>()(p.get())`.
|
| 2097 |
|
| 2098 |
+
### Smart pointer adaptors <a id="smartptr.adapt">[[smartptr.adapt]]</a>
|
| 2099 |
+
|
| 2100 |
+
#### Class template `out_ptr_t` <a id="out.ptr.t">[[out.ptr.t]]</a>
|
| 2101 |
+
|
| 2102 |
+
`out_ptr_t` is a class template used to adapt types such as smart
|
| 2103 |
+
pointers [[smartptr]] for functions that use output pointer parameters.
|
| 2104 |
+
|
| 2105 |
+
[*Example 1*:
|
| 2106 |
+
|
| 2107 |
+
``` cpp
|
| 2108 |
+
#include <memory>
|
| 2109 |
+
#include <cstdio>
|
| 2110 |
+
|
| 2111 |
+
int fopen_s(std::FILE** f, const char* name, const char* mode);
|
| 2112 |
+
|
| 2113 |
+
struct fclose_deleter {
|
| 2114 |
+
void operator()(std::FILE* f) const noexcept {
|
| 2115 |
+
std::fclose(f);
|
| 2116 |
+
}
|
| 2117 |
+
};
|
| 2118 |
+
|
| 2119 |
+
int main(int, char*[]) {
|
| 2120 |
+
constexpr const char* file_name = "ow.o";
|
| 2121 |
+
std::unique_ptr<std::FILE, fclose_deleter> file_ptr;
|
| 2122 |
+
int err = fopen_s(std::out_ptr<std::FILE*>(file_ptr), file_name, "r+b");
|
| 2123 |
+
if (err != 0)
|
| 2124 |
+
return 1;
|
| 2125 |
+
// *file_ptr is valid
|
| 2126 |
+
return 0;
|
| 2127 |
+
}
|
| 2128 |
+
```
|
| 2129 |
+
|
| 2130 |
+
`unique_ptr` can be used with `out_ptr` to be passed into an output
|
| 2131 |
+
pointer-style function, without needing to hold onto an intermediate
|
| 2132 |
+
pointer value and manually delete it on error or failure.
|
| 2133 |
+
|
| 2134 |
+
— *end example*]
|
| 2135 |
+
|
| 2136 |
+
``` cpp
|
| 2137 |
+
namespace std {
|
| 2138 |
+
template<class Smart, class Pointer, class... Args>
|
| 2139 |
+
class out_ptr_t {
|
| 2140 |
+
public:
|
| 2141 |
+
explicit out_ptr_t(Smart&, Args...);
|
| 2142 |
+
out_ptr_t(const out_ptr_t&) = delete;
|
| 2143 |
+
|
| 2144 |
+
~out_ptr_t();
|
| 2145 |
+
|
| 2146 |
+
operator Pointer*() const noexcept;
|
| 2147 |
+
operator void**() const noexcept;
|
| 2148 |
+
|
| 2149 |
+
private:
|
| 2150 |
+
Smart& s; // exposition only
|
| 2151 |
+
tuple<Args...> a; // exposition only
|
| 2152 |
+
Pointer p; // exposition only
|
| 2153 |
+
};
|
| 2154 |
+
}
|
| 2155 |
+
```
|
| 2156 |
+
|
| 2157 |
+
`Pointer` shall meet the *Cpp17NullablePointer* requirements. If `Smart`
|
| 2158 |
+
is a specialization of `shared_ptr` and `sizeof...(Args) == 0`, the
|
| 2159 |
+
program is ill-formed.
|
| 2160 |
+
|
| 2161 |
+
[*Note 1*: It is typically a user error to reset a `shared_ptr` without
|
| 2162 |
+
specifying a deleter, as `shared_ptr` will replace a custom deleter upon
|
| 2163 |
+
usage of `reset`, as specified in
|
| 2164 |
+
[[util.smartptr.shared.mod]]. — *end note*]
|
| 2165 |
+
|
| 2166 |
+
Program-defined specializations of `out_ptr_t` that depend on at least
|
| 2167 |
+
one program-defined type need not meet the requirements for the primary
|
| 2168 |
+
template.
|
| 2169 |
+
|
| 2170 |
+
Evaluations of the conversion functions on the same object may conflict
|
| 2171 |
+
[[intro.races]].
|
| 2172 |
+
|
| 2173 |
+
``` cpp
|
| 2174 |
+
explicit out_ptr_t(Smart& smart, Args... args);
|
| 2175 |
+
```
|
| 2176 |
+
|
| 2177 |
+
*Effects:* Initializes `s` with `smart`, `a` with
|
| 2178 |
+
`std::forward<Args>(args)...`, and value-initializes `p`. Then,
|
| 2179 |
+
equivalent to:
|
| 2180 |
+
|
| 2181 |
+
- ``` cpp
|
| 2182 |
+
s.reset();
|
| 2183 |
+
```
|
| 2184 |
+
|
| 2185 |
+
if the expression `s.reset()` is well-formed;
|
| 2186 |
+
|
| 2187 |
+
- otherwise,
|
| 2188 |
+
``` cpp
|
| 2189 |
+
s = Smart();
|
| 2190 |
+
```
|
| 2191 |
+
|
| 2192 |
+
if `is_constructible_v<Smart>` is `true`;
|
| 2193 |
+
|
| 2194 |
+
- otherwise, the program is ill-formed.
|
| 2195 |
+
|
| 2196 |
+
[*Note 1*: The constructor is not `noexcept` to allow for a variety of
|
| 2197 |
+
non-terminating and safe implementation strategies. For example, an
|
| 2198 |
+
implementation can allocate a `shared_ptr`’s internal node in the
|
| 2199 |
+
constructor and let implementation-defined exceptions escape safely. The
|
| 2200 |
+
destructor can then move the allocated control block in directly and
|
| 2201 |
+
avoid any other exceptions. — *end note*]
|
| 2202 |
+
|
| 2203 |
+
``` cpp
|
| 2204 |
+
~out_ptr_t();
|
| 2205 |
+
```
|
| 2206 |
+
|
| 2207 |
+
Let `SP` be *`POINTER_OF_OR`*`(Smart, Pointer)` [[memory.general]].
|
| 2208 |
+
|
| 2209 |
+
*Effects:* Equivalent to:
|
| 2210 |
+
|
| 2211 |
+
-
|
| 2212 |
+
``` cpp
|
| 2213 |
+
if (p) {
|
| 2214 |
+
apply([&](auto&&... args) {
|
| 2215 |
+
s.reset(static_cast<SP>(p), std::forward<Args>(args)...); }, std::move(a));
|
| 2216 |
+
}
|
| 2217 |
+
```
|
| 2218 |
+
|
| 2219 |
+
if the expression
|
| 2220 |
+
`s.reset(static_cast<SP>(p), std::forward<Args>(args)...)` is
|
| 2221 |
+
well-formed;
|
| 2222 |
+
- otherwise,
|
| 2223 |
+
``` cpp
|
| 2224 |
+
if (p) {
|
| 2225 |
+
apply([&](auto&&... args) {
|
| 2226 |
+
s = Smart(static_cast<SP>(p), std::forward<Args>(args)...); }, std::move(a));
|
| 2227 |
+
}
|
| 2228 |
+
```
|
| 2229 |
+
|
| 2230 |
+
if `is_constructible_v<Smart, SP, Args...>` is `true`;
|
| 2231 |
+
- otherwise, the program is ill-formed.
|
| 2232 |
+
|
| 2233 |
+
``` cpp
|
| 2234 |
+
operator Pointer*() const noexcept;
|
| 2235 |
+
```
|
| 2236 |
+
|
| 2237 |
+
*Preconditions:* `operator void**()` has not been called on `*this`.
|
| 2238 |
+
|
| 2239 |
+
*Returns:* `addressof(const_cast<Pointer&>(p))`.
|
| 2240 |
+
|
| 2241 |
+
``` cpp
|
| 2242 |
+
operator void**() const noexcept;
|
| 2243 |
+
```
|
| 2244 |
+
|
| 2245 |
+
*Constraints:* `is_same_v<Pointer, void*>` is `false`.
|
| 2246 |
+
|
| 2247 |
+
*Mandates:* `is_pointer_v<Pointer>` is `true`.
|
| 2248 |
+
|
| 2249 |
+
*Preconditions:* `operator Pointer*()` has not been called on `*this`.
|
| 2250 |
+
|
| 2251 |
+
*Returns:* A pointer value `v` such that:
|
| 2252 |
+
|
| 2253 |
+
- the initial value `*v` is equivalent to `static_cast<void*>(p)` and
|
| 2254 |
+
- any modification of `*v` that is not followed by a subsequent
|
| 2255 |
+
modification of `*this` affects the value of `p` during the
|
| 2256 |
+
destruction of `*this`, such that `static_cast<void*>(p) == *v`.
|
| 2257 |
+
|
| 2258 |
+
*Remarks:* Accessing `*v` outside the lifetime of `*this` has undefined
|
| 2259 |
+
behavior.
|
| 2260 |
+
|
| 2261 |
+
[*Note 2*: `reinterpret_cast<void**>(static_cast<Pointer*>(*this))` can
|
| 2262 |
+
be a viable implementation strategy for some
|
| 2263 |
+
implementations. — *end note*]
|
| 2264 |
+
|
| 2265 |
+
#### Function template `out_ptr` <a id="out.ptr">[[out.ptr]]</a>
|
| 2266 |
+
|
| 2267 |
+
``` cpp
|
| 2268 |
+
template<class Pointer = void, class Smart, class... Args>
|
| 2269 |
+
auto out_ptr(Smart& s, Args&&... args);
|
| 2270 |
+
```
|
| 2271 |
+
|
| 2272 |
+
Let `P` be `Pointer` if `is_void_v<Pointer>` is `false`, otherwise
|
| 2273 |
+
*`POINTER_OF`*`(Smart)`.
|
| 2274 |
+
|
| 2275 |
+
*Returns:*
|
| 2276 |
+
`out_ptr_t<Smart, P, Args&&...>(s, std::forward<Args>(args)...)`
|
| 2277 |
+
|
| 2278 |
+
#### Class template `inout_ptr_t` <a id="inout.ptr.t">[[inout.ptr.t]]</a>
|
| 2279 |
+
|
| 2280 |
+
`inout_ptr_t` is a class template used to adapt types such as smart
|
| 2281 |
+
pointers [[smartptr]] for functions that use output pointer parameters
|
| 2282 |
+
whose dereferenced values may first be deleted before being set to
|
| 2283 |
+
another allocated value.
|
| 2284 |
+
|
| 2285 |
+
[*Example 1*:
|
| 2286 |
+
|
| 2287 |
+
``` cpp
|
| 2288 |
+
#include <memory>
|
| 2289 |
+
|
| 2290 |
+
struct star_fish* star_fish_alloc();
|
| 2291 |
+
int star_fish_populate(struct star_fish** ps, const char* description);
|
| 2292 |
+
|
| 2293 |
+
struct star_fish_deleter {
|
| 2294 |
+
void operator() (struct star_fish* c) const noexcept;
|
| 2295 |
+
};
|
| 2296 |
+
|
| 2297 |
+
using star_fish_ptr = std::unique_ptr<star_fish, star_fish_deleter>;
|
| 2298 |
+
|
| 2299 |
+
int main(int, char*[]) {
|
| 2300 |
+
star_fish_ptr peach(star_fish_alloc());
|
| 2301 |
+
// ...
|
| 2302 |
+
// used, need to re-make
|
| 2303 |
+
int err = star_fish_populate(std::inout_ptr(peach), "caring clown-fish liker");
|
| 2304 |
+
return err;
|
| 2305 |
+
}
|
| 2306 |
+
```
|
| 2307 |
+
|
| 2308 |
+
A `unique_ptr` can be used with `inout_ptr` to be passed into an output
|
| 2309 |
+
pointer-style function. The original value will be properly deleted
|
| 2310 |
+
according to the function it is used with and a new value reset in its
|
| 2311 |
+
place.
|
| 2312 |
+
|
| 2313 |
+
— *end example*]
|
| 2314 |
+
|
| 2315 |
+
``` cpp
|
| 2316 |
+
namespace std {
|
| 2317 |
+
template<class Smart, class Pointer, class... Args>
|
| 2318 |
+
class inout_ptr_t {
|
| 2319 |
+
public:
|
| 2320 |
+
explicit inout_ptr_t(Smart&, Args...);
|
| 2321 |
+
inout_ptr_t(const inout_ptr_t&) = delete;
|
| 2322 |
+
|
| 2323 |
+
~inout_ptr_t();
|
| 2324 |
+
|
| 2325 |
+
operator Pointer*() const noexcept;
|
| 2326 |
+
operator void**() const noexcept;
|
| 2327 |
+
|
| 2328 |
+
private:
|
| 2329 |
+
Smart& s; // exposition only
|
| 2330 |
+
tuple<Args...> a; // exposition only
|
| 2331 |
+
Pointer p; // exposition only
|
| 2332 |
+
};
|
| 2333 |
+
}
|
| 2334 |
+
```
|
| 2335 |
+
|
| 2336 |
+
`Pointer` shall meet the *Cpp17NullablePointer* requirements. If `Smart`
|
| 2337 |
+
is a specialization of `shared_ptr`, the program is ill-formed.
|
| 2338 |
+
|
| 2339 |
+
[*Note 1*: It is impossible to properly acquire unique ownership of the
|
| 2340 |
+
managed resource from a `shared_ptr` given its shared ownership
|
| 2341 |
+
model. — *end note*]
|
| 2342 |
+
|
| 2343 |
+
Program-defined specializations of `inout_ptr_t` that depend on at least
|
| 2344 |
+
one program-defined type need not meet the requirements for the primary
|
| 2345 |
+
template.
|
| 2346 |
+
|
| 2347 |
+
Evaluations of the conversion functions on the same object may conflict
|
| 2348 |
+
[[intro.races]].
|
| 2349 |
+
|
| 2350 |
+
``` cpp
|
| 2351 |
+
explicit inout_ptr_t(Smart& smart, Args... args);
|
| 2352 |
+
```
|
| 2353 |
+
|
| 2354 |
+
*Effects:* Initializes `s` with `smart`, `a` with
|
| 2355 |
+
`std::forward<Args>(args)...`, and `p` to either
|
| 2356 |
+
|
| 2357 |
+
- `smart` if `is_pointer_v<Smart>` is `true`,
|
| 2358 |
+
- otherwise, `smart.get()`.
|
| 2359 |
+
|
| 2360 |
+
*Remarks:* An implementation can call `s.release()`.
|
| 2361 |
+
|
| 2362 |
+
[*Note 1*: The constructor is not `noexcept` to allow for a variety of
|
| 2363 |
+
non-terminating and safe implementation strategies. For example, an
|
| 2364 |
+
intrusive pointer implementation with a control block can allocate in
|
| 2365 |
+
the constructor and safely fail with an exception. — *end note*]
|
| 2366 |
+
|
| 2367 |
+
``` cpp
|
| 2368 |
+
~inout_ptr_t();
|
| 2369 |
+
```
|
| 2370 |
+
|
| 2371 |
+
Let `SP` be *`POINTER_OF_OR`*`(Smart, Pointer)` [[memory.general]].
|
| 2372 |
+
|
| 2373 |
+
Let *release-statement* be `s.release();` if an implementation does not
|
| 2374 |
+
call `s.release()` in the constructor. Otherwise, it is empty.
|
| 2375 |
+
|
| 2376 |
+
*Effects:* Equivalent to:
|
| 2377 |
+
|
| 2378 |
+
-
|
| 2379 |
+
``` cpp
|
| 2380 |
+
if (p) {
|
| 2381 |
+
apply([&](auto&&... args) {
|
| 2382 |
+
s = Smart( static_cast<SP>(p), std::forward<Args>(args)...); }, std::move(a));
|
| 2383 |
+
}
|
| 2384 |
+
```
|
| 2385 |
+
|
| 2386 |
+
if `is_pointer_v<Smart>` is `true`;
|
| 2387 |
+
- otherwise,
|
| 2388 |
+
``` cpp
|
| 2389 |
+
release-statement;
|
| 2390 |
+
if (p) {
|
| 2391 |
+
apply([&](auto&&... args) {
|
| 2392 |
+
s.reset(static_cast<SP>(p), std::forward<Args>(args)...); }, std::move(a));
|
| 2393 |
+
}
|
| 2394 |
+
```
|
| 2395 |
+
|
| 2396 |
+
if the expression
|
| 2397 |
+
`s.reset(static_cast<SP>(p), std::forward<Args>(args)...)` is well-
|
| 2398 |
+
formed;
|
| 2399 |
+
- otherwise,
|
| 2400 |
+
``` cpp
|
| 2401 |
+
release-statement;
|
| 2402 |
+
if (p) {
|
| 2403 |
+
apply([&](auto&&... args) {
|
| 2404 |
+
s = Smart(static_cast<SP>(p), std::forward<Args>(args)...); }, std::move(a));
|
| 2405 |
+
}
|
| 2406 |
+
```
|
| 2407 |
+
|
| 2408 |
+
if `is_constructible_v<Smart, SP, Args...>` is `true`;
|
| 2409 |
+
- otherwise, the program is ill-formed.
|
| 2410 |
+
|
| 2411 |
+
``` cpp
|
| 2412 |
+
operator Pointer*() const noexcept;
|
| 2413 |
+
```
|
| 2414 |
+
|
| 2415 |
+
*Preconditions:* `operator void**()` has not been called on `*this`.
|
| 2416 |
+
|
| 2417 |
+
*Returns:* `addressof(const_cast<Pointer&>(p))`.
|
| 2418 |
+
|
| 2419 |
+
``` cpp
|
| 2420 |
+
operator void**() const noexcept;
|
| 2421 |
+
```
|
| 2422 |
+
|
| 2423 |
+
*Constraints:* `is_same_v<Pointer, void*>` is `false`.
|
| 2424 |
+
|
| 2425 |
+
*Mandates:* `is_pointer_v<Pointer>` is `true`.
|
| 2426 |
+
|
| 2427 |
+
*Preconditions:* `operator Pointer*()` has not been called on `*this`.
|
| 2428 |
+
|
| 2429 |
+
*Returns:* A pointer value `v` such that:
|
| 2430 |
+
|
| 2431 |
+
- the initial value `*v` is equivalent to `static_cast<void*>(p)` and
|
| 2432 |
+
- any modification of `*v` that is not followed by subsequent
|
| 2433 |
+
modification of `*this` affects the value of `p` during the
|
| 2434 |
+
destruction of `*this`, such that `static_cast<void*>(p) == *v`.
|
| 2435 |
+
|
| 2436 |
+
*Remarks:* Accessing `*v` outside the lifetime of `*this` has undefined
|
| 2437 |
+
behavior.
|
| 2438 |
+
|
| 2439 |
+
[*Note 2*: `reinterpret_cast<void**>(static_cast<Pointer*>(*this))` can
|
| 2440 |
+
be a viable implementation strategy for some
|
| 2441 |
+
implementations. — *end note*]
|
| 2442 |
+
|
| 2443 |
+
#### Function template `inout_ptr` <a id="inout.ptr">[[inout.ptr]]</a>
|
| 2444 |
+
|
| 2445 |
+
``` cpp
|
| 2446 |
+
template<class Pointer = void, class Smart, class... Args>
|
| 2447 |
+
auto inout_ptr(Smart& s, Args&&... args);
|
| 2448 |
+
```
|
| 2449 |
+
|
| 2450 |
+
Let `P` be `Pointer` if `is_void_v<Pointer>` is `false`, otherwise
|
| 2451 |
+
*`POINTER_OF`*`(Smart)`.
|
| 2452 |
+
|
| 2453 |
+
*Returns:*
|
| 2454 |
+
`inout_ptr_t<Smart, P, Args&&...>(s, std::forward<Args>(args)...)`.
|
| 2455 |
+
|