- tmp/tmplc3wn656/{from.md → to.md} +728 -757
tmp/tmplc3wn656/{from.md → to.md}
RENAMED
|
@@ -4,105 +4,35 @@
|
|
| 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
|
| 9 |
-
scope
|
| 10 |
|
| 11 |
The mechanism by which *u* disposes of *p* is known as *p*’s associated
|
| 12 |
*deleter*, a function object whose correct invocation results in *p*’s
|
| 13 |
appropriate disposition (typically its deletion).
|
| 14 |
|
| 15 |
Let the notation *u.p* denote the pointer stored by *u*, and let *u.d*
|
| 16 |
denote the associated deleter. Upon request, *u* can *reset* (replace)
|
| 17 |
-
*u.p* and *u.d* with another pointer and deleter, but
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
Additionally, *u* can, upon request, *transfer ownership* to another
|
| 22 |
-
unique pointer *u2*. Upon completion of such a transfer, the following
|
| 23 |
-
postconditions hold:
|
| 24 |
-
|
| 25 |
-
- *u2.p* is equal to the pre-transfer *u.p*,
|
| 26 |
-
- *u.p* is equal to `nullptr`, and
|
| 27 |
-
- if the pre-transfer *u.d* maintained state, such state has been
|
| 28 |
-
transferred to *u2.d*.
|
| 29 |
-
|
| 30 |
-
As in the case of a reset, *u2* must properly dispose of its
|
| 31 |
-
pre-transfer owned object via the pre-transfer associated deleter before
|
| 32 |
-
the ownership transfer is considered complete.
|
| 33 |
-
|
| 34 |
-
[*Note 1*: A deleter’s state need never be copied, only moved or
|
| 35 |
-
swapped as ownership is transferred. — *end note*]
|
| 36 |
|
| 37 |
Each object of a type `U` instantiated from the `unique_ptr` template
|
| 38 |
specified in this subclause has the strict ownership semantics,
|
| 39 |
specified above, of a unique pointer. In partial satisfaction of these
|
| 40 |
-
semantics, each such `U` is
|
| 41 |
-
but is not
|
| 42 |
-
parameter `T` of `unique_ptr` may be
|
|
|
|
| 43 |
|
| 44 |
-
[*Note
|
| 45 |
for dynamically allocated memory, passing ownership of dynamically
|
| 46 |
allocated memory to a function, and returning dynamically allocated
|
| 47 |
memory from a function. — *end note*]
|
| 48 |
|
| 49 |
-
``` cpp
|
| 50 |
-
namespace std {
|
| 51 |
-
template<class T> struct default_delete;
|
| 52 |
-
template<class T> struct default_delete<T[]>;
|
| 53 |
-
|
| 54 |
-
template<class T, class D = default_delete<T>> class unique_ptr;
|
| 55 |
-
template<class T, class D> class unique_ptr<T[], D>;
|
| 56 |
-
|
| 57 |
-
template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args);
|
| 58 |
-
template<class T> unique_ptr<T> make_unique(size_t n);
|
| 59 |
-
template<class T, class... Args> unspecified make_unique(Args&&...) = delete;
|
| 60 |
-
|
| 61 |
-
template<class T, class D> void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
|
| 62 |
-
|
| 63 |
-
template<class T1, class D1, class T2, class D2>
|
| 64 |
-
bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
|
| 65 |
-
template<class T1, class D1, class T2, class D2>
|
| 66 |
-
bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
|
| 67 |
-
template<class T1, class D1, class T2, class D2>
|
| 68 |
-
bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
|
| 69 |
-
template<class T1, class D1, class T2, class D2>
|
| 70 |
-
bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
|
| 71 |
-
template<class T1, class D1, class T2, class D2>
|
| 72 |
-
bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
|
| 73 |
-
template<class T1, class D1, class T2, class D2>
|
| 74 |
-
bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
|
| 75 |
-
|
| 76 |
-
template <class T, class D>
|
| 77 |
-
bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
|
| 78 |
-
template <class T, class D>
|
| 79 |
-
bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
|
| 80 |
-
template <class T, class D>
|
| 81 |
-
bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
|
| 82 |
-
template <class T, class D>
|
| 83 |
-
bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
|
| 84 |
-
template <class T, class D>
|
| 85 |
-
bool operator<(const unique_ptr<T, D>& x, nullptr_t);
|
| 86 |
-
template <class T, class D>
|
| 87 |
-
bool operator<(nullptr_t, const unique_ptr<T, D>& y);
|
| 88 |
-
template <class T, class D>
|
| 89 |
-
bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
|
| 90 |
-
template <class T, class D>
|
| 91 |
-
bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
|
| 92 |
-
template <class T, class D>
|
| 93 |
-
bool operator>(const unique_ptr<T, D>& x, nullptr_t);
|
| 94 |
-
template <class T, class D>
|
| 95 |
-
bool operator>(nullptr_t, const unique_ptr<T, D>& y);
|
| 96 |
-
template <class T, class D>
|
| 97 |
-
bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
|
| 98 |
-
template <class T, class D>
|
| 99 |
-
bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
|
| 100 |
-
|
| 101 |
-
}
|
| 102 |
-
```
|
| 103 |
-
|
| 104 |
#### Default deleters <a id="unique.ptr.dltr">[[unique.ptr.dltr]]</a>
|
| 105 |
|
| 106 |
##### In general <a id="unique.ptr.dltr.general">[[unique.ptr.dltr.general]]</a>
|
| 107 |
|
| 108 |
The class template `default_delete` serves as the default deleter
|
|
@@ -125,24 +55,23 @@ namespace std {
|
|
| 125 |
|
| 126 |
``` cpp
|
| 127 |
template<class U> default_delete(const default_delete<U>& other) noexcept;
|
| 128 |
```
|
| 129 |
|
|
|
|
|
|
|
| 130 |
*Effects:* Constructs a `default_delete` object from another
|
| 131 |
`default_delete<U>` object.
|
| 132 |
|
| 133 |
-
*Remarks:* This constructor shall not participate in overload resolution
|
| 134 |
-
unless `U*` is implicitly convertible to `T*`.
|
| 135 |
-
|
| 136 |
``` cpp
|
| 137 |
void operator()(T* ptr) const;
|
| 138 |
```
|
| 139 |
|
|
|
|
|
|
|
| 140 |
*Effects:* Calls `delete` on `ptr`.
|
| 141 |
|
| 142 |
-
*Remarks:* If `T` is an incomplete type, the program is ill-formed.
|
| 143 |
-
|
| 144 |
##### `default_delete<T[]>` <a id="unique.ptr.dltr.dflt1">[[unique.ptr.dltr.dflt1]]</a>
|
| 145 |
|
| 146 |
``` cpp
|
| 147 |
namespace std {
|
| 148 |
template<class T> struct default_delete<T[]> {
|
|
@@ -155,26 +84,25 @@ namespace std {
|
|
| 155 |
|
| 156 |
``` cpp
|
| 157 |
template<class U> default_delete(const default_delete<U[]>& other) noexcept;
|
| 158 |
```
|
| 159 |
|
| 160 |
-
*
|
|
|
|
|
|
|
| 161 |
`default_delete<U[]>` object.
|
| 162 |
|
| 163 |
-
*Remarks:* This constructor shall not participate in overload resolution
|
| 164 |
-
unless `U(*)[]` is convertible to `T(*)[]`.
|
| 165 |
-
|
| 166 |
``` cpp
|
| 167 |
template<class U> void operator()(U* ptr) const;
|
| 168 |
```
|
| 169 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
*Effects:* Calls `delete[]` on `ptr`.
|
| 171 |
|
| 172 |
-
*Remarks:* If `U` is an incomplete type, the program is ill-formed. This
|
| 173 |
-
function shall not participate in overload resolution unless `U(*)[]` is
|
| 174 |
-
convertible to `T(*)[]`.
|
| 175 |
-
|
| 176 |
#### `unique_ptr` for single objects <a id="unique.ptr.single">[[unique.ptr.single]]</a>
|
| 177 |
|
| 178 |
``` cpp
|
| 179 |
namespace std {
|
| 180 |
template<class T, class D = default_delete<T>> class unique_ptr {
|
|
@@ -196,11 +124,12 @@ namespace std {
|
|
| 196 |
// [unique.ptr.single.dtor], destructor
|
| 197 |
~unique_ptr();
|
| 198 |
|
| 199 |
// [unique.ptr.single.asgn], assignment
|
| 200 |
unique_ptr& operator=(unique_ptr&& u) noexcept;
|
| 201 |
-
template
|
|
|
|
| 202 |
unique_ptr& operator=(nullptr_t) noexcept;
|
| 203 |
|
| 204 |
// [unique.ptr.single.observers], observers
|
| 205 |
add_lvalue_reference_t<T> operator*() const;
|
| 206 |
pointer operator->() const noexcept;
|
|
@@ -220,126 +149,107 @@ namespace std {
|
|
| 220 |
};
|
| 221 |
}
|
| 222 |
```
|
| 223 |
|
| 224 |
The default type for the template parameter `D` is `default_delete`. A
|
| 225 |
-
client-supplied template argument `D` shall be a function object type
|
| 226 |
-
[[function.objects]]
|
| 227 |
to function object type for which, given a value `d` of type `D` and a
|
| 228 |
value `ptr` of type `unique_ptr<T, D>::pointer`, the expression `d(ptr)`
|
| 229 |
is valid and has the effect of disposing of the pointer as appropriate
|
| 230 |
for that deleter.
|
| 231 |
|
| 232 |
-
If the deleter’s type `D` is not a reference type, `D` shall
|
| 233 |
-
requirements
|
| 234 |
|
| 235 |
If the *qualified-id* `remove_reference_t<D>::pointer` is valid and
|
| 236 |
-
denotes a type
|
| 237 |
D>::pointer` shall be a synonym for `remove_reference_t<D>::pointer`.
|
| 238 |
Otherwise `unique_ptr<T, D>::pointer` shall be a synonym for
|
| 239 |
`element_type*`. The type `unique_ptr<T,
|
| 240 |
-
D>::pointer` shall
|
| 241 |
-
[[
|
| 242 |
|
| 243 |
-
[*Example 1*: Given an allocator type `X`
|
| 244 |
-
|
| 245 |
`A::pointer`, `A::const_pointer`, `A::void_pointer`, and
|
| 246 |
`A::const_void_pointer` may be used as
|
| 247 |
`unique_ptr<T, D>::pointer`. — *end example*]
|
| 248 |
|
| 249 |
-
#####
|
| 250 |
|
| 251 |
``` cpp
|
| 252 |
constexpr unique_ptr() noexcept;
|
| 253 |
constexpr unique_ptr(nullptr_t) noexcept;
|
| 254 |
```
|
| 255 |
|
| 256 |
-
*
|
| 257 |
-
(
|
| 258 |
-
|
|
|
|
|
|
|
|
|
|
| 259 |
|
| 260 |
*Effects:* Constructs a `unique_ptr` object that owns nothing,
|
| 261 |
value-initializing the stored pointer and the stored deleter.
|
| 262 |
|
| 263 |
-
*
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
*Remarks:* If `is_pointer_v<deleter_type>` is `true` or
|
| 267 |
-
`is_default_constructible_v<deleter_type>` is `false`, this constructor
|
| 268 |
-
shall not participate in overload resolution.
|
| 269 |
|
| 270 |
``` cpp
|
| 271 |
explicit unique_ptr(pointer p) noexcept;
|
| 272 |
```
|
| 273 |
|
| 274 |
-
*
|
| 275 |
-
|
| 276 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
|
| 278 |
*Effects:* Constructs a `unique_ptr` which owns `p`, initializing the
|
| 279 |
stored pointer with `p` and value-initializing the stored deleter.
|
| 280 |
|
| 281 |
-
*
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
*Remarks:* If `is_pointer_v<deleter_type>` is `true` or
|
| 285 |
-
`is_default_constructible_v<deleter_type>` is `false`, this constructor
|
| 286 |
-
shall not participate in overload resolution. If class template argument
|
| 287 |
-
deduction ([[over.match.class.deduct]]) would select the function
|
| 288 |
-
template corresponding to this constructor, then the program is
|
| 289 |
-
ill-formed.
|
| 290 |
-
|
| 291 |
-
``` cpp
|
| 292 |
-
unique_ptr(pointer p, see below d1) noexcept;
|
| 293 |
-
unique_ptr(pointer p, see below d2) noexcept;
|
| 294 |
-
```
|
| 295 |
-
|
| 296 |
-
The signature of these constructors depends upon whether `D` is a
|
| 297 |
-
reference type. If `D` is a non-reference type `A`, then the signatures
|
| 298 |
-
are:
|
| 299 |
|
| 300 |
``` cpp
|
| 301 |
-
unique_ptr(pointer p, const
|
| 302 |
-
unique_ptr(pointer p,
|
| 303 |
```
|
| 304 |
|
| 305 |
-
|
| 306 |
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
unique_ptr(pointer p, A&& d) = delete;
|
| 310 |
-
```
|
| 311 |
-
|
| 312 |
-
If `D` is an lvalue reference type `const A&`, then the signatures are:
|
| 313 |
|
| 314 |
-
``
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
```
|
|
|
|
| 318 |
|
| 319 |
*Effects:* Constructs a `unique_ptr` object which owns `p`, initializing
|
| 320 |
the stored pointer with `p` and initializing the deleter from
|
| 321 |
`std::forward<decltype(d)>(d)`.
|
| 322 |
|
| 323 |
-
*
|
| 324 |
-
|
|
|
|
| 325 |
|
| 326 |
-
*
|
| 327 |
-
|
| 328 |
-
returns a reference to the lvalue `d`.
|
| 329 |
-
|
| 330 |
-
*Remarks:* If class template argument
|
| 331 |
-
deduction ([[over.match.class.deduct]]) would select a function
|
| 332 |
-
template corresponding to either of these constructors, then the program
|
| 333 |
-
is ill-formed.
|
| 334 |
|
| 335 |
[*Example 1*:
|
| 336 |
|
| 337 |
``` cpp
|
| 338 |
D d;
|
| 339 |
-
unique_ptr<int, D> p1(new int, D()); // D must be
|
| 340 |
-
unique_ptr<int, D> p2(new int, d); // D must be
|
| 341 |
unique_ptr<int, D&> p3(new int, d); // p3 holds a reference to d
|
| 342 |
unique_ptr<int, const D&> p4(new int, D()); // error: rvalue deleter object combined
|
| 343 |
// with reference deleter type
|
| 344 |
```
|
| 345 |
|
|
@@ -347,142 +257,144 @@ unique_ptr<int, const D&> p4(new int, D()); // error: rvalue deleter object comb
|
|
| 347 |
|
| 348 |
``` cpp
|
| 349 |
unique_ptr(unique_ptr&& u) noexcept;
|
| 350 |
```
|
| 351 |
|
| 352 |
-
*
|
| 353 |
-
requirements of `MoveConstructible` (Table [[tab:moveconstructible]]).
|
| 354 |
-
Construction of the deleter from an rvalue of type `D` shall not throw
|
| 355 |
-
an exception.
|
| 356 |
|
| 357 |
-
*
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
|
| 362 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 363 |
`std::forward<D>`. — *end note*]
|
| 364 |
|
| 365 |
-
*
|
| 366 |
-
construction. `get_deleter()` returns a reference
|
| 367 |
-
that was constructed from `u.get_deleter()`. If
|
| 368 |
-
then `get_deleter()` and `u.get_deleter()` both
|
| 369 |
-
lvalue deleter.
|
| 370 |
|
| 371 |
``` cpp
|
| 372 |
template<class U, class E> unique_ptr(unique_ptr<U, E>&& u) noexcept;
|
| 373 |
```
|
| 374 |
|
| 375 |
-
*
|
| 376 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 377 |
exception. Otherwise, `E` is a reference type and construction of the
|
| 378 |
-
deleter from an lvalue of type `E`
|
| 379 |
-
|
| 380 |
|
| 381 |
-
*
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
- `unique_ptr<U, E>::pointer` is implicitly convertible to `pointer`,
|
| 385 |
-
- `U` is not an array type, and
|
| 386 |
-
- either `D` is a reference type and `E` is the same type as `D`, or `D`
|
| 387 |
-
is not a reference type and `E` is implicitly convertible to `D`.
|
| 388 |
-
|
| 389 |
-
*Effects:* Constructs a `unique_ptr` by transferring ownership from `u`
|
| 390 |
-
to `*this`. If `E` is a reference type, this deleter is copy constructed
|
| 391 |
-
from `u`’s deleter; otherwise, this deleter is move constructed from
|
| 392 |
-
`u`’s deleter.
|
| 393 |
|
| 394 |
[*Note 2*: The deleter constructor can be implemented with
|
| 395 |
`std::forward<E>`. — *end note*]
|
| 396 |
|
| 397 |
-
*
|
| 398 |
-
construction. `get_deleter()` returns a reference
|
| 399 |
-
that was constructed from `u.get_deleter()`.
|
| 400 |
|
| 401 |
-
#####
|
| 402 |
|
| 403 |
``` cpp
|
| 404 |
~unique_ptr();
|
| 405 |
```
|
| 406 |
|
| 407 |
-
*
|
| 408 |
-
|
| 409 |
|
| 410 |
[*Note 3*: The use of `default_delete` requires `T` to be a complete
|
| 411 |
type. — *end note*]
|
| 412 |
|
| 413 |
*Effects:* If `get() == nullptr` there are no effects. Otherwise
|
| 414 |
`get_deleter()(get())`.
|
| 415 |
|
| 416 |
-
#####
|
| 417 |
|
| 418 |
``` cpp
|
| 419 |
unique_ptr& operator=(unique_ptr&& u) noexcept;
|
| 420 |
```
|
| 421 |
|
| 422 |
-
*
|
| 423 |
-
|
| 424 |
-
|
|
|
|
|
|
|
| 425 |
exception. Otherwise, `D` is a reference type; `remove_reference_t<D>`
|
| 426 |
-
|
| 427 |
-
deleter from an lvalue of type `D`
|
| 428 |
|
| 429 |
-
*Effects:*
|
| 430 |
-
`reset(u.release())` followed by
|
| 431 |
`get_deleter() = std::forward<D>(u.get_deleter())`.
|
| 432 |
|
| 433 |
*Returns:* `*this`.
|
| 434 |
|
|
|
|
|
|
|
| 435 |
``` cpp
|
| 436 |
template<class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
|
| 437 |
```
|
| 438 |
|
| 439 |
-
*
|
| 440 |
-
from an rvalue of type `E` shall be well-formed and shall not throw an
|
| 441 |
-
exception. Otherwise, `E` is a reference type and assignment of the
|
| 442 |
-
deleter from an lvalue of type `E` shall be well-formed and shall not
|
| 443 |
-
throw an exception.
|
| 444 |
-
|
| 445 |
-
*Remarks:* This operator shall not participate in overload resolution
|
| 446 |
-
unless:
|
| 447 |
|
| 448 |
- `unique_ptr<U, E>::pointer` is implicitly convertible to `pointer`,
|
| 449 |
and
|
| 450 |
- `U` is not an array type, and
|
| 451 |
- `is_assignable_v<D&, E&&>` is `true`.
|
| 452 |
|
| 453 |
-
*
|
| 454 |
-
`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 455 |
`get_deleter() = std::forward<E>(u.get_deleter())`.
|
| 456 |
|
| 457 |
*Returns:* `*this`.
|
| 458 |
|
|
|
|
|
|
|
| 459 |
``` cpp
|
| 460 |
unique_ptr& operator=(nullptr_t) noexcept;
|
| 461 |
```
|
| 462 |
|
| 463 |
*Effects:* As if by `reset()`.
|
| 464 |
|
| 465 |
-
*
|
| 466 |
|
| 467 |
*Returns:* `*this`.
|
| 468 |
|
| 469 |
-
#####
|
| 470 |
|
| 471 |
``` cpp
|
| 472 |
add_lvalue_reference_t<T> operator*() const;
|
| 473 |
```
|
| 474 |
|
| 475 |
-
*
|
| 476 |
|
| 477 |
*Returns:* `*get()`.
|
| 478 |
|
| 479 |
``` cpp
|
| 480 |
pointer operator->() const noexcept;
|
| 481 |
```
|
| 482 |
|
| 483 |
-
*
|
| 484 |
|
| 485 |
*Returns:* `get()`.
|
| 486 |
|
| 487 |
[*Note 4*: The use of this function typically requires that `T` be a
|
| 488 |
complete type. — *end note*]
|
|
@@ -504,47 +416,46 @@ const deleter_type& get_deleter() const noexcept;
|
|
| 504 |
explicit operator bool() const noexcept;
|
| 505 |
```
|
| 506 |
|
| 507 |
*Returns:* `get() != nullptr`.
|
| 508 |
|
| 509 |
-
#####
|
| 510 |
|
| 511 |
``` cpp
|
| 512 |
pointer release() noexcept;
|
| 513 |
```
|
| 514 |
|
| 515 |
-
*
|
| 516 |
|
| 517 |
*Returns:* The value `get()` had at the start of the call to `release`.
|
| 518 |
|
| 519 |
``` cpp
|
| 520 |
void reset(pointer p = pointer()) noexcept;
|
| 521 |
```
|
| 522 |
|
| 523 |
-
*
|
| 524 |
-
|
| 525 |
|
| 526 |
*Effects:* Assigns `p` to the stored pointer, and then if and only if
|
| 527 |
the old value of the stored pointer, `old_p`, was not equal to
|
| 528 |
`nullptr`, calls `get_deleter()(old_p)`.
|
| 529 |
|
| 530 |
[*Note 5*: The order of these operations is significant because the
|
| 531 |
call to `get_deleter()` may destroy `*this`. — *end note*]
|
| 532 |
|
| 533 |
-
*
|
| 534 |
|
| 535 |
[*Note 6*: The postcondition does not hold if the call to
|
| 536 |
`get_deleter()` destroys `*this` since `this->get()` is no longer a
|
| 537 |
valid expression. — *end note*]
|
| 538 |
|
| 539 |
``` cpp
|
| 540 |
void swap(unique_ptr& u) noexcept;
|
| 541 |
```
|
| 542 |
|
| 543 |
-
*
|
| 544 |
-
|
| 545 |
-
under `swap`.
|
| 546 |
|
| 547 |
*Effects:* Invokes `swap` on the stored pointers and on the stored
|
| 548 |
deleters of `*this` and `u`.
|
| 549 |
|
| 550 |
#### `unique_ptr` for array objects with a runtime length <a id="unique.ptr.runtime">[[unique.ptr.runtime]]</a>
|
|
@@ -612,19 +523,20 @@ interface.
|
|
| 612 |
Descriptions are provided below only for members that differ from the
|
| 613 |
primary template.
|
| 614 |
|
| 615 |
The template argument `T` shall be a complete type.
|
| 616 |
|
| 617 |
-
#####
|
| 618 |
|
| 619 |
``` cpp
|
| 620 |
template<class U> explicit unique_ptr(U p) noexcept;
|
| 621 |
```
|
| 622 |
|
| 623 |
This constructor behaves the same as the constructor in the primary
|
| 624 |
-
template that takes a single parameter of type `pointer`
|
| 625 |
-
|
|
|
|
| 626 |
|
| 627 |
- `U` is the same type as `pointer`, or
|
| 628 |
- `pointer` is the same type as `element_type*`, `U` is a pointer type
|
| 629 |
`V*`, and `V(*)[]` is convertible to `element_type(*)[]`.
|
| 630 |
|
|
@@ -632,70 +544,68 @@ additionally shall not participate in overload resolution unless
|
|
| 632 |
template<class U> unique_ptr(U p, see below d) noexcept;
|
| 633 |
template<class U> unique_ptr(U p, see below d) noexcept;
|
| 634 |
```
|
| 635 |
|
| 636 |
These constructors behave the same as the constructors in the primary
|
| 637 |
-
template that take a parameter of type `pointer` and a second parameter
|
| 638 |
-
|
| 639 |
-
|
| 640 |
|
| 641 |
- `U` is the same type as `pointer`,
|
| 642 |
- `U` is `nullptr_t`, or
|
| 643 |
- `pointer` is the same type as `element_type*`, `U` is a pointer type
|
| 644 |
`V*`, and `V(*)[]` is convertible to `element_type(*)[]`.
|
| 645 |
|
| 646 |
``` cpp
|
| 647 |
-
template
|
| 648 |
-
unique_ptr(unique_ptr<U, E>&& u) noexcept;
|
| 649 |
```
|
| 650 |
|
| 651 |
-
This constructor behaves the same as in the primary template
|
| 652 |
-
|
| 653 |
-
|
| 654 |
|
| 655 |
- `U` is an array type, and
|
| 656 |
- `pointer` is the same type as `element_type*`, and
|
| 657 |
- `UP::pointer` is the same type as `UP::element_type*`, and
|
| 658 |
- `UP::element_type(*)[]` is convertible to `element_type(*)[]`, and
|
| 659 |
- either `D` is a reference type and `E` is the same type as `D`, or `D`
|
| 660 |
is not a reference type and `E` is implicitly convertible to `D`.
|
| 661 |
|
| 662 |
-
[*Note 1*: This replaces the
|
| 663 |
-
primary template — *end note*]
|
| 664 |
|
| 665 |
-
#####
|
| 666 |
|
| 667 |
``` cpp
|
| 668 |
-
template
|
| 669 |
-
unique_ptr& operator=(unique_ptr<U, E>&& u)noexcept;
|
| 670 |
```
|
| 671 |
|
| 672 |
-
This operator behaves the same as in the primary template
|
| 673 |
-
|
| 674 |
-
|
| 675 |
|
| 676 |
- `U` is an array type, and
|
| 677 |
- `pointer` is the same type as `element_type*`, and
|
| 678 |
- `UP::pointer` is the same type as `UP::element_type*`, and
|
| 679 |
- `UP::element_type(*)[]` is convertible to `element_type(*)[]`, and
|
| 680 |
- `is_assignable_v<D&, E&&>` is `true`.
|
| 681 |
|
| 682 |
-
[*Note 2*: This replaces the
|
| 683 |
-
primary template — *end note*]
|
| 684 |
|
| 685 |
-
#####
|
| 686 |
|
| 687 |
``` cpp
|
| 688 |
T& operator[](size_t i) const;
|
| 689 |
```
|
| 690 |
|
| 691 |
-
*
|
| 692 |
stored pointer points.
|
| 693 |
|
| 694 |
*Returns:* `get()[i]`.
|
| 695 |
|
| 696 |
-
#####
|
| 697 |
|
| 698 |
``` cpp
|
| 699 |
void reset(nullptr_t p = nullptr) noexcept;
|
| 700 |
```
|
| 701 |
|
|
@@ -704,145 +614,165 @@ void reset(nullptr_t p = nullptr) noexcept;
|
|
| 704 |
``` cpp
|
| 705 |
template<class U> void reset(U p) noexcept;
|
| 706 |
```
|
| 707 |
|
| 708 |
This function behaves the same as the `reset` member of the primary
|
| 709 |
-
template
|
| 710 |
-
|
|
|
|
| 711 |
|
| 712 |
- `U` is the same type as `pointer`, or
|
| 713 |
- `pointer` is the same type as `element_type*`, `U` is a pointer type
|
| 714 |
`V*`, and `V(*)[]` is convertible to `element_type(*)[]`.
|
| 715 |
|
| 716 |
-
####
|
| 717 |
|
| 718 |
``` cpp
|
| 719 |
template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args);
|
| 720 |
```
|
| 721 |
|
| 722 |
-
*
|
| 723 |
-
unless `T` is not an array.
|
| 724 |
|
| 725 |
*Returns:* `unique_ptr<T>(new T(std::forward<Args>(args)...))`.
|
| 726 |
|
| 727 |
``` cpp
|
| 728 |
template<class T> unique_ptr<T> make_unique(size_t n);
|
| 729 |
```
|
| 730 |
|
| 731 |
-
*
|
| 732 |
-
unless `T` is an array of unknown bound.
|
| 733 |
|
| 734 |
*Returns:* `unique_ptr<T>(new remove_extent_t<T>[n]())`.
|
| 735 |
|
| 736 |
``` cpp
|
| 737 |
template<class T, class... Args> unspecified make_unique(Args&&...) = delete;
|
| 738 |
```
|
| 739 |
|
| 740 |
-
*
|
| 741 |
-
unless `T` is an array of known bound.
|
| 742 |
|
| 743 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 744 |
|
| 745 |
``` cpp
|
| 746 |
template<class T, class D> void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
|
| 747 |
```
|
| 748 |
|
| 749 |
-
*
|
| 750 |
-
unless `is_swappable_v<D>` is `true`.
|
| 751 |
|
| 752 |
*Effects:* Calls `x.swap(y)`.
|
| 753 |
|
| 754 |
``` cpp
|
| 755 |
template<class T1, class D1, class T2, class D2>
|
| 756 |
bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
|
| 757 |
```
|
| 758 |
|
| 759 |
*Returns:* `x.get() == y.get()`.
|
| 760 |
|
| 761 |
-
``` cpp
|
| 762 |
-
template <class T1, class D1, class T2, class D2>
|
| 763 |
-
bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
|
| 764 |
-
```
|
| 765 |
-
|
| 766 |
-
*Returns:* `x.get() != y.get()`.
|
| 767 |
-
|
| 768 |
``` cpp
|
| 769 |
template<class T1, class D1, class T2, class D2>
|
| 770 |
bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
|
| 771 |
```
|
| 772 |
|
| 773 |
-
|
| 774 |
|
| 775 |
``` cpp
|
| 776 |
common_type_t<typename unique_ptr<T1, D1>::pointer,
|
| 777 |
typename unique_ptr<T2, D2>::pointer>
|
| 778 |
```
|
| 779 |
|
| 780 |
-
|
| 781 |
-
type ([[function.objects]]) that induces a strict weak
|
| 782 |
-
ordering ([[alg.sorting]]) on the pointer values.
|
| 783 |
|
| 784 |
-
|
|
|
|
| 785 |
|
| 786 |
-
*
|
| 787 |
-
|
| 788 |
-
|
| 789 |
|
| 790 |
-
`
|
| 791 |
-
template <class T1, class D1, class T2, class D2>
|
| 792 |
-
bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
|
| 793 |
-
```
|
| 794 |
-
|
| 795 |
-
*Returns:* `!(y < x)`.
|
| 796 |
|
| 797 |
``` cpp
|
| 798 |
template<class T1, class D1, class T2, class D2>
|
| 799 |
bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
|
| 800 |
```
|
| 801 |
|
| 802 |
*Returns:* `y < x`.
|
| 803 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 804 |
``` cpp
|
| 805 |
template<class T1, class D1, class T2, class D2>
|
| 806 |
bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
|
| 807 |
```
|
| 808 |
|
| 809 |
*Returns:* `!(x < y)`.
|
| 810 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 811 |
``` cpp
|
| 812 |
template<class T, class D>
|
| 813 |
bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
|
| 814 |
-
template <class T, class D>
|
| 815 |
-
bool operator==(nullptr_t, const unique_ptr<T, D>& x) noexcept;
|
| 816 |
```
|
| 817 |
|
| 818 |
*Returns:* `!x`.
|
| 819 |
|
| 820 |
-
``` cpp
|
| 821 |
-
template <class T, class D>
|
| 822 |
-
bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
|
| 823 |
-
template <class T, class D>
|
| 824 |
-
bool operator!=(nullptr_t, const unique_ptr<T, D>& x) noexcept;
|
| 825 |
-
```
|
| 826 |
-
|
| 827 |
-
*Returns:* `(bool)x`.
|
| 828 |
-
|
| 829 |
``` cpp
|
| 830 |
template<class T, class D>
|
| 831 |
bool operator<(const unique_ptr<T, D>& x, nullptr_t);
|
| 832 |
template<class T, class D>
|
| 833 |
bool operator<(nullptr_t, const unique_ptr<T, D>& x);
|
| 834 |
```
|
| 835 |
|
| 836 |
-
*
|
| 837 |
-
|
| 838 |
-
|
| 839 |
|
| 840 |
*Returns:* The first function template returns
|
| 841 |
-
|
| 842 |
-
`
|
| 843 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 844 |
|
| 845 |
``` cpp
|
| 846 |
template<class T, class D>
|
| 847 |
bool operator>(const unique_ptr<T, D>& x, nullptr_t);
|
| 848 |
template<class T, class D>
|
|
@@ -870,33 +800,54 @@ template <class T, class D>
|
|
| 870 |
```
|
| 871 |
|
| 872 |
*Returns:* The first function template returns `!(x < nullptr)`. The
|
| 873 |
second function template returns `!(nullptr < x)`.
|
| 874 |
|
| 875 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 876 |
|
| 877 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 878 |
|
| 879 |
``` cpp
|
| 880 |
namespace std {
|
| 881 |
class bad_weak_ptr : public exception {
|
| 882 |
public:
|
| 883 |
-
|
|
|
|
| 884 |
};
|
| 885 |
}
|
| 886 |
```
|
| 887 |
|
| 888 |
An exception of type `bad_weak_ptr` is thrown by the `shared_ptr`
|
| 889 |
constructor taking a `weak_ptr`.
|
| 890 |
|
| 891 |
``` cpp
|
| 892 |
-
|
| 893 |
```
|
| 894 |
|
| 895 |
-
*
|
| 896 |
|
| 897 |
-
###
|
| 898 |
|
| 899 |
The `shared_ptr` class template stores a pointer, usually obtained via
|
| 900 |
`new`. `shared_ptr` implements semantics of shared ownership; the last
|
| 901 |
remaining owner of the pointer is responsible for destroying the object,
|
| 902 |
or otherwise releasing the resources associated with the stored pointer.
|
|
@@ -909,130 +860,88 @@ namespace std {
|
|
| 909 |
using element_type = remove_extent_t<T>;
|
| 910 |
using weak_type = weak_ptr<T>;
|
| 911 |
|
| 912 |
// [util.smartptr.shared.const], constructors
|
| 913 |
constexpr shared_ptr() noexcept;
|
| 914 |
-
template<class Y> explicit shared_ptr(Y* p);
|
| 915 |
-
template<class Y, class D> shared_ptr(Y* p, D d);
|
| 916 |
-
template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
|
| 917 |
-
template <class D> shared_ptr(nullptr_t p, D d);
|
| 918 |
-
template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
|
| 919 |
-
template<class Y> shared_ptr(const shared_ptr<Y>& r, element_type* p) noexcept;
|
| 920 |
-
shared_ptr(const shared_ptr& r) noexcept;
|
| 921 |
-
template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
|
| 922 |
-
shared_ptr(shared_ptr&& r) noexcept;
|
| 923 |
-
template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
|
| 924 |
-
template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
|
| 925 |
-
template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
|
| 926 |
constexpr shared_ptr(nullptr_t) noexcept : shared_ptr() { }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 927 |
|
| 928 |
// [util.smartptr.shared.dest], destructor
|
| 929 |
~shared_ptr();
|
| 930 |
|
| 931 |
// [util.smartptr.shared.assign], assignment
|
| 932 |
shared_ptr& operator=(const shared_ptr& r) noexcept;
|
| 933 |
-
template<class Y>
|
|
|
|
| 934 |
shared_ptr& operator=(shared_ptr&& r) noexcept;
|
| 935 |
-
template<class Y>
|
| 936 |
-
|
|
|
|
|
|
|
| 937 |
|
| 938 |
// [util.smartptr.shared.mod], modifiers
|
| 939 |
void swap(shared_ptr& r) noexcept;
|
| 940 |
void reset() noexcept;
|
| 941 |
-
template<class Y>
|
| 942 |
-
|
| 943 |
-
template<class Y, class D
|
|
|
|
|
|
|
|
|
|
| 944 |
|
| 945 |
// [util.smartptr.shared.obs], observers
|
| 946 |
element_type* get() const noexcept;
|
| 947 |
T& operator*() const noexcept;
|
| 948 |
T* operator->() const noexcept;
|
| 949 |
element_type& operator[](ptrdiff_t i) const;
|
| 950 |
long use_count() const noexcept;
|
| 951 |
explicit operator bool() const noexcept;
|
| 952 |
-
template<class U>
|
| 953 |
-
|
|
|
|
|
|
|
| 954 |
};
|
| 955 |
|
| 956 |
-
template<class T> shared_ptr(weak_ptr<T>) -> shared_ptr<T>;
|
| 957 |
-
template<class T, class D> shared_ptr(unique_ptr<T, D>) -> shared_ptr<T>;
|
| 958 |
-
|
| 959 |
-
// [util.smartptr.shared.create], shared_ptr creation
|
| 960 |
-
template<class T, class... Args>
|
| 961 |
-
shared_ptr<T> make_shared(Args&&... args);
|
| 962 |
-
template<class T, class A, class... Args>
|
| 963 |
-
shared_ptr<T> allocate_shared(const A& a, Args&&... args);
|
| 964 |
-
|
| 965 |
-
// [util.smartptr.shared.cmp], shared_ptr comparisons
|
| 966 |
-
template<class T, class U>
|
| 967 |
-
bool operator==(const shared_ptr<T>& a, const shared_ptr<U>& b) noexcept;
|
| 968 |
-
template<class T, class U>
|
| 969 |
-
bool operator!=(const shared_ptr<T>& a, const shared_ptr<U>& b) noexcept;
|
| 970 |
-
template<class T, class U>
|
| 971 |
-
bool operator<(const shared_ptr<T>& a, const shared_ptr<U>& b) noexcept;
|
| 972 |
-
template<class T, class U>
|
| 973 |
-
bool operator>(const shared_ptr<T>& a, const shared_ptr<U>& b) noexcept;
|
| 974 |
-
template<class T, class U>
|
| 975 |
-
bool operator<=(const shared_ptr<T>& a, const shared_ptr<U>& b) noexcept;
|
| 976 |
-
template<class T, class U>
|
| 977 |
-
bool operator>=(const shared_ptr<T>& a, const shared_ptr<U>& b) noexcept;
|
| 978 |
-
|
| 979 |
template<class T>
|
| 980 |
-
|
| 981 |
-
template
|
| 982 |
-
|
| 983 |
-
template <class T>
|
| 984 |
-
bool operator!=(const shared_ptr<T>& a, nullptr_t) noexcept;
|
| 985 |
-
template <class T>
|
| 986 |
-
bool operator!=(nullptr_t, const shared_ptr<T>& b) noexcept;
|
| 987 |
-
template <class T>
|
| 988 |
-
bool operator<(const shared_ptr<T>& a, nullptr_t) noexcept;
|
| 989 |
-
template <class T>
|
| 990 |
-
bool operator<(nullptr_t, const shared_ptr<T>& b) noexcept;
|
| 991 |
-
template <class T>
|
| 992 |
-
bool operator<=(const shared_ptr<T>& a, nullptr_t) noexcept;
|
| 993 |
-
template <class T>
|
| 994 |
-
bool operator<=(nullptr_t, const shared_ptr<T>& b) noexcept;
|
| 995 |
-
template <class T>
|
| 996 |
-
bool operator>(const shared_ptr<T>& a, nullptr_t) noexcept;
|
| 997 |
-
template <class T>
|
| 998 |
-
bool operator>(nullptr_t, const shared_ptr<T>& b) noexcept;
|
| 999 |
-
template <class T>
|
| 1000 |
-
bool operator>=(const shared_ptr<T>& a, nullptr_t) noexcept;
|
| 1001 |
-
template <class T>
|
| 1002 |
-
bool operator>=(nullptr_t, const shared_ptr<T>& b) noexcept;
|
| 1003 |
-
|
| 1004 |
-
// [util.smartptr.shared.spec], shared_ptr specialized algorithms
|
| 1005 |
-
template<class T>
|
| 1006 |
-
void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
|
| 1007 |
-
|
| 1008 |
-
// [util.smartptr.shared.cast], shared_ptr casts
|
| 1009 |
-
template<class T, class U>
|
| 1010 |
-
shared_ptr<T> static_pointer_cast(const shared_ptr<U>& r) noexcept;
|
| 1011 |
-
template<class T, class U>
|
| 1012 |
-
shared_ptr<T> dynamic_pointer_cast(const shared_ptr<U>& r) noexcept;
|
| 1013 |
-
template<class T, class U>
|
| 1014 |
-
shared_ptr<T> const_pointer_cast(const shared_ptr<U>& r) noexcept;
|
| 1015 |
-
template<class T, class U>
|
| 1016 |
-
shared_ptr<T> reinterpret_pointer_cast(const shared_ptr<U>& r) noexcept;
|
| 1017 |
-
|
| 1018 |
-
// [util.smartptr.getdeleter], shared_ptr get_deleter
|
| 1019 |
-
template<class D, class T>
|
| 1020 |
-
D* get_deleter(const shared_ptr<T>& p) noexcept;
|
| 1021 |
-
|
| 1022 |
-
// [util.smartptr.shared.io], shared_ptr I/O
|
| 1023 |
-
template<class E, class T, class Y>
|
| 1024 |
-
basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, const shared_ptr<Y>& p);
|
| 1025 |
}
|
| 1026 |
```
|
| 1027 |
|
| 1028 |
-
Specializations of `shared_ptr` shall be
|
| 1029 |
-
|
| 1030 |
-
standard containers. Specializations of `shared_ptr` shall be
|
| 1031 |
contextually convertible to `bool`, allowing their use in boolean
|
| 1032 |
-
expressions and declarations in conditions.
|
| 1033 |
-
|
|
|
|
|
|
|
|
|
|
| 1034 |
|
| 1035 |
[*Example 1*:
|
| 1036 |
|
| 1037 |
``` cpp
|
| 1038 |
if (shared_ptr<X> px = dynamic_pointer_cast<X>(py)) {
|
|
@@ -1046,173 +955,168 @@ For purposes of determining the presence of a data race, member
|
|
| 1046 |
functions shall access and modify only the `shared_ptr` and `weak_ptr`
|
| 1047 |
objects themselves and not objects they refer to. Changes in
|
| 1048 |
`use_count()` do not reflect modifications that can introduce data
|
| 1049 |
races.
|
| 1050 |
|
| 1051 |
-
For the purposes of subclause [[
|
| 1052 |
-
|
| 1053 |
convertible to `T*` or `Y` is `U[N]` and `T` is cv `U[]`.
|
| 1054 |
|
| 1055 |
-
####
|
| 1056 |
|
| 1057 |
In the constructor definitions below, enables `shared_from_this` with
|
| 1058 |
`p`, for a pointer `p` of type `Y*`, means that if `Y` has an
|
| 1059 |
unambiguous and accessible base class that is a specialization of
|
| 1060 |
-
`enable_shared_from_this`
|
| 1061 |
-
|
| 1062 |
-
|
| 1063 |
|
| 1064 |
``` cpp
|
| 1065 |
if (p != nullptr && p->weak_this.expired())
|
| 1066 |
p->weak_this = shared_ptr<remove_cv_t<Y>>(*this, const_cast<remove_cv_t<Y>*>(p));
|
| 1067 |
```
|
| 1068 |
|
| 1069 |
The assignment to the `weak_this` member is not atomic and conflicts
|
| 1070 |
-
with any potentially concurrent access to the same object
|
| 1071 |
-
[[intro.multithread]]
|
| 1072 |
|
| 1073 |
``` cpp
|
| 1074 |
constexpr shared_ptr() noexcept;
|
| 1075 |
```
|
| 1076 |
|
| 1077 |
-
*
|
| 1078 |
-
|
| 1079 |
-
*Postconditions:* `use_count() == 0 && get() == nullptr`.
|
| 1080 |
|
| 1081 |
``` cpp
|
| 1082 |
template<class Y> explicit shared_ptr(Y* p);
|
| 1083 |
```
|
| 1084 |
|
| 1085 |
-
*
|
| 1086 |
-
|
| 1087 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1088 |
|
| 1089 |
*Effects:* When `T` is not an array type, constructs a `shared_ptr`
|
| 1090 |
object that owns the pointer `p`. Otherwise, constructs a `shared_ptr`
|
| 1091 |
that owns `p` and a deleter of an unspecified type that calls
|
| 1092 |
`delete[] p`. When `T` is not an array type, enables `shared_from_this`
|
| 1093 |
with `p`. If an exception is thrown, `delete p` is called when `T` is
|
| 1094 |
not an array type, `delete[] p` otherwise.
|
| 1095 |
|
| 1096 |
-
*
|
| 1097 |
|
| 1098 |
*Throws:* `bad_alloc`, or an *implementation-defined* exception when a
|
| 1099 |
resource other than memory could not be obtained.
|
| 1100 |
|
| 1101 |
-
*Remarks:* When `T` is an array type, this constructor shall not
|
| 1102 |
-
participate in overload resolution unless the expression `delete[] p` is
|
| 1103 |
-
well-formed and either `T` is `U[N]` and `Y(*)[N]` is convertible to
|
| 1104 |
-
`T*`, or `T` is `U[]` and `Y(*)[]` is convertible to `T*`. When `T` is
|
| 1105 |
-
not an array type, this constructor shall not participate in overload
|
| 1106 |
-
resolution unless the expression `delete p` is well-formed and `Y*` is
|
| 1107 |
-
convertible to `T*`.
|
| 1108 |
-
|
| 1109 |
``` cpp
|
| 1110 |
template<class Y, class D> shared_ptr(Y* p, D d);
|
| 1111 |
template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
|
| 1112 |
template<class D> shared_ptr(nullptr_t p, D d);
|
| 1113 |
template<class D, class A> shared_ptr(nullptr_t p, D d, A a);
|
| 1114 |
```
|
| 1115 |
|
| 1116 |
-
*
|
| 1117 |
-
|
| 1118 |
-
|
| 1119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1120 |
|
| 1121 |
*Effects:* Constructs a `shared_ptr` object that owns the object `p` and
|
| 1122 |
the deleter `d`. When `T` is not an array type, the first and second
|
| 1123 |
constructors enable `shared_from_this` with `p`. The second and fourth
|
| 1124 |
constructors shall use a copy of `a` to allocate memory for internal
|
| 1125 |
use. If an exception is thrown, `d(p)` is called.
|
| 1126 |
|
| 1127 |
-
*
|
| 1128 |
|
| 1129 |
*Throws:* `bad_alloc`, or an *implementation-defined* exception when a
|
| 1130 |
resource other than memory could not be obtained.
|
| 1131 |
|
| 1132 |
-
*Remarks:* When `T` is an array type, this constructor shall not
|
| 1133 |
-
participate in overload resolution unless `is_move_constructible_v<D>`
|
| 1134 |
-
is `true`, the expression `d(p)` is well-formed, and either `T` is
|
| 1135 |
-
`U[N]` and `Y(*)[N]` is convertible to `T*`, or `T` is `U[]` and
|
| 1136 |
-
`Y(*)[]` is convertible to `T*`. When `T` is not an array type, this
|
| 1137 |
-
constructor shall not participate in overload resolution unless
|
| 1138 |
-
`is_move_constructible_v<D>` is `true`, the expression `d(p)` is
|
| 1139 |
-
well-formed, and `Y*` is convertible to `T*`.
|
| 1140 |
-
|
| 1141 |
``` cpp
|
| 1142 |
template<class Y> shared_ptr(const shared_ptr<Y>& r, element_type* p) noexcept;
|
|
|
|
| 1143 |
```
|
| 1144 |
|
| 1145 |
*Effects:* Constructs a `shared_ptr` instance that stores `p` and shares
|
| 1146 |
-
ownership with `r`.
|
| 1147 |
|
| 1148 |
-
*
|
|
|
|
| 1149 |
|
| 1150 |
[*Note 1*: To avoid the possibility of a dangling pointer, the user of
|
| 1151 |
-
this constructor
|
| 1152 |
ownership group of `r` is destroyed. — *end note*]
|
| 1153 |
|
| 1154 |
[*Note 2*: This constructor allows creation of an empty `shared_ptr`
|
| 1155 |
instance with a non-null stored pointer. — *end note*]
|
| 1156 |
|
| 1157 |
``` cpp
|
| 1158 |
shared_ptr(const shared_ptr& r) noexcept;
|
| 1159 |
template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
|
| 1160 |
```
|
| 1161 |
|
| 1162 |
-
*
|
| 1163 |
-
resolution unless `Y*` is compatible with `T*`.
|
| 1164 |
|
| 1165 |
*Effects:* If `r` is empty, constructs an empty `shared_ptr` object;
|
| 1166 |
otherwise, constructs a `shared_ptr` object that shares ownership with
|
| 1167 |
`r`.
|
| 1168 |
|
| 1169 |
-
*
|
| 1170 |
|
| 1171 |
``` cpp
|
| 1172 |
shared_ptr(shared_ptr&& r) noexcept;
|
| 1173 |
template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
|
| 1174 |
```
|
| 1175 |
|
| 1176 |
-
*
|
| 1177 |
-
resolution unless `Y*` is compatible with `T*`.
|
| 1178 |
|
| 1179 |
*Effects:* Move constructs a `shared_ptr` instance from `r`.
|
| 1180 |
|
| 1181 |
-
*
|
| 1182 |
-
|
| 1183 |
|
| 1184 |
``` cpp
|
| 1185 |
template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
|
| 1186 |
```
|
| 1187 |
|
|
|
|
|
|
|
| 1188 |
*Effects:* Constructs a `shared_ptr` object that shares ownership with
|
| 1189 |
`r` and stores a copy of the pointer stored in `r`. If an exception is
|
| 1190 |
thrown, the constructor has no effect.
|
| 1191 |
|
| 1192 |
-
*
|
| 1193 |
|
| 1194 |
*Throws:* `bad_weak_ptr` when `r.expired()`.
|
| 1195 |
|
| 1196 |
-
*Remarks:* This constructor shall not participate in overload resolution
|
| 1197 |
-
unless `Y*` is compatible with `T*`.
|
| 1198 |
-
|
| 1199 |
``` cpp
|
| 1200 |
template<class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
|
| 1201 |
```
|
| 1202 |
|
| 1203 |
-
*
|
| 1204 |
-
|
| 1205 |
-
convertible to `element_type*`.
|
| 1206 |
|
| 1207 |
*Effects:* If `r.get() == nullptr`, equivalent to `shared_ptr()`.
|
| 1208 |
Otherwise, if `D` is not a reference type, equivalent to
|
| 1209 |
`shared_ptr(r.release(), r.get_deleter())`. Otherwise, equivalent to
|
| 1210 |
`shared_ptr(r.release(), ref(r.get_deleter()))`. If an exception is
|
| 1211 |
thrown, the constructor has no effect.
|
| 1212 |
|
| 1213 |
-
####
|
| 1214 |
|
| 1215 |
``` cpp
|
| 1216 |
~shared_ptr();
|
| 1217 |
```
|
| 1218 |
|
|
@@ -1228,22 +1132,22 @@ thrown, the constructor has no effect.
|
|
| 1228 |
instances that share ownership with `*this` by one, after `*this` has
|
| 1229 |
been destroyed all `shared_ptr` instances that shared ownership with
|
| 1230 |
`*this` will report a `use_count()` that is one less than its previous
|
| 1231 |
value. — *end note*]
|
| 1232 |
|
| 1233 |
-
####
|
| 1234 |
|
| 1235 |
``` cpp
|
| 1236 |
shared_ptr& operator=(const shared_ptr& r) noexcept;
|
| 1237 |
template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
|
| 1238 |
```
|
| 1239 |
|
| 1240 |
*Effects:* Equivalent to `shared_ptr(r).swap(*this)`.
|
| 1241 |
|
| 1242 |
*Returns:* `*this`.
|
| 1243 |
|
| 1244 |
-
[*Note
|
| 1245 |
|
| 1246 |
The use count updates caused by the temporary object construction and
|
| 1247 |
destruction are not observable side effects, so the implementation may
|
| 1248 |
meet the effects (and the implied guarantees) via different means,
|
| 1249 |
without creating a temporary. In particular, in the example:
|
|
@@ -1274,11 +1178,11 @@ template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
|
|
| 1274 |
|
| 1275 |
*Effects:* Equivalent to `shared_ptr(std::move(r)).swap(*this)`.
|
| 1276 |
|
| 1277 |
*Returns:* `*this`.
|
| 1278 |
|
| 1279 |
-
####
|
| 1280 |
|
| 1281 |
``` cpp
|
| 1282 |
void swap(shared_ptr& r) noexcept;
|
| 1283 |
```
|
| 1284 |
|
|
@@ -1306,11 +1210,11 @@ template<class Y, class D> void reset(Y* p, D d);
|
|
| 1306 |
template<class Y, class D, class A> void reset(Y* p, D d, A a);
|
| 1307 |
```
|
| 1308 |
|
| 1309 |
*Effects:* Equivalent to `shared_ptr(p, d, a).swap(*this)`.
|
| 1310 |
|
| 1311 |
-
####
|
| 1312 |
|
| 1313 |
``` cpp
|
| 1314 |
element_type* get() const noexcept;
|
| 1315 |
```
|
| 1316 |
|
|
@@ -1318,45 +1222,45 @@ element_type* get() const noexcept;
|
|
| 1318 |
|
| 1319 |
``` cpp
|
| 1320 |
T& operator*() const noexcept;
|
| 1321 |
```
|
| 1322 |
|
| 1323 |
-
*
|
| 1324 |
|
| 1325 |
*Returns:* `*get()`.
|
| 1326 |
|
| 1327 |
*Remarks:* When `T` is an array type or cv `void`, it is unspecified
|
| 1328 |
whether this member function is declared. If it is declared, it is
|
| 1329 |
unspecified what its return type is, except that the declaration
|
| 1330 |
-
(although not necessarily the definition) of the function shall be
|
| 1331 |
-
formed.
|
| 1332 |
|
| 1333 |
``` cpp
|
| 1334 |
T* operator->() const noexcept;
|
| 1335 |
```
|
| 1336 |
|
| 1337 |
-
*
|
| 1338 |
|
| 1339 |
*Returns:* `get()`.
|
| 1340 |
|
| 1341 |
*Remarks:* When `T` is an array type, it is unspecified whether this
|
| 1342 |
member function is declared. If it is declared, it is unspecified what
|
| 1343 |
its return type is, except that the declaration (although not
|
| 1344 |
-
necessarily the definition) of the function shall be well
|
| 1345 |
|
| 1346 |
``` cpp
|
| 1347 |
element_type& operator[](ptrdiff_t i) const;
|
| 1348 |
```
|
| 1349 |
|
| 1350 |
-
*
|
| 1351 |
|
| 1352 |
*Returns:* `get()[i]`.
|
| 1353 |
|
| 1354 |
*Remarks:* When `T` is not an array type, it is unspecified whether this
|
| 1355 |
member function is declared. If it is declared, it is unspecified what
|
| 1356 |
its return type is, except that the declaration (although not
|
| 1357 |
-
necessarily the definition) of the function shall be well
|
| 1358 |
|
| 1359 |
*Throws:* Nothing.
|
| 1360 |
|
| 1361 |
``` cpp
|
| 1362 |
long use_count() const noexcept;
|
|
@@ -1365,17 +1269,17 @@ long use_count() const noexcept;
|
|
| 1365 |
*Returns:* The number of `shared_ptr` objects, `*this` included, that
|
| 1366 |
share ownership with `*this`, or `0` when `*this` is empty.
|
| 1367 |
|
| 1368 |
*Synchronization:* None.
|
| 1369 |
|
| 1370 |
-
[*Note
|
| 1371 |
`use_count()`. — *end note*]
|
| 1372 |
|
| 1373 |
-
[*Note
|
| 1374 |
`use_count()`. — *end note*]
|
| 1375 |
|
| 1376 |
-
[*Note
|
| 1377 |
`use_count()`, the result should be treated as approximate. In
|
| 1378 |
particular, `use_count() == 1` does not imply that accesses through a
|
| 1379 |
previously destroyed `shared_ptr` have in any sense
|
| 1380 |
completed. — *end note*]
|
| 1381 |
|
|
@@ -1397,217 +1301,425 @@ template<class U> bool owner_before(const weak_ptr<U>& b) const noexcept;
|
|
| 1397 |
- under the equivalence relation defined by `owner_before`,
|
| 1398 |
`!a.owner_before(b) && !b.owner_before(a)`, two `shared_ptr` or
|
| 1399 |
`weak_ptr` instances are equivalent if and only if they share
|
| 1400 |
ownership or are both empty.
|
| 1401 |
|
| 1402 |
-
####
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1403 |
|
| 1404 |
``` cpp
|
| 1405 |
-
template<class T,
|
| 1406 |
-
shared_ptr<T> make_shared(
|
| 1407 |
-
template<class T, class A,
|
| 1408 |
-
shared_ptr<T> allocate_shared(const A& a,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1409 |
```
|
| 1410 |
|
| 1411 |
-
*
|
| 1412 |
-
|
| 1413 |
-
object of type `T`, shall be well formed. `A` shall be an
|
| 1414 |
-
allocator ([[allocator.requirements]]). The copy constructor and
|
| 1415 |
-
destructor of `A` shall not throw exceptions.
|
| 1416 |
|
| 1417 |
-
*Effects:* Allocates memory
|
| 1418 |
-
|
| 1419 |
-
|
| 1420 |
-
|
| 1421 |
-
|
|
|
|
|
|
|
| 1422 |
|
| 1423 |
*Returns:* A `shared_ptr` instance that stores and owns the address of
|
| 1424 |
-
the newly constructed object
|
| 1425 |
|
| 1426 |
-
*
|
|
|
|
| 1427 |
|
| 1428 |
-
*Throws:* `bad_alloc`, or an exception thrown from `
|
| 1429 |
-
the
|
| 1430 |
|
| 1431 |
-
*Remarks:*
|
| 1432 |
-
`shared_from_this` with the address of the newly constructed object of
|
| 1433 |
-
type `T`. Implementations should perform no more than one memory
|
| 1434 |
-
allocation.
|
| 1435 |
|
| 1436 |
-
|
|
|
|
| 1437 |
pointer. — *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1438 |
|
| 1439 |
-
|
| 1440 |
-
`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1441 |
reference counts. — *end note*]
|
| 1442 |
|
| 1443 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1444 |
|
| 1445 |
``` cpp
|
| 1446 |
template<class T, class U>
|
| 1447 |
bool operator==(const shared_ptr<T>& a, const shared_ptr<U>& b) noexcept;
|
| 1448 |
```
|
| 1449 |
|
| 1450 |
*Returns:* `a.get() == b.get()`.
|
| 1451 |
|
| 1452 |
-
``` cpp
|
| 1453 |
-
template<class T, class U>
|
| 1454 |
-
bool operator<(const shared_ptr<T>& a, const shared_ptr<U>& b) noexcept;
|
| 1455 |
-
```
|
| 1456 |
-
|
| 1457 |
-
*Returns:* `less<>()(a.get(), b.get())`.
|
| 1458 |
-
|
| 1459 |
-
[*Note 9*: Defining a comparison function allows `shared_ptr` objects
|
| 1460 |
-
to be used as keys in associative containers. — *end note*]
|
| 1461 |
-
|
| 1462 |
``` cpp
|
| 1463 |
template<class T>
|
| 1464 |
bool operator==(const shared_ptr<T>& a, nullptr_t) noexcept;
|
| 1465 |
-
template <class T>
|
| 1466 |
-
bool operator==(nullptr_t, const shared_ptr<T>& a) noexcept;
|
| 1467 |
```
|
| 1468 |
|
| 1469 |
*Returns:* `!a`.
|
| 1470 |
|
| 1471 |
``` cpp
|
| 1472 |
-
template
|
| 1473 |
-
|
| 1474 |
-
template <class T>
|
| 1475 |
-
bool operator!=(nullptr_t, const shared_ptr<T>& a) noexcept;
|
| 1476 |
-
```
|
| 1477 |
-
|
| 1478 |
-
*Returns:* `(bool)a`.
|
| 1479 |
-
|
| 1480 |
-
``` cpp
|
| 1481 |
-
template <class T>
|
| 1482 |
-
bool operator<(const shared_ptr<T>& a, nullptr_t) noexcept;
|
| 1483 |
-
template <class T>
|
| 1484 |
-
bool operator<(nullptr_t, const shared_ptr<T>& a) noexcept;
|
| 1485 |
-
```
|
| 1486 |
-
|
| 1487 |
-
*Returns:* The first function template returns
|
| 1488 |
-
`less<shared_ptr<T>::element_type*>()(a.get(), nullptr)`. The second
|
| 1489 |
-
function template returns
|
| 1490 |
-
`less<shared_ptr<T>::element_type*>()(nullptr, a.get())`.
|
| 1491 |
-
|
| 1492 |
-
``` cpp
|
| 1493 |
-
template <class T>
|
| 1494 |
-
bool operator>(const shared_ptr<T>& a, nullptr_t) noexcept;
|
| 1495 |
-
template <class T>
|
| 1496 |
-
bool operator>(nullptr_t, const shared_ptr<T>& a) noexcept;
|
| 1497 |
```
|
| 1498 |
|
| 1499 |
-
*Returns:*
|
| 1500 |
-
function template returns `a < nullptr`.
|
| 1501 |
-
|
| 1502 |
-
``` cpp
|
| 1503 |
-
template <class T>
|
| 1504 |
-
bool operator<=(const shared_ptr<T>& a, nullptr_t) noexcept;
|
| 1505 |
-
template <class T>
|
| 1506 |
-
bool operator<=(nullptr_t, const shared_ptr<T>& a) noexcept;
|
| 1507 |
-
```
|
| 1508 |
|
| 1509 |
-
*
|
| 1510 |
-
|
| 1511 |
|
| 1512 |
``` cpp
|
| 1513 |
template<class T>
|
| 1514 |
-
|
| 1515 |
-
template <class T>
|
| 1516 |
-
bool operator>=(nullptr_t, const shared_ptr<T>& a) noexcept;
|
| 1517 |
```
|
| 1518 |
|
| 1519 |
-
*Returns:*
|
| 1520 |
-
second function template returns `!(nullptr < a)`.
|
| 1521 |
|
| 1522 |
-
####
|
| 1523 |
|
| 1524 |
``` cpp
|
| 1525 |
template<class T>
|
| 1526 |
void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
|
| 1527 |
```
|
| 1528 |
|
| 1529 |
*Effects:* Equivalent to `a.swap(b)`.
|
| 1530 |
|
| 1531 |
-
####
|
| 1532 |
|
| 1533 |
``` cpp
|
| 1534 |
template<class T, class U>
|
| 1535 |
shared_ptr<T> static_pointer_cast(const shared_ptr<U>& r) noexcept;
|
|
|
|
|
|
|
| 1536 |
```
|
| 1537 |
|
| 1538 |
-
*
|
| 1539 |
-
formed.
|
| 1540 |
|
| 1541 |
*Returns:*
|
| 1542 |
|
| 1543 |
``` cpp
|
| 1544 |
-
shared_ptr<T>(
|
| 1545 |
```
|
| 1546 |
|
| 1547 |
-
|
|
|
|
|
|
|
|
|
|
| 1548 |
`shared_ptr<T>(static_cast<T*>(r.get()))` will eventually result in
|
| 1549 |
undefined behavior, attempting to delete the same object
|
| 1550 |
twice. — *end note*]
|
| 1551 |
|
| 1552 |
``` cpp
|
| 1553 |
template<class T, class U>
|
| 1554 |
shared_ptr<T> dynamic_pointer_cast(const shared_ptr<U>& r) noexcept;
|
|
|
|
|
|
|
| 1555 |
```
|
| 1556 |
|
| 1557 |
-
*
|
| 1558 |
-
formed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1559 |
|
| 1560 |
*Returns:*
|
| 1561 |
|
| 1562 |
- When `dynamic_cast<typename shared_ptr<T>::element_type*>(r.get())`
|
| 1563 |
-
returns a
|
|
|
|
| 1564 |
- Otherwise, `shared_ptr<T>()`.
|
| 1565 |
|
| 1566 |
-
[*Note
|
| 1567 |
`shared_ptr<T>(dynamic_cast<T*>(r.get()))` will eventually result in
|
| 1568 |
undefined behavior, attempting to delete the same object
|
| 1569 |
twice. — *end note*]
|
| 1570 |
|
| 1571 |
``` cpp
|
| 1572 |
template<class T, class U>
|
| 1573 |
shared_ptr<T> const_pointer_cast(const shared_ptr<U>& r) noexcept;
|
|
|
|
|
|
|
| 1574 |
```
|
| 1575 |
|
| 1576 |
-
*
|
| 1577 |
|
| 1578 |
*Returns:*
|
| 1579 |
|
| 1580 |
``` cpp
|
| 1581 |
-
shared_ptr<T>(
|
| 1582 |
```
|
| 1583 |
|
| 1584 |
-
|
|
|
|
|
|
|
|
|
|
| 1585 |
`shared_ptr<T>(const_cast<T*>(r.get()))` will eventually result in
|
| 1586 |
undefined behavior, attempting to delete the same object
|
| 1587 |
twice. — *end note*]
|
| 1588 |
|
| 1589 |
``` cpp
|
| 1590 |
template<class T, class U>
|
| 1591 |
shared_ptr<T> reinterpret_pointer_cast(const shared_ptr<U>& r) noexcept;
|
|
|
|
|
|
|
| 1592 |
```
|
| 1593 |
|
| 1594 |
-
*
|
| 1595 |
-
formed.
|
| 1596 |
|
| 1597 |
*Returns:*
|
| 1598 |
|
| 1599 |
``` cpp
|
| 1600 |
-
shared_ptr<T>(
|
| 1601 |
```
|
| 1602 |
|
| 1603 |
-
|
|
|
|
|
|
|
|
|
|
| 1604 |
`shared_ptr<T>(reinterpret_cast<T*>(r.get()))` will eventually result in
|
| 1605 |
undefined behavior, attempting to delete the same object
|
| 1606 |
twice. — *end note*]
|
| 1607 |
|
| 1608 |
-
####
|
| 1609 |
|
| 1610 |
``` cpp
|
| 1611 |
template<class D, class T>
|
| 1612 |
D* get_deleter(const shared_ptr<T>& p) noexcept;
|
| 1613 |
```
|
|
@@ -1615,139 +1727,147 @@ template<class D, class T>
|
|
| 1615 |
*Returns:* If `p` owns a deleter `d` of type cv-unqualified `D`, returns
|
| 1616 |
`addressof(d)`; otherwise returns `nullptr`. The returned pointer
|
| 1617 |
remains valid as long as there exists a `shared_ptr` instance that owns
|
| 1618 |
`d`.
|
| 1619 |
|
| 1620 |
-
[*Note
|
| 1621 |
than that. This can happen if the implementation doesn’t destroy the
|
| 1622 |
deleter until all `weak_ptr` instances that share ownership with `p`
|
| 1623 |
have been destroyed. — *end note*]
|
| 1624 |
|
| 1625 |
-
####
|
| 1626 |
|
| 1627 |
``` cpp
|
| 1628 |
template<class E, class T, class Y>
|
| 1629 |
basic_ostream<E, T>& operator<<(basic_ostream<E, T>& os, const shared_ptr<Y>& p);
|
| 1630 |
```
|
| 1631 |
|
| 1632 |
*Effects:* As if by: `os << p.get();`
|
| 1633 |
|
| 1634 |
*Returns:* `os`.
|
| 1635 |
|
| 1636 |
-
###
|
| 1637 |
|
| 1638 |
The `weak_ptr` class template stores a weak reference to an object that
|
| 1639 |
is already managed by a `shared_ptr`. To access the object, a `weak_ptr`
|
| 1640 |
can be converted to a `shared_ptr` using the member function `lock`.
|
| 1641 |
|
| 1642 |
``` cpp
|
| 1643 |
namespace std {
|
| 1644 |
template<class T> class weak_ptr {
|
| 1645 |
public:
|
| 1646 |
-
using element_type = T;
|
| 1647 |
|
| 1648 |
// [util.smartptr.weak.const], constructors
|
| 1649 |
constexpr weak_ptr() noexcept;
|
| 1650 |
-
template<class Y>
|
|
|
|
| 1651 |
weak_ptr(const weak_ptr& r) noexcept;
|
| 1652 |
-
template<class Y>
|
|
|
|
| 1653 |
weak_ptr(weak_ptr&& r) noexcept;
|
| 1654 |
-
template<class Y>
|
|
|
|
| 1655 |
|
| 1656 |
// [util.smartptr.weak.dest], destructor
|
| 1657 |
~weak_ptr();
|
| 1658 |
|
| 1659 |
// [util.smartptr.weak.assign], assignment
|
| 1660 |
weak_ptr& operator=(const weak_ptr& r) noexcept;
|
| 1661 |
-
template<class Y>
|
| 1662 |
-
|
|
|
|
|
|
|
| 1663 |
weak_ptr& operator=(weak_ptr&& r) noexcept;
|
| 1664 |
-
template<class Y>
|
|
|
|
| 1665 |
|
| 1666 |
// [util.smartptr.weak.mod], modifiers
|
| 1667 |
void swap(weak_ptr& r) noexcept;
|
| 1668 |
void reset() noexcept;
|
| 1669 |
|
| 1670 |
// [util.smartptr.weak.obs], observers
|
| 1671 |
long use_count() const noexcept;
|
| 1672 |
bool expired() const noexcept;
|
| 1673 |
shared_ptr<T> lock() const noexcept;
|
| 1674 |
-
template<class U>
|
| 1675 |
-
|
|
|
|
|
|
|
| 1676 |
};
|
| 1677 |
|
| 1678 |
-
template<class T>
|
| 1679 |
-
|
| 1680 |
|
| 1681 |
// [util.smartptr.weak.spec], specialized algorithms
|
| 1682 |
-
template<class T>
|
|
|
|
| 1683 |
}
|
| 1684 |
```
|
| 1685 |
|
| 1686 |
-
Specializations of `weak_ptr` shall be
|
| 1687 |
-
|
| 1688 |
template parameter `T` of `weak_ptr` may be an incomplete type.
|
| 1689 |
|
| 1690 |
-
####
|
| 1691 |
|
| 1692 |
``` cpp
|
| 1693 |
constexpr weak_ptr() noexcept;
|
| 1694 |
```
|
| 1695 |
|
| 1696 |
*Effects:* Constructs an empty `weak_ptr` object.
|
| 1697 |
|
| 1698 |
-
*
|
| 1699 |
|
| 1700 |
``` cpp
|
| 1701 |
weak_ptr(const weak_ptr& r) noexcept;
|
| 1702 |
template<class Y> weak_ptr(const weak_ptr<Y>& r) noexcept;
|
| 1703 |
template<class Y> weak_ptr(const shared_ptr<Y>& r) noexcept;
|
| 1704 |
```
|
| 1705 |
|
| 1706 |
-
*
|
| 1707 |
-
|
| 1708 |
|
| 1709 |
*Effects:* If `r` is empty, constructs an empty `weak_ptr` object;
|
| 1710 |
otherwise, constructs a `weak_ptr` object that shares ownership with `r`
|
| 1711 |
and stores a copy of the pointer stored in `r`.
|
| 1712 |
|
| 1713 |
-
*
|
| 1714 |
|
| 1715 |
``` cpp
|
| 1716 |
weak_ptr(weak_ptr&& r) noexcept;
|
| 1717 |
template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept;
|
| 1718 |
```
|
| 1719 |
|
| 1720 |
-
*
|
| 1721 |
-
resolution unless `Y*` is compatible with `T*`.
|
| 1722 |
|
| 1723 |
*Effects:* Move constructs a `weak_ptr` instance from `r`.
|
| 1724 |
|
| 1725 |
-
*
|
| 1726 |
-
|
| 1727 |
|
| 1728 |
-
####
|
| 1729 |
|
| 1730 |
``` cpp
|
| 1731 |
~weak_ptr();
|
| 1732 |
```
|
| 1733 |
|
| 1734 |
*Effects:* Destroys this `weak_ptr` object but has no effect on the
|
| 1735 |
object its stored pointer points to.
|
| 1736 |
|
| 1737 |
-
####
|
| 1738 |
|
| 1739 |
``` cpp
|
| 1740 |
weak_ptr& operator=(const weak_ptr& r) noexcept;
|
| 1741 |
template<class Y> weak_ptr& operator=(const weak_ptr<Y>& r) noexcept;
|
| 1742 |
template<class Y> weak_ptr& operator=(const shared_ptr<Y>& r) noexcept;
|
| 1743 |
```
|
| 1744 |
|
| 1745 |
*Effects:* Equivalent to `weak_ptr(r).swap(*this)`.
|
| 1746 |
|
| 1747 |
*Remarks:* The implementation may meet the effects (and the implied
|
| 1748 |
-
guarantees) via different means, without creating a temporary.
|
| 1749 |
|
| 1750 |
*Returns:* `*this`.
|
| 1751 |
|
| 1752 |
``` cpp
|
| 1753 |
weak_ptr& operator=(weak_ptr&& r) noexcept;
|
|
@@ -1756,11 +1876,11 @@ template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept;
|
|
| 1756 |
|
| 1757 |
*Effects:* Equivalent to `weak_ptr(std::move(r)).swap(*this)`.
|
| 1758 |
|
| 1759 |
*Returns:* `*this`.
|
| 1760 |
|
| 1761 |
-
####
|
| 1762 |
|
| 1763 |
``` cpp
|
| 1764 |
void swap(weak_ptr& r) noexcept;
|
| 1765 |
```
|
| 1766 |
|
|
@@ -1770,11 +1890,11 @@ void swap(weak_ptr& r) noexcept;
|
|
| 1770 |
void reset() noexcept;
|
| 1771 |
```
|
| 1772 |
|
| 1773 |
*Effects:* Equivalent to `weak_ptr().swap(*this)`.
|
| 1774 |
|
| 1775 |
-
####
|
| 1776 |
|
| 1777 |
``` cpp
|
| 1778 |
long use_count() const noexcept;
|
| 1779 |
```
|
| 1780 |
|
|
@@ -1793,12 +1913,12 @@ shared_ptr<T> lock() const noexcept;
|
|
| 1793 |
|
| 1794 |
*Returns:* `expired() ? shared_ptr<T>() : shared_ptr<T>(*this)`,
|
| 1795 |
executed atomically.
|
| 1796 |
|
| 1797 |
``` cpp
|
| 1798 |
-
template<class U> bool owner_before(const shared_ptr<U>& b) const;
|
| 1799 |
-
template<class U> bool owner_before(const weak_ptr<U>& b) const;
|
| 1800 |
```
|
| 1801 |
|
| 1802 |
*Returns:* An unspecified value such that
|
| 1803 |
|
| 1804 |
- `x.owner_before(y)` defines a strict weak ordering as defined
|
|
@@ -1806,20 +1926,20 @@ template<class U> bool owner_before(const weak_ptr<U>& b) const;
|
|
| 1806 |
- under the equivalence relation defined by `owner_before`,
|
| 1807 |
`!a.owner_before(b) && !b.owner_before(a)`, two `shared_ptr` or
|
| 1808 |
`weak_ptr` instances are equivalent if and only if they share
|
| 1809 |
ownership or are both empty.
|
| 1810 |
|
| 1811 |
-
####
|
| 1812 |
|
| 1813 |
``` cpp
|
| 1814 |
template<class T>
|
| 1815 |
void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
|
| 1816 |
```
|
| 1817 |
|
| 1818 |
*Effects:* Equivalent to `a.swap(b)`.
|
| 1819 |
|
| 1820 |
-
###
|
| 1821 |
|
| 1822 |
The class template `owner_less` allows ownership-based mixed comparisons
|
| 1823 |
of shared and weak pointers.
|
| 1824 |
|
| 1825 |
``` cpp
|
|
@@ -1851,11 +1971,11 @@ namespace std {
|
|
| 1851 |
using is_transparent = unspecified;
|
| 1852 |
};
|
| 1853 |
}
|
| 1854 |
```
|
| 1855 |
|
| 1856 |
-
`operator()(x, y)`
|
| 1857 |
|
| 1858 |
[*Note 1*:
|
| 1859 |
|
| 1860 |
Note that
|
| 1861 |
|
|
@@ -1866,11 +1986,11 @@ Note that
|
|
| 1866 |
`weak_ptr` instances are equivalent if and only if they share
|
| 1867 |
ownership or are both empty.
|
| 1868 |
|
| 1869 |
— *end note*]
|
| 1870 |
|
| 1871 |
-
###
|
| 1872 |
|
| 1873 |
A class `T` can inherit from `enable_shared_from_this<T>` to inherit the
|
| 1874 |
`shared_from_this` member functions that obtain a `shared_ptr` instance
|
| 1875 |
pointing to `*this`.
|
| 1876 |
|
|
@@ -1895,15 +2015,17 @@ namespace std {
|
|
| 1895 |
protected:
|
| 1896 |
constexpr enable_shared_from_this() noexcept;
|
| 1897 |
enable_shared_from_this(const enable_shared_from_this&) noexcept;
|
| 1898 |
enable_shared_from_this& operator=(const enable_shared_from_this&) noexcept;
|
| 1899 |
~enable_shared_from_this();
|
|
|
|
| 1900 |
public:
|
| 1901 |
shared_ptr<T> shared_from_this();
|
| 1902 |
shared_ptr<T const> shared_from_this() const;
|
| 1903 |
weak_ptr<T> weak_from_this() noexcept;
|
| 1904 |
weak_ptr<T const> weak_from_this() const noexcept;
|
|
|
|
| 1905 |
private:
|
| 1906 |
mutable weak_ptr<T> weak_this; // exposition only
|
| 1907 |
};
|
| 1908 |
}
|
| 1909 |
```
|
|
@@ -1938,176 +2060,25 @@ weak_ptr<T> weak_from_this() noexcept;
|
|
| 1938 |
weak_ptr<T const> weak_from_this() const noexcept;
|
| 1939 |
```
|
| 1940 |
|
| 1941 |
*Returns:* `weak_this`.
|
| 1942 |
|
| 1943 |
-
###
|
| 1944 |
-
|
| 1945 |
-
Concurrent access to a `shared_ptr` object from multiple threads does
|
| 1946 |
-
not introduce a data race if the access is done exclusively via the
|
| 1947 |
-
functions in this section and the instance is passed as their first
|
| 1948 |
-
argument.
|
| 1949 |
-
|
| 1950 |
-
The meaning of the arguments of type `memory_order` is explained in
|
| 1951 |
-
[[atomics.order]].
|
| 1952 |
-
|
| 1953 |
-
``` cpp
|
| 1954 |
-
template<class T>
|
| 1955 |
-
bool atomic_is_lock_free(const shared_ptr<T>* p);
|
| 1956 |
-
```
|
| 1957 |
-
|
| 1958 |
-
*Requires:* `p` shall not be null.
|
| 1959 |
-
|
| 1960 |
-
*Returns:* `true` if atomic access to `*p` is lock-free, `false`
|
| 1961 |
-
otherwise.
|
| 1962 |
-
|
| 1963 |
-
*Throws:* Nothing.
|
| 1964 |
-
|
| 1965 |
-
``` cpp
|
| 1966 |
-
template<class T>
|
| 1967 |
-
shared_ptr<T> atomic_load(const shared_ptr<T>* p);
|
| 1968 |
-
```
|
| 1969 |
-
|
| 1970 |
-
*Requires:* `p` shall not be null.
|
| 1971 |
-
|
| 1972 |
-
*Returns:* `atomic_load_explicit(p, memory_order_seq_cst)`.
|
| 1973 |
-
|
| 1974 |
-
*Throws:* Nothing.
|
| 1975 |
-
|
| 1976 |
-
``` cpp
|
| 1977 |
-
template<class T>
|
| 1978 |
-
shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
|
| 1979 |
-
```
|
| 1980 |
-
|
| 1981 |
-
*Requires:* `p` shall not be null.
|
| 1982 |
-
|
| 1983 |
-
*Requires:* `mo` shall not be `memory_order_release` or
|
| 1984 |
-
`memory_order_acq_rel`.
|
| 1985 |
-
|
| 1986 |
-
*Returns:* `*p`.
|
| 1987 |
-
|
| 1988 |
-
*Throws:* Nothing.
|
| 1989 |
-
|
| 1990 |
-
``` cpp
|
| 1991 |
-
template<class T>
|
| 1992 |
-
void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
|
| 1993 |
-
```
|
| 1994 |
-
|
| 1995 |
-
*Requires:* `p` shall not be null.
|
| 1996 |
-
|
| 1997 |
-
*Effects:* As if by `atomic_store_explicit(p, r, memory_order_seq_cst)`.
|
| 1998 |
-
|
| 1999 |
-
*Throws:* Nothing.
|
| 2000 |
-
|
| 2001 |
-
``` cpp
|
| 2002 |
-
template<class T>
|
| 2003 |
-
void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
|
| 2004 |
-
```
|
| 2005 |
-
|
| 2006 |
-
*Requires:* `p` shall not be null.
|
| 2007 |
-
|
| 2008 |
-
*Requires:* `mo` shall not be `memory_order_acquire` or
|
| 2009 |
-
`memory_order_acq_rel`.
|
| 2010 |
-
|
| 2011 |
-
*Effects:* As if by `p->swap(r)`.
|
| 2012 |
-
|
| 2013 |
-
*Throws:* Nothing.
|
| 2014 |
-
|
| 2015 |
-
``` cpp
|
| 2016 |
-
template<class T>
|
| 2017 |
-
shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
|
| 2018 |
-
```
|
| 2019 |
-
|
| 2020 |
-
*Requires:* `p` shall not be null.
|
| 2021 |
-
|
| 2022 |
-
*Returns:* `atomic_exchange_explicit(p, r, memory_order_seq_cst)`.
|
| 2023 |
-
|
| 2024 |
-
*Throws:* Nothing.
|
| 2025 |
-
|
| 2026 |
-
``` cpp
|
| 2027 |
-
template<class T>
|
| 2028 |
-
shared_ptr<T> atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
|
| 2029 |
-
```
|
| 2030 |
-
|
| 2031 |
-
*Requires:* `p` shall not be null.
|
| 2032 |
-
|
| 2033 |
-
*Effects:* As if by `p->swap(r)`.
|
| 2034 |
-
|
| 2035 |
-
*Returns:* The previous value of `*p`.
|
| 2036 |
-
|
| 2037 |
-
*Throws:* Nothing.
|
| 2038 |
-
|
| 2039 |
-
``` cpp
|
| 2040 |
-
template<class T>
|
| 2041 |
-
bool atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
|
| 2042 |
-
```
|
| 2043 |
-
|
| 2044 |
-
*Requires:* `p` shall not be null and `v` shall not be null.
|
| 2045 |
-
|
| 2046 |
-
*Returns:*
|
| 2047 |
-
|
| 2048 |
-
``` cpp
|
| 2049 |
-
atomic_compare_exchange_weak_explicit(p, v, w, memory_order_seq_cst, memory_order_seq_cst)
|
| 2050 |
-
```
|
| 2051 |
-
|
| 2052 |
-
*Throws:* Nothing.
|
| 2053 |
-
|
| 2054 |
-
``` cpp
|
| 2055 |
-
template<class T>
|
| 2056 |
-
bool atomic_compare_exchange_strong(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
|
| 2057 |
-
```
|
| 2058 |
-
|
| 2059 |
-
*Returns:*
|
| 2060 |
-
|
| 2061 |
-
``` cpp
|
| 2062 |
-
atomic_compare_exchange_strong_explicit(p, v, w, memory_order_seq_cst, memory_order_seq_cst)
|
| 2063 |
-
```
|
| 2064 |
-
|
| 2065 |
-
``` cpp
|
| 2066 |
-
template<class T>
|
| 2067 |
-
bool atomic_compare_exchange_weak_explicit(
|
| 2068 |
-
shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w,
|
| 2069 |
-
memory_order success, memory_order failure);
|
| 2070 |
-
template<class T>
|
| 2071 |
-
bool atomic_compare_exchange_strong_explicit(
|
| 2072 |
-
shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w,
|
| 2073 |
-
memory_order success, memory_order failure);
|
| 2074 |
-
```
|
| 2075 |
-
|
| 2076 |
-
*Requires:* `p` shall not be null and `v` shall not be null. The
|
| 2077 |
-
`failure` argument shall not be `memory_order_release` nor
|
| 2078 |
-
`memory_order_acq_rel`.
|
| 2079 |
-
|
| 2080 |
-
*Effects:* If `*p` is equivalent to `*v`, assigns `w` to `*p` and has
|
| 2081 |
-
synchronization semantics corresponding to the value of `success`,
|
| 2082 |
-
otherwise assigns `*p` to `*v` and has synchronization semantics
|
| 2083 |
-
corresponding to the value of `failure`.
|
| 2084 |
-
|
| 2085 |
-
*Returns:* `true` if `*p` was equivalent to `*v`, `false` otherwise.
|
| 2086 |
-
|
| 2087 |
-
*Throws:* Nothing.
|
| 2088 |
-
|
| 2089 |
-
*Remarks:* Two `shared_ptr` objects are equivalent if they store the
|
| 2090 |
-
same pointer value and share ownership. The weak form may fail
|
| 2091 |
-
spuriously. See [[atomics.types.operations]].
|
| 2092 |
-
|
| 2093 |
-
#### Smart pointer hash support <a id="util.smartptr.hash">[[util.smartptr.hash]]</a>
|
| 2094 |
|
| 2095 |
``` cpp
|
| 2096 |
template<class T, class D> struct hash<unique_ptr<T, D>>;
|
| 2097 |
```
|
| 2098 |
|
| 2099 |
Letting `UP` be `unique_ptr<T,D>`, the specialization `hash<UP>` is
|
| 2100 |
-
enabled
|
| 2101 |
enabled. When enabled, for an object `p` of type `UP`, `hash<UP>()(p)`
|
| 2102 |
-
|
| 2103 |
-
|
| 2104 |
-
guaranteed to be `noexcept`.
|
| 2105 |
|
| 2106 |
``` cpp
|
| 2107 |
template<class T> struct hash<shared_ptr<T>>;
|
| 2108 |
```
|
| 2109 |
|
| 2110 |
For an object `p` of type `shared_ptr<T>`, `hash<shared_ptr<T>>()(p)`
|
| 2111 |
-
|
| 2112 |
`hash<typename shared_ptr<T>::element_type*>()(p.get())`.
|
| 2113 |
|
|
|
|
| 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
|
| 9 |
+
scope [[stmt.dcl]]). In this context, *u* is said to *own* `p`.
|
| 10 |
|
| 11 |
The mechanism by which *u* disposes of *p* is known as *p*’s associated
|
| 12 |
*deleter*, a function object whose correct invocation results in *p*’s
|
| 13 |
appropriate disposition (typically its deletion).
|
| 14 |
|
| 15 |
Let the notation *u.p* denote the pointer stored by *u*, and let *u.d*
|
| 16 |
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 this subclause has the strict ownership semantics,
|
| 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.
|
| 28 |
|
| 29 |
+
[*Note 1*: The uses of `unique_ptr` include providing exception safety
|
| 30 |
for dynamically allocated memory, passing ownership of dynamically
|
| 31 |
allocated memory to a function, and returning dynamically allocated
|
| 32 |
memory from a function. — *end note*]
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
#### Default deleters <a id="unique.ptr.dltr">[[unique.ptr.dltr]]</a>
|
| 35 |
|
| 36 |
##### In general <a id="unique.ptr.dltr.general">[[unique.ptr.dltr.general]]</a>
|
| 37 |
|
| 38 |
The class template `default_delete` serves as the default deleter
|
|
|
|
| 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`.
|
| 72 |
|
|
|
|
|
|
|
| 73 |
##### `default_delete<T[]>` <a id="unique.ptr.dltr.dflt1">[[unique.ptr.dltr.dflt1]]</a>
|
| 74 |
|
| 75 |
``` cpp
|
| 76 |
namespace std {
|
| 77 |
template<class T> struct default_delete<T[]> {
|
|
|
|
| 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 {
|
|
|
|
| 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;
|
|
|
|
| 149 |
};
|
| 150 |
}
|
| 151 |
```
|
| 152 |
|
| 153 |
The default type for the template parameter `D` is `default_delete`. A
|
| 154 |
+
client-supplied template argument `D` shall be a function object type
|
| 155 |
+
[[function.objects]], lvalue reference to function, or lvalue reference
|
| 156 |
to function object type for which, given a value `d` of type `D` and a
|
| 157 |
value `ptr` of type `unique_ptr<T, D>::pointer`, the expression `d(ptr)`
|
| 158 |
is valid and has the effect of disposing of the pointer as appropriate
|
| 159 |
for that deleter.
|
| 160 |
|
| 161 |
+
If the deleter’s type `D` is not a reference type, `D` shall meet the
|
| 162 |
+
*Cpp17Destructible* requirements ([[cpp17.destructible]]).
|
| 163 |
|
| 164 |
If the *qualified-id* `remove_reference_t<D>::pointer` is valid and
|
| 165 |
+
denotes a type [[temp.deduct]], then `unique_ptr<T,
|
| 166 |
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` ([[cpp17.allocator]]) and
|
| 173 |
+
letting `A` be a synonym for `allocator_traits<X>`, the types
|
| 174 |
`A::pointer`, `A::const_pointer`, `A::void_pointer`, and
|
| 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
|
| 213 |
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.
|
| 233 |
|
| 234 |
*Effects:* Constructs a `unique_ptr` object which owns `p`, initializing
|
| 235 |
the stored pointer with `p` and initializing the deleter from
|
| 236 |
`std::forward<decltype(d)>(d)`.
|
| 237 |
|
| 238 |
+
*Ensures:* `get() == p`. `get_deleter()` returns a reference to the
|
| 239 |
+
stored deleter. If `D` is a reference type then `get_deleter()` returns
|
| 240 |
+
a reference to the lvalue `d`.
|
| 241 |
|
| 242 |
+
*Remarks:* If `D` is a reference type, the second constructor is defined
|
| 243 |
+
as deleted.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
|
| 245 |
[*Example 1*:
|
| 246 |
|
| 247 |
``` cpp
|
| 248 |
D d;
|
| 249 |
+
unique_ptr<int, D> p1(new int, D()); // D must be Cpp17MoveConstructible
|
| 250 |
+
unique_ptr<int, D> p2(new int, d); // D must be Cpp17CopyConstructible
|
| 251 |
unique_ptr<int, D&> p3(new int, d); // p3 holds a reference to d
|
| 252 |
unique_ptr<int, const D&> p4(new int, D()); // error: rvalue deleter object combined
|
| 253 |
// with reference deleter type
|
| 254 |
```
|
| 255 |
|
|
|
|
| 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
|
| 265 |
+
*Cpp17MoveConstructible* requirements ([[cpp17.moveconstructible]]).
|
| 266 |
+
Construction of the deleter from an rvalue of type `D` does not throw an
|
| 267 |
+
exception.
|
| 268 |
|
| 269 |
+
*Effects:* Constructs a `unique_ptr` from `u`. If `D` is a reference
|
| 270 |
+
type, this deleter is copy constructed from `u`’s deleter; otherwise,
|
| 271 |
+
this deleter is move constructed from `u`’s deleter.
|
| 272 |
+
|
| 273 |
+
[*Note 1*: The construction of the deleter can be implemented with
|
| 274 |
`std::forward<D>`. — *end note*]
|
| 275 |
|
| 276 |
+
*Ensures:* `get()` yields the value `u.get()` yielded before the
|
| 277 |
+
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`,
|
| 289 |
+
- `U` is not an array type, and
|
| 290 |
+
- either `D` is a reference type and `E` is the same type as `D`, or `D`
|
| 291 |
+
is not a reference type and `E` is implicitly convertible to `D`.
|
| 292 |
+
|
| 293 |
+
*Preconditions:* If `E` is not a reference type, construction of the
|
| 294 |
+
deleter from an rvalue of type `E` is well-formed and does not throw an
|
| 295 |
exception. Otherwise, `E` is a reference type and construction of the
|
| 296 |
+
deleter from an lvalue of type `E` is well-formed and does not throw an
|
| 297 |
+
exception.
|
| 298 |
|
| 299 |
+
*Effects:* Constructs a `unique_ptr` from `u`. If `E` is a reference
|
| 300 |
+
type, this deleter is copy constructed from `u`’s deleter; otherwise,
|
| 301 |
+
this deleter is move constructed from `u`’s deleter.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
|
| 303 |
[*Note 2*: The deleter constructor can be implemented with
|
| 304 |
`std::forward<E>`. — *end note*]
|
| 305 |
|
| 306 |
+
*Ensures:* `get()` yields the value `u.get()` yielded before the
|
| 307 |
+
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 |
+
*Preconditions:* The expression `get_deleter()(get())` is well-formed,
|
| 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 |
*Effects:* If `get() == nullptr` there are no effects. Otherwise
|
| 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
|
| 334 |
+
*Cpp17MoveAssignable* requirements ([[cpp17.moveassignable]]) and
|
| 335 |
+
assignment of the deleter from an rvalue of type `D` does not throw an
|
| 336 |
exception. Otherwise, `D` is a reference type; `remove_reference_t<D>`
|
| 337 |
+
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`,
|
| 354 |
and
|
| 355 |
- `U` is not an array type, and
|
| 356 |
- `is_assignable_v<D&, E&&>` is `true`.
|
| 357 |
|
| 358 |
+
*Preconditions:* If `E` is not a reference type, assignment of the
|
| 359 |
+
deleter from an rvalue of type `E` is well-formed and does not throw an
|
| 360 |
+
exception. Otherwise, `E` is a reference type and assignment of the
|
| 361 |
+
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`.
|
| 378 |
|
| 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*]
|
|
|
|
| 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 |
+
*Preconditions:* The expression `get_deleter()(get())` is well-formed,
|
| 436 |
+
has well-defined behavior, and does not throw exceptions.
|
| 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()` may destroy `*this`. — *end note*]
|
| 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>
|
|
|
|
| 523 |
Descriptions are provided below only for members that differ from the
|
| 524 |
primary template.
|
| 525 |
|
| 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 |
+
|
| 537 |
+
*Constraints:*
|
| 538 |
|
| 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 |
|
|
|
|
| 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 |
+
|
| 551 |
+
*Constraints:*
|
| 552 |
|
| 553 |
- `U` is the same type as `pointer`,
|
| 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>`:
|
| 565 |
|
| 566 |
- `U` is an array type, and
|
| 567 |
- `pointer` is the same type as `element_type*`, and
|
| 568 |
- `UP::pointer` is the same type as `UP::element_type*`, and
|
| 569 |
- `UP::element_type(*)[]` is convertible to `element_type(*)[]`, and
|
| 570 |
- either `D` is a reference type and `E` is the same type as `D`, or `D`
|
| 571 |
is not a reference type and `E` is implicitly convertible to `D`.
|
| 572 |
|
| 573 |
+
[*Note 1*: This replaces the *Constraints:* specification of the
|
| 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>`:
|
| 585 |
|
| 586 |
- `U` is an array type, and
|
| 587 |
- `pointer` is the same type as `element_type*`, and
|
| 588 |
- `UP::pointer` is the same type as `UP::element_type*`, and
|
| 589 |
- `UP::element_type(*)[]` is convertible to `element_type(*)[]`, and
|
| 590 |
- `is_assignable_v<D&, E&&>` is `true`.
|
| 591 |
|
| 592 |
+
[*Note 2*: This replaces the *Constraints:* specification of the
|
| 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 |
|
|
|
|
| 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 |
+
|
| 621 |
+
*Constraints:*
|
| 622 |
|
| 623 |
- `U` is the same type as `pointer`, or
|
| 624 |
- `pointer` is the same type as `element_type*`, `U` is a pointer type
|
| 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]())`.
|
| 644 |
|
| 645 |
``` cpp
|
| 646 |
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])`.
|
| 666 |
+
|
| 667 |
+
``` cpp
|
| 668 |
+
template<class T, class... Args> unspecified make_unique_for_overwrite(Args&&...) = delete;
|
| 669 |
+
```
|
| 670 |
+
|
| 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
|
| 691 |
template<class T1, class D1, class T2, class D2>
|
| 692 |
bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
|
| 693 |
```
|
| 694 |
|
| 695 |
+
Let `CT` denote
|
| 696 |
|
| 697 |
``` cpp
|
| 698 |
common_type_t<typename unique_ptr<T1, D1>::pointer,
|
| 699 |
typename unique_ptr<T2, D2>::pointer>
|
| 700 |
```
|
| 701 |
|
| 702 |
+
*Mandates:*
|
|
|
|
|
|
|
| 703 |
|
| 704 |
+
- `unique_ptr<T1, D1>::pointer` is implicitly convertible to `CT` and
|
| 705 |
+
- `unique_ptr<T2, D2>::pointer` is implicitly convertible to `CT`.
|
| 706 |
|
| 707 |
+
*Preconditions:* The specialization `less<CT>` is a function object
|
| 708 |
+
type [[function.objects]] that induces a strict weak
|
| 709 |
+
ordering [[alg.sorting]] on the pointer values.
|
| 710 |
|
| 711 |
+
*Returns:* `less<CT>()(x.get(), y.get())`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 712 |
|
| 713 |
``` cpp
|
| 714 |
template<class T1, class D1, class T2, class D2>
|
| 715 |
bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
|
| 716 |
```
|
| 717 |
|
| 718 |
*Returns:* `y < x`.
|
| 719 |
|
| 720 |
+
``` cpp
|
| 721 |
+
template<class T1, class D1, class T2, class D2>
|
| 722 |
+
bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
|
| 723 |
+
```
|
| 724 |
+
|
| 725 |
+
*Returns:* `!(y < x)`.
|
| 726 |
+
|
| 727 |
``` cpp
|
| 728 |
template<class T1, class D1, class T2, class D2>
|
| 729 |
bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
|
| 730 |
```
|
| 731 |
|
| 732 |
*Returns:* `!(x < y)`.
|
| 733 |
|
| 734 |
+
``` cpp
|
| 735 |
+
template<class T1, class D1, class T2, class D2>
|
| 736 |
+
requires three_way_comparable_with<typename unique_ptr<T1, D1>::pointer,
|
| 737 |
+
typename unique_ptr<T2, D2>::pointer>
|
| 738 |
+
compare_three_way_result_t<typename unique_ptr<T1, D1>::pointer,
|
| 739 |
+
typename unique_ptr<T2, D2>::pointer>
|
| 740 |
+
operator<=>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
|
| 741 |
+
```
|
| 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.
|
| 762 |
|
| 763 |
*Returns:* The first function template returns
|
| 764 |
+
|
| 765 |
+
``` cpp
|
| 766 |
+
less<unique_ptr<T, D>::pointer>()(x.get(), nullptr)
|
| 767 |
+
```
|
| 768 |
+
|
| 769 |
+
The second function template returns
|
| 770 |
+
|
| 771 |
+
``` cpp
|
| 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>
|
|
|
|
| 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_with<typename unique_ptr<T, D>::pointer, nullptr_t>
|
| 808 |
+
compare_three_way_result_t<typename unique_ptr<T, D>::pointer, nullptr_t>
|
| 809 |
+
operator<=>(const unique_ptr<T, D>& x, nullptr_t);
|
| 810 |
+
```
|
| 811 |
|
| 812 |
+
*Returns:* `compare_three_way()(x.get(), nullptr)`.
|
| 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>
|
| 818 |
+
basic_ostream<E, T>& operator<<(basic_ostream<E, T>& os, const unique_ptr<Y, D>& p);
|
| 819 |
+
```
|
| 820 |
+
|
| 821 |
+
*Constraints:* `os << p.get()` is a valid expression.
|
| 822 |
+
|
| 823 |
+
*Effects:* Equivalent to: `os << p.get();`
|
| 824 |
+
|
| 825 |
+
*Returns:* `os`.
|
| 826 |
+
|
| 827 |
+
### Class `bad_weak_ptr` <a id="util.smartptr.weak.bad">[[util.smartptr.weak.bad]]</a>
|
| 828 |
|
| 829 |
``` cpp
|
| 830 |
namespace std {
|
| 831 |
class bad_weak_ptr : public exception {
|
| 832 |
public:
|
| 833 |
+
// see [exception] for the specification of the special member functions
|
| 834 |
+
const char* what() const noexcept override;
|
| 835 |
};
|
| 836 |
}
|
| 837 |
```
|
| 838 |
|
| 839 |
An exception of type `bad_weak_ptr` is thrown by the `shared_ptr`
|
| 840 |
constructor taking a `weak_ptr`.
|
| 841 |
|
| 842 |
``` cpp
|
| 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.
|
|
|
|
| 860 |
using element_type = remove_extent_t<T>;
|
| 861 |
using weak_type = weak_ptr<T>;
|
| 862 |
|
| 863 |
// [util.smartptr.shared.const], constructors
|
| 864 |
constexpr shared_ptr() noexcept;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 865 |
constexpr shared_ptr(nullptr_t) noexcept : shared_ptr() { }
|
| 866 |
+
template<class Y>
|
| 867 |
+
explicit shared_ptr(Y* p);
|
| 868 |
+
template<class Y, class D>
|
| 869 |
+
shared_ptr(Y* p, D d);
|
| 870 |
+
template<class Y, class D, class A>
|
| 871 |
+
shared_ptr(Y* p, D d, A a);
|
| 872 |
+
template<class D>
|
| 873 |
+
shared_ptr(nullptr_t p, D d);
|
| 874 |
+
template<class D, class A>
|
| 875 |
+
shared_ptr(nullptr_t p, D d, A a);
|
| 876 |
+
template<class Y>
|
| 877 |
+
shared_ptr(const shared_ptr<Y>& r, element_type* p) noexcept;
|
| 878 |
+
template<class Y>
|
| 879 |
+
shared_ptr(shared_ptr<Y>&& r, element_type* p) noexcept;
|
| 880 |
+
shared_ptr(const shared_ptr& r) noexcept;
|
| 881 |
+
template<class Y>
|
| 882 |
+
shared_ptr(const shared_ptr<Y>& r) noexcept;
|
| 883 |
+
shared_ptr(shared_ptr&& r) noexcept;
|
| 884 |
+
template<class Y>
|
| 885 |
+
shared_ptr(shared_ptr<Y>&& r) noexcept;
|
| 886 |
+
template<class Y>
|
| 887 |
+
explicit shared_ptr(const weak_ptr<Y>& r);
|
| 888 |
+
template<class Y, class D>
|
| 889 |
+
shared_ptr(unique_ptr<Y, D>&& r);
|
| 890 |
|
| 891 |
// [util.smartptr.shared.dest], destructor
|
| 892 |
~shared_ptr();
|
| 893 |
|
| 894 |
// [util.smartptr.shared.assign], assignment
|
| 895 |
shared_ptr& operator=(const shared_ptr& r) noexcept;
|
| 896 |
+
template<class Y>
|
| 897 |
+
shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
|
| 898 |
shared_ptr& operator=(shared_ptr&& r) noexcept;
|
| 899 |
+
template<class Y>
|
| 900 |
+
shared_ptr& operator=(shared_ptr<Y>&& r) noexcept;
|
| 901 |
+
template<class Y, class D>
|
| 902 |
+
shared_ptr& operator=(unique_ptr<Y, D>&& r);
|
| 903 |
|
| 904 |
// [util.smartptr.shared.mod], modifiers
|
| 905 |
void swap(shared_ptr& r) noexcept;
|
| 906 |
void reset() noexcept;
|
| 907 |
+
template<class Y>
|
| 908 |
+
void reset(Y* p);
|
| 909 |
+
template<class Y, class D>
|
| 910 |
+
void reset(Y* p, D d);
|
| 911 |
+
template<class Y, class D, class A>
|
| 912 |
+
void reset(Y* p, D d, A a);
|
| 913 |
|
| 914 |
// [util.smartptr.shared.obs], observers
|
| 915 |
element_type* get() const noexcept;
|
| 916 |
T& operator*() const noexcept;
|
| 917 |
T* operator->() const noexcept;
|
| 918 |
element_type& operator[](ptrdiff_t i) const;
|
| 919 |
long use_count() const noexcept;
|
| 920 |
explicit operator bool() const noexcept;
|
| 921 |
+
template<class U>
|
| 922 |
+
bool owner_before(const shared_ptr<U>& b) const noexcept;
|
| 923 |
+
template<class U>
|
| 924 |
+
bool owner_before(const weak_ptr<U>& b) const noexcept;
|
| 925 |
};
|
| 926 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 927 |
template<class T>
|
| 928 |
+
shared_ptr(weak_ptr<T>) -> shared_ptr<T>;
|
| 929 |
+
template<class T, class D>
|
| 930 |
+
shared_ptr(unique_ptr<T, D>) -> shared_ptr<T>;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 931 |
}
|
| 932 |
```
|
| 933 |
|
| 934 |
+
Specializations of `shared_ptr` shall be *Cpp17CopyConstructible*,
|
| 935 |
+
*Cpp17CopyAssignable*, and *Cpp17LessThanComparable*, allowing their use
|
| 936 |
+
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` may be a function type. — *end note*]
|
| 943 |
|
| 944 |
[*Example 1*:
|
| 945 |
|
| 946 |
``` cpp
|
| 947 |
if (shared_ptr<X> px = dynamic_pointer_cast<X>(py)) {
|
|
|
|
| 955 |
functions shall access and modify only the `shared_ptr` and `weak_ptr`
|
| 956 |
objects themselves and not objects they refer to. Changes in
|
| 957 |
`use_count()` do not reflect modifications that can introduce data
|
| 958 |
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>*`
|
| 970 |
+
shall be implicitly convertible to `T*` and the constructor evaluates
|
| 971 |
+
the statement:
|
| 972 |
|
| 973 |
``` cpp
|
| 974 |
if (p != nullptr && p->weak_this.expired())
|
| 975 |
p->weak_this = shared_ptr<remove_cv_t<Y>>(*this, const_cast<remove_cv_t<Y>*>(p));
|
| 976 |
```
|
| 977 |
|
| 978 |
The assignment to the `weak_this` member is not atomic and conflicts
|
| 979 |
+
with any potentially concurrent access to the same object
|
| 980 |
+
[[intro.multithread]].
|
| 981 |
|
| 982 |
``` cpp
|
| 983 |
constexpr shared_ptr() noexcept;
|
| 984 |
```
|
| 985 |
|
| 986 |
+
*Ensures:* `use_count() == 0 && get() == nullptr`.
|
|
|
|
|
|
|
| 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`
|
| 1005 |
object that owns the pointer `p`. Otherwise, constructs a `shared_ptr`
|
| 1006 |
that owns `p` and a deleter of an unspecified type that calls
|
| 1007 |
`delete[] p`. When `T` is not an array type, enables `shared_from_this`
|
| 1008 |
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 could not be obtained.
|
| 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);
|
| 1020 |
template<class D, class A> shared_ptr(nullptr_t p, D d, A a);
|
| 1021 |
```
|
| 1022 |
|
| 1023 |
+
*Constraints:* `is_move_constructible_v<D>` is `true`, and `d(p)` is a
|
| 1024 |
+
well-formed expression. For the first two overloads:
|
| 1025 |
+
|
| 1026 |
+
- If `T` is an array type, then either `T` is `U[N]` and `Y(*)[N]` is
|
| 1027 |
+
convertible to `T*`, or `T` is `U[]` and `Y(*)[]` is convertible to
|
| 1028 |
+
`T*`.
|
| 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* requirements ([[cpp17.allocator]]).
|
| 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 could not be obtained.
|
| 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 |
```
|
| 1051 |
|
| 1052 |
*Effects:* Constructs a `shared_ptr` instance that stores `p` and shares
|
| 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*: To avoid the possibility of a dangling pointer, the user of
|
| 1059 |
+
this constructor should ensure that `p` remains valid at least until the
|
| 1060 |
ownership group of `r` is destroyed. — *end note*]
|
| 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
|
| 1066 |
shared_ptr(const shared_ptr& r) noexcept;
|
| 1067 |
template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
|
| 1068 |
```
|
| 1069 |
|
| 1070 |
+
*Constraints:* For the second constructor, `Y*` is compatible with `T*`.
|
|
|
|
| 1071 |
|
| 1072 |
*Effects:* If `r` is empty, constructs an empty `shared_ptr` object;
|
| 1073 |
otherwise, constructs a `shared_ptr` object that shares ownership with
|
| 1074 |
`r`.
|
| 1075 |
|
| 1076 |
+
*Ensures:* `get() == r.get() && use_count() == r.use_count()`.
|
| 1077 |
|
| 1078 |
``` cpp
|
| 1079 |
shared_ptr(shared_ptr&& r) noexcept;
|
| 1080 |
template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
|
| 1081 |
```
|
| 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` shall contain the old value of `r`. `r` shall be
|
| 1088 |
+
empty. `r.get() == nullptr`.
|
| 1089 |
|
| 1090 |
``` cpp
|
| 1091 |
template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
|
| 1092 |
```
|
| 1093 |
|
| 1094 |
+
*Constraints:* `Y*` is compatible with `T*`.
|
| 1095 |
+
|
| 1096 |
*Effects:* Constructs a `shared_ptr` object that shares ownership with
|
| 1097 |
`r` and stores a copy of the pointer stored in `r`. If an exception is
|
| 1098 |
thrown, the constructor has no effect.
|
| 1099 |
|
| 1100 |
+
*Ensures:* `use_count() == r.use_count()`.
|
| 1101 |
|
| 1102 |
*Throws:* `bad_weak_ptr` when `r.expired()`.
|
| 1103 |
|
|
|
|
|
|
|
|
|
|
| 1104 |
``` cpp
|
| 1105 |
template<class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
|
| 1106 |
```
|
| 1107 |
|
| 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, equivalent to
|
| 1114 |
`shared_ptr(r.release(), ref(r.get_deleter()))`. If an exception is
|
| 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 |
|
|
|
|
| 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 1*:
|
| 1149 |
|
| 1150 |
The use count updates caused by the temporary object construction and
|
| 1151 |
destruction are not observable side effects, so the implementation may
|
| 1152 |
meet the effects (and the implied guarantees) via different means,
|
| 1153 |
without creating a temporary. In particular, in the example:
|
|
|
|
| 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 |
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 |
|
| 1223 |
``` cpp
|
| 1224 |
T& operator*() const noexcept;
|
| 1225 |
```
|
| 1226 |
|
| 1227 |
+
*Preconditions:* `get() != 0`.
|
| 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
|
| 1233 |
unspecified what its return type is, except that the declaration
|
| 1234 |
+
(although not necessarily the definition) of the function shall be
|
| 1235 |
+
well-formed.
|
| 1236 |
|
| 1237 |
``` cpp
|
| 1238 |
T* operator->() const noexcept;
|
| 1239 |
```
|
| 1240 |
|
| 1241 |
+
*Preconditions:* `get() != 0`.
|
| 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
|
| 1247 |
its return type is, except that the declaration (although not
|
| 1248 |
+
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() != 0 && i >= 0`. If `T` is `U[N]`, `i < N`.
|
| 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;
|
|
|
|
| 1269 |
*Returns:* The number of `shared_ptr` objects, `*this` included, that
|
| 1270 |
share ownership with `*this`, or `0` when `*this` is empty.
|
| 1271 |
|
| 1272 |
*Synchronization:* None.
|
| 1273 |
|
| 1274 |
+
[*Note 1*: `get() == nullptr` does not imply a specific return value of
|
| 1275 |
`use_count()`. — *end note*]
|
| 1276 |
|
| 1277 |
+
[*Note 2*: `weak_ptr<T>::lock()` can affect the return value of
|
| 1278 |
`use_count()`. — *end note*]
|
| 1279 |
|
| 1280 |
+
[*Note 3*: When multiple threads can affect the return value of
|
| 1281 |
`use_count()`, the result should be treated as approximate. In
|
| 1282 |
particular, `use_count() == 1` does not imply that accesses through a
|
| 1283 |
previously destroyed `shared_ptr` have in any sense
|
| 1284 |
completed. — *end note*]
|
| 1285 |
|
|
|
|
| 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.
|
| 1312 |
|
| 1313 |
``` cpp
|
| 1314 |
+
template<class T, ...>
|
| 1315 |
+
shared_ptr<T> make_shared(args);
|
| 1316 |
+
template<class T, class A, ...>
|
| 1317 |
+
shared_ptr<T> allocate_shared(const A& a, args);
|
| 1318 |
+
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* requirements
|
| 1325 |
+
([[cpp17.allocator]]).
|
|
|
|
|
|
|
|
|
|
| 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 1*: This provides efficiency equivalent to an intrusive smart
|
| 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`.
|
| 1353 |
+
- When an object of an array type is specified to have a default initial
|
| 1354 |
+
value, this shall be interpreted to mean that each array element of
|
| 1355 |
+
the object has a default initial value.
|
| 1356 |
+
- When a (sub)object of a non-array type `U` is specified to have an
|
| 1357 |
+
initial value of `v`, or `U(l...)`, where `l...` is a list of
|
| 1358 |
+
constructor arguments, `make_shared` shall initialize this (sub)object
|
| 1359 |
+
via the expression `::new(pv) U(v)` or `::new(pv) U(l...)`
|
| 1360 |
+
respectively, where `pv` has type `void*` and points to storage
|
| 1361 |
+
suitable to hold an object of type `U`.
|
| 1362 |
+
- When a (sub)object of a non-array type `U` is specified to have an
|
| 1363 |
+
initial value of `v`, or `U(l...)`, where `l...` is a list of
|
| 1364 |
+
constructor arguments, `allocate_shared` shall initialize this
|
| 1365 |
+
(sub)object via the expression
|
| 1366 |
+
- `allocator_traits<A2>::construct(a2, pv, v)` or
|
| 1367 |
+
- `allocator_traits<A2>::construct(a2, pv, l...)`
|
| 1368 |
|
| 1369 |
+
respectively, where `pv` points to storage suitable to hold an object
|
| 1370 |
+
of type `U` and `a2` of type `A2` is a rebound copy of the allocator
|
| 1371 |
+
`a` passed to `allocate_shared` such that its `value_type` is
|
| 1372 |
+
`remove_cv_t<U>`.
|
| 1373 |
+
- When a (sub)object of non-array type `U` is specified to have a
|
| 1374 |
+
default initial value, `make_shared` shall initialize this (sub)object
|
| 1375 |
+
via the expression `::new(pv) U()`, where `pv` has type `void*` and
|
| 1376 |
+
points to storage suitable to hold an object of type `U`.
|
| 1377 |
+
- When a (sub)object of non-array type `U` is specified to have a
|
| 1378 |
+
default initial value, `allocate_shared` shall initialize this
|
| 1379 |
+
(sub)object via the expression
|
| 1380 |
+
`allocator_traits<A2>::construct(a2, pv)`, where `pv` points to
|
| 1381 |
+
storage suitable to hold an object of type `U` and `a2` of type `A2`
|
| 1382 |
+
is a rebound copy of the allocator `a` passed to `allocate_shared`
|
| 1383 |
+
such that its `value_type` is `remove_cv_t<U>`.
|
| 1384 |
+
- When a (sub)object of non-array type `U` is initialized by
|
| 1385 |
+
`make_shared_for_overwrite` or `allocate_shared_for_overwrite`, it is
|
| 1386 |
+
initialized via the expression `::new(pv) U`, where `pv` has type
|
| 1387 |
+
`void*` and points to storage suitable to hold an object of type `U`.
|
| 1388 |
+
- Array elements are initialized in ascending order of their addresses.
|
| 1389 |
+
- When the lifetime of the object managed by the return value ends, or
|
| 1390 |
+
when the initialization of an array element throws an exception, the
|
| 1391 |
+
initialized elements are destroyed in the reverse order of their
|
| 1392 |
+
original construction.
|
| 1393 |
+
- When a (sub)object of non-array type `U` that was initialized by
|
| 1394 |
+
`make_shared` is to be destroyed, it is destroyed via the expression
|
| 1395 |
+
`pv->~U()` where `pv` points to that object of type `U`.
|
| 1396 |
+
- When a (sub)object of non-array type `U` that was initialized by
|
| 1397 |
+
`allocate_shared` is to be destroyed, it is destroyed via the
|
| 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 1*: These functions will typically allocate more memory than
|
| 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>
|
| 1409 |
+
shared_ptr<T> make_shared(Args&&... args); // T is not array
|
| 1410 |
+
template<class T, class A, class... Args>
|
| 1411 |
+
shared_ptr<T> allocate_shared(const A& a, Args&&... args); // T is not array
|
| 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 |
+
|
| 1423 |
+
[*Example 1*:
|
| 1424 |
+
|
| 1425 |
+
``` cpp
|
| 1426 |
+
shared_ptr<int> p = make_shared<int>(); // shared_ptr to int()
|
| 1427 |
+
shared_ptr<vector<int>> q = make_shared<vector<int>>(16, 1);
|
| 1428 |
+
// shared_ptr to vector of 16 elements with value 1
|
| 1429 |
+
```
|
| 1430 |
+
|
| 1431 |
+
— *end example*]
|
| 1432 |
+
|
| 1433 |
+
``` cpp
|
| 1434 |
+
template<class T> shared_ptr<T>
|
| 1435 |
+
make_shared(size_t N); // T is U[]
|
| 1436 |
+
template<class T, class A>
|
| 1437 |
+
shared_ptr<T> allocate_shared(const A& a, size_t N); // T is U[]
|
| 1438 |
+
```
|
| 1439 |
+
|
| 1440 |
+
*Constraints:* `T` is of the form `U[]`.
|
| 1441 |
+
|
| 1442 |
+
*Returns:* A `shared_ptr` to an object of type `U[N]` with a default
|
| 1443 |
+
initial value, where `U` is `remove_extent_t<T>`.
|
| 1444 |
+
|
| 1445 |
+
[*Example 2*:
|
| 1446 |
+
|
| 1447 |
+
``` cpp
|
| 1448 |
+
shared_ptr<double[]> p = make_shared<double[]>(1024);
|
| 1449 |
+
// shared_ptr to a value-initialized double[1024]
|
| 1450 |
+
shared_ptr<double[][2][2]> q = make_shared<double[][2][2]>(6);
|
| 1451 |
+
// shared_ptr to a value-initialized double[6][2][2]
|
| 1452 |
+
```
|
| 1453 |
+
|
| 1454 |
+
— *end example*]
|
| 1455 |
+
|
| 1456 |
+
``` cpp
|
| 1457 |
+
template<class T>
|
| 1458 |
+
shared_ptr<T> make_shared(); // T is U[N]
|
| 1459 |
+
template<class T, class A>
|
| 1460 |
+
shared_ptr<T> allocate_shared(const A& a); // T is U[N]
|
| 1461 |
+
```
|
| 1462 |
+
|
| 1463 |
+
*Constraints:* `T` is of the form `U[N]`.
|
| 1464 |
+
|
| 1465 |
+
*Returns:* A `shared_ptr` to an object of type `T` with a default
|
| 1466 |
+
initial value.
|
| 1467 |
+
|
| 1468 |
+
[*Example 3*:
|
| 1469 |
+
|
| 1470 |
+
``` cpp
|
| 1471 |
+
shared_ptr<double[1024]> p = make_shared<double[1024]>();
|
| 1472 |
+
// shared_ptr to a value-initialized double[1024]
|
| 1473 |
+
shared_ptr<double[6][2][2]> q = make_shared<double[6][2][2]>();
|
| 1474 |
+
// shared_ptr to a value-initialized double[6][2][2]
|
| 1475 |
+
```
|
| 1476 |
+
|
| 1477 |
+
— *end example*]
|
| 1478 |
+
|
| 1479 |
+
``` cpp
|
| 1480 |
+
template<class T>
|
| 1481 |
+
shared_ptr<T> make_shared(size_t N,
|
| 1482 |
+
const remove_extent_t<T>& u); // T is U[]
|
| 1483 |
+
template<class T, class A>
|
| 1484 |
+
shared_ptr<T> allocate_shared(const A& a, size_t N,
|
| 1485 |
+
const remove_extent_t<T>& u); // T is U[]
|
| 1486 |
+
```
|
| 1487 |
+
|
| 1488 |
+
*Constraints:* `T` is of the form `U[]`.
|
| 1489 |
+
|
| 1490 |
+
*Returns:* A `shared_ptr` to an object of type `U[N]`, where `U` is
|
| 1491 |
+
`remove_extent_t<T>` and each array element has an initial value of `u`.
|
| 1492 |
+
|
| 1493 |
+
[*Example 4*:
|
| 1494 |
+
|
| 1495 |
+
``` cpp
|
| 1496 |
+
shared_ptr<double[]> p = make_shared<double[]>(1024, 1.0);
|
| 1497 |
+
// shared_ptr to a double[1024], where each element is 1.0
|
| 1498 |
+
shared_ptr<double[][2]> q = make_shared<double[][2]>(6, {1.0, 0.0});
|
| 1499 |
+
// shared_ptr to a double[6][2], where each double[2] element is {1.0, 0.0}
|
| 1500 |
+
shared_ptr<vector<int>[]> r = make_shared<vector<int>[]>(4, {1, 2});
|
| 1501 |
+
// shared_ptr to a vector<int>[4], where each vector has contents {1, 2}
|
| 1502 |
+
```
|
| 1503 |
+
|
| 1504 |
+
— *end example*]
|
| 1505 |
+
|
| 1506 |
+
``` cpp
|
| 1507 |
+
template<class T>
|
| 1508 |
+
shared_ptr<T> make_shared(const remove_extent_t<T>& u); // T is U[N]
|
| 1509 |
+
template<class T, class A>
|
| 1510 |
+
shared_ptr<T> allocate_shared(const A& a,
|
| 1511 |
+
const remove_extent_t<T>& u); // T is U[N]
|
| 1512 |
+
```
|
| 1513 |
+
|
| 1514 |
+
*Constraints:* `T` is of the form `U[N]`.
|
| 1515 |
+
|
| 1516 |
+
*Returns:* A `shared_ptr` to an object of type `T`, where each array
|
| 1517 |
+
element of type `remove_extent_t<T>` has an initial value of `u`.
|
| 1518 |
+
|
| 1519 |
+
[*Example 5*:
|
| 1520 |
+
|
| 1521 |
+
``` cpp
|
| 1522 |
+
shared_ptr<double[1024]> p = make_shared<double[1024]>(1.0);
|
| 1523 |
+
// shared_ptr to a double[1024], where each element is 1.0
|
| 1524 |
+
shared_ptr<double[6][2]> q = make_shared<double[6][2]>({1.0, 0.0});
|
| 1525 |
+
// shared_ptr to a double[6][2], where each double[2] element is {1.0, 0.0}
|
| 1526 |
+
shared_ptr<vector<int>[4]> r = make_shared<vector<int>[4]>({1, 2});
|
| 1527 |
+
// shared_ptr to a vector<int>[4], where each vector has contents {1, 2}
|
| 1528 |
+
```
|
| 1529 |
+
|
| 1530 |
+
— *end example*]
|
| 1531 |
+
|
| 1532 |
+
``` cpp
|
| 1533 |
+
template<class T>
|
| 1534 |
+
shared_ptr<T> make_shared_for_overwrite();
|
| 1535 |
+
template<class T, class A>
|
| 1536 |
+
shared_ptr<T> allocate_shared_for_overwrite(const A& a);
|
| 1537 |
+
```
|
| 1538 |
+
|
| 1539 |
+
*Constraints:* `T` is not an array of unknown bound.
|
| 1540 |
+
|
| 1541 |
+
*Returns:* A `shared_ptr` to an object of type `T`.
|
| 1542 |
+
|
| 1543 |
+
[*Example 6*:
|
| 1544 |
+
|
| 1545 |
+
``` cpp
|
| 1546 |
+
struct X { double data[1024]; };
|
| 1547 |
+
shared_ptr<X> p = make_shared_for_overwrite<X>();
|
| 1548 |
+
// shared_ptr to a default-initialized X, where each element in X::data has an indeterminate value
|
| 1549 |
+
|
| 1550 |
+
shared_ptr<double[1024]> q = make_shared_for_overwrite<double[1024]>();
|
| 1551 |
+
// shared_ptr to a default-initialized double[1024], where each element has an indeterminate value
|
| 1552 |
+
```
|
| 1553 |
+
|
| 1554 |
+
— *end example*]
|
| 1555 |
+
|
| 1556 |
+
``` cpp
|
| 1557 |
+
template<class T>
|
| 1558 |
+
shared_ptr<T> make_shared_for_overwrite(size_t N);
|
| 1559 |
+
template<class T, class A>
|
| 1560 |
+
shared_ptr<T> allocate_shared_for_overwrite(const A& a, size_t N);
|
| 1561 |
+
```
|
| 1562 |
+
|
| 1563 |
+
*Constraints:* `T` is an array of unknown bound.
|
| 1564 |
+
|
| 1565 |
+
*Returns:* A `shared_ptr` to an object of type `U[N]`, where `U` is
|
| 1566 |
+
`remove_extent_t<T>`.
|
| 1567 |
+
|
| 1568 |
+
[*Example 7*:
|
| 1569 |
+
|
| 1570 |
+
``` cpp
|
| 1571 |
+
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 |
```
|
| 1583 |
|
| 1584 |
*Returns:* `a.get() == b.get()`.
|
| 1585 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1586 |
``` cpp
|
| 1587 |
template<class T>
|
| 1588 |
bool operator==(const shared_ptr<T>& a, nullptr_t) noexcept;
|
|
|
|
|
|
|
| 1589 |
```
|
| 1590 |
|
| 1591 |
*Returns:* `!a`.
|
| 1592 |
|
| 1593 |
``` cpp
|
| 1594 |
+
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 1*: Defining a comparison function allows `shared_ptr` objects
|
| 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:* `compare_three_way()(a.get(), nullptr)`.
|
|
|
|
| 1609 |
|
| 1610 |
+
#### Specialized algorithms <a id="util.smartptr.shared.spec">[[util.smartptr.shared.spec]]</a>
|
| 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>
|
| 1625 |
+
shared_ptr<T> static_pointer_cast(shared_ptr<U>&& r) noexcept;
|
| 1626 |
```
|
| 1627 |
|
| 1628 |
+
*Mandates:* The expression `static_cast<T*>((U*)nullptr)` is
|
| 1629 |
+
well-formed.
|
| 1630 |
|
| 1631 |
*Returns:*
|
| 1632 |
|
| 1633 |
``` cpp
|
| 1634 |
+
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 1*: The seemingly equivalent expression
|
| 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
|
| 1646 |
template<class T, class U>
|
| 1647 |
shared_ptr<T> dynamic_pointer_cast(const shared_ptr<U>& r) noexcept;
|
| 1648 |
+
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 well
|
| 1655 |
+
formed.
|
| 1656 |
+
|
| 1657 |
+
*Preconditions:* The expression
|
| 1658 |
+
`dynamic_cast<typename shared_ptr<T>::element_type*>(r.get())` has
|
| 1659 |
+
well-defined behavior.
|
| 1660 |
|
| 1661 |
*Returns:*
|
| 1662 |
|
| 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 2*: The seemingly equivalent expression
|
| 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
|
| 1674 |
template<class T, class U>
|
| 1675 |
shared_ptr<T> const_pointer_cast(const shared_ptr<U>& r) noexcept;
|
| 1676 |
+
template<class T, class U>
|
| 1677 |
+
shared_ptr<T> const_pointer_cast(shared_ptr<U>&& r) noexcept;
|
| 1678 |
```
|
| 1679 |
|
| 1680 |
+
*Mandates:* The expression `const_cast<T*>((U*)nullptr)` is well-formed.
|
| 1681 |
|
| 1682 |
*Returns:*
|
| 1683 |
|
| 1684 |
``` cpp
|
| 1685 |
+
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 3*: The seemingly equivalent expression
|
| 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
|
| 1697 |
template<class T, class U>
|
| 1698 |
shared_ptr<T> reinterpret_pointer_cast(const shared_ptr<U>& r) noexcept;
|
| 1699 |
+
template<class T, class U>
|
| 1700 |
+
shared_ptr<T> reinterpret_pointer_cast(shared_ptr<U>&& r) noexcept;
|
| 1701 |
```
|
| 1702 |
|
| 1703 |
+
*Mandates:* The expression `reinterpret_cast<T*>((U*)nullptr)` is
|
| 1704 |
+
well-formed.
|
| 1705 |
|
| 1706 |
*Returns:*
|
| 1707 |
|
| 1708 |
``` cpp
|
| 1709 |
+
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 4*: The seemingly equivalent expression
|
| 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 |
*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 1*: It is unspecified whether the pointer remains valid longer
|
| 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 |
|
| 1754 |
``` cpp
|
| 1755 |
namespace std {
|
| 1756 |
template<class T> class weak_ptr {
|
| 1757 |
public:
|
| 1758 |
+
using element_type = remove_extent_t<T>;
|
| 1759 |
|
| 1760 |
// [util.smartptr.weak.const], constructors
|
| 1761 |
constexpr weak_ptr() noexcept;
|
| 1762 |
+
template<class Y>
|
| 1763 |
+
weak_ptr(const shared_ptr<Y>& r) noexcept;
|
| 1764 |
weak_ptr(const weak_ptr& r) noexcept;
|
| 1765 |
+
template<class Y>
|
| 1766 |
+
weak_ptr(const weak_ptr<Y>& r) noexcept;
|
| 1767 |
weak_ptr(weak_ptr&& r) noexcept;
|
| 1768 |
+
template<class Y>
|
| 1769 |
+
weak_ptr(weak_ptr<Y>&& r) noexcept;
|
| 1770 |
|
| 1771 |
// [util.smartptr.weak.dest], destructor
|
| 1772 |
~weak_ptr();
|
| 1773 |
|
| 1774 |
// [util.smartptr.weak.assign], assignment
|
| 1775 |
weak_ptr& operator=(const weak_ptr& r) noexcept;
|
| 1776 |
+
template<class Y>
|
| 1777 |
+
weak_ptr& operator=(const weak_ptr<Y>& r) noexcept;
|
| 1778 |
+
template<class Y>
|
| 1779 |
+
weak_ptr& operator=(const shared_ptr<Y>& r) noexcept;
|
| 1780 |
weak_ptr& operator=(weak_ptr&& r) noexcept;
|
| 1781 |
+
template<class Y>
|
| 1782 |
+
weak_ptr& operator=(weak_ptr<Y>&& r) noexcept;
|
| 1783 |
|
| 1784 |
// [util.smartptr.weak.mod], modifiers
|
| 1785 |
void swap(weak_ptr& r) noexcept;
|
| 1786 |
void reset() noexcept;
|
| 1787 |
|
| 1788 |
// [util.smartptr.weak.obs], observers
|
| 1789 |
long use_count() const noexcept;
|
| 1790 |
bool expired() const noexcept;
|
| 1791 |
shared_ptr<T> lock() const noexcept;
|
| 1792 |
+
template<class U>
|
| 1793 |
+
bool owner_before(const shared_ptr<U>& b) const noexcept;
|
| 1794 |
+
template<class U>
|
| 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;
|
| 1823 |
template<class Y> weak_ptr(const weak_ptr<Y>& r) noexcept;
|
| 1824 |
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 that shares ownership with `r`
|
| 1832 |
and stores a copy of the pointer stored in `r`.
|
| 1833 |
|
| 1834 |
+
*Ensures:* `use_count() == r.use_count()`.
|
| 1835 |
|
| 1836 |
``` cpp
|
| 1837 |
weak_ptr(weak_ptr&& r) noexcept;
|
| 1838 |
template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept;
|
| 1839 |
```
|
| 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` shall contain the old value of `r`. `r` shall be
|
| 1846 |
+
empty. `r.use_count() == 0`.
|
| 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;
|
|
|
|
| 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 |
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 |
|
|
|
|
| 1913 |
|
| 1914 |
*Returns:* `expired() ? shared_ptr<T>() : shared_ptr<T>(*this)`,
|
| 1915 |
executed atomically.
|
| 1916 |
|
| 1917 |
``` cpp
|
| 1918 |
+
template<class U> bool owner_before(const shared_ptr<U>& b) const noexcept;
|
| 1919 |
+
template<class U> bool owner_before(const weak_ptr<U>& b) const noexcept;
|
| 1920 |
```
|
| 1921 |
|
| 1922 |
*Returns:* An unspecified value such that
|
| 1923 |
|
| 1924 |
- `x.owner_before(y)` defines a strict weak ordering as defined
|
|
|
|
| 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
|
|
|
|
| 1971 |
using is_transparent = unspecified;
|
| 1972 |
};
|
| 1973 |
}
|
| 1974 |
```
|
| 1975 |
|
| 1976 |
+
`operator()(x, y)` returns `x.owner_before(y)`.
|
| 1977 |
|
| 1978 |
[*Note 1*:
|
| 1979 |
|
| 1980 |
Note that
|
| 1981 |
|
|
|
|
| 1986 |
`weak_ptr` instances are equivalent if and only if they share
|
| 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 |
|
|
|
|
| 2015 |
protected:
|
| 2016 |
constexpr enable_shared_from_this() noexcept;
|
| 2017 |
enable_shared_from_this(const enable_shared_from_this&) noexcept;
|
| 2018 |
enable_shared_from_this& operator=(const enable_shared_from_this&) noexcept;
|
| 2019 |
~enable_shared_from_this();
|
| 2020 |
+
|
| 2021 |
public:
|
| 2022 |
shared_ptr<T> shared_from_this();
|
| 2023 |
shared_ptr<T const> shared_from_this() const;
|
| 2024 |
weak_ptr<T> weak_from_this() noexcept;
|
| 2025 |
weak_ptr<T const> weak_from_this() const noexcept;
|
| 2026 |
+
|
| 2027 |
private:
|
| 2028 |
mutable weak_ptr<T> weak_this; // exposition only
|
| 2029 |
};
|
| 2030 |
}
|
| 2031 |
```
|
|
|
|
| 2060 |
weak_ptr<T const> weak_from_this() const noexcept;
|
| 2061 |
```
|
| 2062 |
|
| 2063 |
*Returns:* `weak_this`.
|
| 2064 |
|
| 2065 |
+
### Smart pointer hash support <a id="util.smartptr.hash">[[util.smartptr.hash]]</a>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2066 |
|
| 2067 |
``` cpp
|
| 2068 |
template<class T, class D> struct hash<unique_ptr<T, D>>;
|
| 2069 |
```
|
| 2070 |
|
| 2071 |
Letting `UP` be `unique_ptr<T,D>`, the specialization `hash<UP>` is
|
| 2072 |
+
enabled [[unord.hash]] if and only if `hash<typename UP::pointer>` is
|
| 2073 |
enabled. When enabled, for an object `p` of type `UP`, `hash<UP>()(p)`
|
| 2074 |
+
evaluates to the same value as `hash<typename UP::pointer>()(p.get())`.
|
| 2075 |
+
The member functions are not guaranteed to be `noexcept`.
|
|
|
|
| 2076 |
|
| 2077 |
``` cpp
|
| 2078 |
template<class T> struct hash<shared_ptr<T>>;
|
| 2079 |
```
|
| 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 |
|