- tmp/tmpq41ma8tp/{from.md → to.md} +601 -430
tmp/tmpq41ma8tp/{from.md → to.md}
RENAMED
|
@@ -27,24 +27,26 @@ postconditions hold:
|
|
| 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 |
|
| 35 |
Each object of a type `U` instantiated from the `unique_ptr` template
|
| 36 |
specified in this subclause has the strict ownership semantics,
|
| 37 |
specified above, of a unique pointer. In partial satisfaction of these
|
| 38 |
semantics, each such `U` is `MoveConstructible` and `MoveAssignable`,
|
| 39 |
but is not `CopyConstructible` nor `CopyAssignable`. The template
|
| 40 |
parameter `T` of `unique_ptr` may be an incomplete type.
|
| 41 |
|
| 42 |
-
The uses of `unique_ptr` include providing exception safety
|
| 43 |
-
dynamically allocated memory, passing ownership of dynamically
|
| 44 |
-
memory to a function, and returning dynamically allocated
|
| 45 |
-
function.
|
| 46 |
|
| 47 |
``` cpp
|
| 48 |
namespace std {
|
| 49 |
template<class T> struct default_delete;
|
| 50 |
template<class T> struct default_delete<T[]>;
|
|
@@ -133,56 +135,65 @@ unless `U*` is implicitly convertible to `T*`.
|
|
| 133 |
|
| 134 |
``` cpp
|
| 135 |
void operator()(T* ptr) const;
|
| 136 |
```
|
| 137 |
|
| 138 |
-
*Effects:*
|
| 139 |
|
| 140 |
*Remarks:* If `T` is an incomplete type, the program is ill-formed.
|
| 141 |
|
| 142 |
##### `default_delete<T[]>` <a id="unique.ptr.dltr.dflt1">[[unique.ptr.dltr.dflt1]]</a>
|
| 143 |
|
| 144 |
``` cpp
|
| 145 |
namespace std {
|
| 146 |
template <class T> struct default_delete<T[]> {
|
| 147 |
constexpr default_delete() noexcept = default;
|
| 148 |
-
|
| 149 |
-
template <class U> void operator()(U*) const
|
| 150 |
};
|
| 151 |
}
|
| 152 |
```
|
| 153 |
|
| 154 |
``` cpp
|
| 155 |
-
|
| 156 |
```
|
| 157 |
|
| 158 |
-
*Effects:*
|
|
|
|
| 159 |
|
| 160 |
-
*Remarks:*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
#### `unique_ptr` for single objects <a id="unique.ptr.single">[[unique.ptr.single]]</a>
|
| 163 |
|
| 164 |
``` cpp
|
| 165 |
namespace std {
|
| 166 |
template <class T, class D = default_delete<T>> class unique_ptr {
|
| 167 |
public:
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
|
| 172 |
// [unique.ptr.single.ctor], constructors
|
| 173 |
constexpr unique_ptr() noexcept;
|
| 174 |
explicit unique_ptr(pointer p) noexcept;
|
| 175 |
unique_ptr(pointer p, see below d1) noexcept;
|
| 176 |
unique_ptr(pointer p, see below d2) noexcept;
|
| 177 |
unique_ptr(unique_ptr&& u) noexcept;
|
| 178 |
-
constexpr unique_ptr(nullptr_t) noexcept
|
| 179 |
-
: unique_ptr() { }
|
| 180 |
template <class U, class E>
|
| 181 |
unique_ptr(unique_ptr<U, E>&& u) noexcept;
|
| 182 |
-
template <class U>
|
| 183 |
-
unique_ptr(auto_ptr<U>&& u) noexcept;
|
| 184 |
|
| 185 |
// [unique.ptr.single.dtor], destructor
|
| 186 |
~unique_ptr();
|
| 187 |
|
| 188 |
// [unique.ptr.single.asgn], assignment
|
|
@@ -196,11 +207,11 @@ namespace std {
|
|
| 196 |
pointer get() const noexcept;
|
| 197 |
deleter_type& get_deleter() noexcept;
|
| 198 |
const deleter_type& get_deleter() const noexcept;
|
| 199 |
explicit operator bool() const noexcept;
|
| 200 |
|
| 201 |
-
// [unique.ptr.single.modifiers] modifiers
|
| 202 |
pointer release() noexcept;
|
| 203 |
void reset(pointer p = pointer()) noexcept;
|
| 204 |
void swap(unique_ptr& u) noexcept;
|
| 205 |
|
| 206 |
// disable copy from lvalue
|
|
@@ -210,149 +221,148 @@ namespace std {
|
|
| 210 |
}
|
| 211 |
```
|
| 212 |
|
| 213 |
The default type for the template parameter `D` is `default_delete`. A
|
| 214 |
client-supplied template argument `D` shall be a function object type (
|
| 215 |
-
[[function.objects]]), lvalue
|
| 216 |
to function object type for which, given a value `d` of type `D` and a
|
| 217 |
value `ptr` of type `unique_ptr<T, D>::pointer`, the expression `d(ptr)`
|
| 218 |
is valid and has the effect of disposing of the pointer as appropriate
|
| 219 |
for that deleter.
|
| 220 |
|
| 221 |
If the deleter’s type `D` is not a reference type, `D` shall satisfy the
|
| 222 |
-
requirements of `Destructible` (Table [[destructible]]).
|
| 223 |
|
| 224 |
-
If the
|
|
|
|
| 225 |
D>::pointer` shall be a synonym for `remove_reference_t<D>::pointer`.
|
| 226 |
-
Otherwise `unique_ptr<T, D>::pointer` shall be a synonym for
|
| 227 |
-
type `unique_ptr<T,
|
| 228 |
D>::pointer` shall satisfy the requirements of `NullablePointer` (
|
| 229 |
[[nullablepointer.requirements]]).
|
| 230 |
|
| 231 |
-
Given an allocator type `X` ([[allocator.requirements]])
|
| 232 |
-
`A` be a synonym for `allocator_traits<X>`, the types
|
| 233 |
-
`A::
|
| 234 |
-
be used as
|
|
|
|
| 235 |
|
| 236 |
##### `unique_ptr` constructors <a id="unique.ptr.single.ctor">[[unique.ptr.single.ctor]]</a>
|
| 237 |
|
| 238 |
``` cpp
|
| 239 |
constexpr unique_ptr() noexcept;
|
|
|
|
| 240 |
```
|
| 241 |
|
| 242 |
*Requires:* `D` shall satisfy the requirements of `DefaultConstructible`
|
| 243 |
-
(Table [[defaultconstructible]]), and that construction shall not
|
| 244 |
-
an exception.
|
| 245 |
|
| 246 |
*Effects:* Constructs a `unique_ptr` object that owns nothing,
|
| 247 |
value-initializing the stored pointer and the stored deleter.
|
| 248 |
|
| 249 |
*Postconditions:* `get() == nullptr`. `get_deleter()` returns a
|
| 250 |
reference to the stored deleter.
|
| 251 |
|
| 252 |
-
*Remarks:* If
|
| 253 |
-
|
|
|
|
| 254 |
|
| 255 |
``` cpp
|
| 256 |
explicit unique_ptr(pointer p) noexcept;
|
| 257 |
```
|
| 258 |
|
| 259 |
*Requires:* `D` shall satisfy the requirements of `DefaultConstructible`
|
| 260 |
-
(Table [[defaultconstructible]]), and that construction shall not
|
| 261 |
-
an exception.
|
| 262 |
|
| 263 |
*Effects:* Constructs a `unique_ptr` which owns `p`, initializing the
|
| 264 |
stored pointer with `p` and value-initializing the stored deleter.
|
| 265 |
|
| 266 |
*Postconditions:* `get() == p`. `get_deleter()` returns a reference to
|
| 267 |
the stored deleter.
|
| 268 |
|
| 269 |
-
*Remarks:* If
|
| 270 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
|
| 272 |
``` cpp
|
| 273 |
unique_ptr(pointer p, see below d1) noexcept;
|
| 274 |
unique_ptr(pointer p, see below d2) noexcept;
|
| 275 |
```
|
| 276 |
|
| 277 |
The signature of these constructors depends upon whether `D` is a
|
| 278 |
-
reference type. If `D` is non-reference type `A`, then the signatures
|
| 279 |
are:
|
| 280 |
|
| 281 |
``` cpp
|
| 282 |
-
unique_ptr(pointer p, const A& d);
|
| 283 |
-
unique_ptr(pointer p, A&& d);
|
| 284 |
```
|
| 285 |
|
| 286 |
-
If `D` is an lvalue
|
| 287 |
|
| 288 |
``` cpp
|
| 289 |
-
unique_ptr(pointer p, A& d);
|
| 290 |
-
unique_ptr(pointer p, A&& d);
|
| 291 |
```
|
| 292 |
|
| 293 |
-
If `D` is an lvalue
|
| 294 |
|
| 295 |
``` cpp
|
| 296 |
-
unique_ptr(pointer p, const A& d);
|
| 297 |
-
unique_ptr(pointer p, const A&& d);
|
| 298 |
```
|
| 299 |
|
| 300 |
-
*Requires:*
|
| 301 |
-
|
| 302 |
-
- If `D` is not an lvalue-reference type then
|
| 303 |
-
- If `d` is an lvalue or `const` rvalue then the first constructor of
|
| 304 |
-
this pair will be selected. `D` shall satisfy the requirements of
|
| 305 |
-
`CopyConstructible` (Table [[copyconstructible]]), and the copy
|
| 306 |
-
constructor of `D` shall not throw an exception. This `unique_ptr`
|
| 307 |
-
will hold a copy of `d`.
|
| 308 |
-
- Otherwise, `d` is a non-const rvalue and the second constructor of
|
| 309 |
-
this pair will be selected. `D` shall satisfy the requirements of
|
| 310 |
-
`MoveConstructible` (Table [[moveconstructible]]), and the move
|
| 311 |
-
constructor of `D` shall not throw an exception. This `unique_ptr`
|
| 312 |
-
will hold a value move constructed from `d`.
|
| 313 |
-
- Otherwise `D` is an lvalue-reference type. `d` shall be
|
| 314 |
-
reference-compatible with one of the constructors. If `d` is an
|
| 315 |
-
rvalue, it will bind to the second constructor of this pair and the
|
| 316 |
-
program is ill-formed. The diagnostic could be implemented using a
|
| 317 |
-
`static_assert` which assures that `D` is not a reference type. Else
|
| 318 |
-
`d` is an lvalue and will bind to the first constructor of this pair.
|
| 319 |
-
The type which `D` references need not be `CopyConstructible` nor
|
| 320 |
-
`MoveConstructible`. This `unique_ptr` will hold a `D` which refers to
|
| 321 |
-
the lvalue `d`. `D` may not be an rvalue-reference type.
|
| 322 |
-
|
| 323 |
*Effects:* Constructs a `unique_ptr` object which owns `p`, initializing
|
| 324 |
-
the stored pointer with `p` and initializing the deleter
|
| 325 |
-
|
|
|
|
|
|
|
|
|
|
| 326 |
|
| 327 |
*Postconditions:* `get() == p`. `get_deleter()` returns a reference to
|
| 328 |
the stored deleter. If `D` is a reference type then `get_deleter()`
|
| 329 |
returns a reference to the lvalue `d`.
|
| 330 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 331 |
``` cpp
|
| 332 |
D d;
|
| 333 |
unique_ptr<int, D> p1(new int, D()); // D must be MoveConstructible
|
| 334 |
unique_ptr<int, D> p2(new int, d); // D must be CopyConstructible
|
| 335 |
unique_ptr<int, D&> p3(new int, d); // p3 holds a reference to d
|
| 336 |
unique_ptr<int, const D&> p4(new int, D()); // error: rvalue deleter object combined
|
| 337 |
// with reference deleter type
|
| 338 |
```
|
| 339 |
|
|
|
|
|
|
|
| 340 |
``` cpp
|
| 341 |
unique_ptr(unique_ptr&& u) noexcept;
|
| 342 |
```
|
| 343 |
|
| 344 |
*Requires:* If `D` is not a reference type, `D` shall satisfy the
|
| 345 |
-
requirements of `MoveConstructible` (Table [[moveconstructible]]).
|
| 346 |
Construction of the deleter from an rvalue of type `D` shall not throw
|
| 347 |
an exception.
|
| 348 |
|
| 349 |
*Effects:* Constructs a `unique_ptr` by transferring ownership from `u`
|
| 350 |
to `*this`. If `D` is a reference type, this deleter is copy constructed
|
| 351 |
from `u`’s deleter; otherwise, this deleter is move constructed from
|
| 352 |
-
`u`’s deleter.
|
| 353 |
-
|
|
|
|
|
|
|
| 354 |
|
| 355 |
*Postconditions:* `get()` yields the value `u.get()` yielded before the
|
| 356 |
construction. `get_deleter()` returns a reference to the stored deleter
|
| 357 |
that was constructed from `u.get_deleter()`. If `D` is a reference type
|
| 358 |
then `get_deleter()` and `u.get_deleter()` both reference the same
|
|
@@ -377,42 +387,30 @@ unless:
|
|
| 377 |
is not a reference type and `E` is implicitly convertible to `D`.
|
| 378 |
|
| 379 |
*Effects:* Constructs a `unique_ptr` by transferring ownership from `u`
|
| 380 |
to `*this`. If `E` is a reference type, this deleter is copy constructed
|
| 381 |
from `u`’s deleter; otherwise, this deleter is move constructed from
|
| 382 |
-
`u`’s deleter.
|
| 383 |
-
|
|
|
|
|
|
|
| 384 |
|
| 385 |
*Postconditions:* `get()` yields the value `u.get()` yielded before the
|
| 386 |
construction. `get_deleter()` returns a reference to the stored deleter
|
| 387 |
that was constructed from `u.get_deleter()`.
|
| 388 |
|
| 389 |
-
``` cpp
|
| 390 |
-
template <class U>
|
| 391 |
-
unique_ptr(auto_ptr<U>&& u) noexcept;
|
| 392 |
-
```
|
| 393 |
-
|
| 394 |
-
*Effects:* Constructs a `unique_ptr` object, initializing the stored
|
| 395 |
-
pointer with `u.release()` and value-initializing the stored deleter.
|
| 396 |
-
|
| 397 |
-
*Postconditions:* `get()` yields the value `u.get()` yielded before the
|
| 398 |
-
construction. `u.get() == nullptr`. `get_deleter()` returns a reference
|
| 399 |
-
to the stored deleter.
|
| 400 |
-
|
| 401 |
-
*Remarks:* This constructor shall not participate in overload resolution
|
| 402 |
-
unless `U*` is implicitly convertible to `T*` and `D` is the same type
|
| 403 |
-
as `default_delete<T>`.
|
| 404 |
-
|
| 405 |
##### `unique_ptr` destructor <a id="unique.ptr.single.dtor">[[unique.ptr.single.dtor]]</a>
|
| 406 |
|
| 407 |
``` cpp
|
| 408 |
~unique_ptr();
|
| 409 |
```
|
| 410 |
|
| 411 |
*Requires:* The expression `get_deleter()(get())` shall be well formed,
|
| 412 |
-
shall have well-defined behavior, and shall not throw exceptions.
|
| 413 |
-
|
|
|
|
|
|
|
| 414 |
|
| 415 |
*Effects:* If `get() == nullptr` there are no effects. Otherwise
|
| 416 |
`get_deleter()(get())`.
|
| 417 |
|
| 418 |
##### `unique_ptr` assignment <a id="unique.ptr.single.asgn">[[unique.ptr.single.asgn]]</a>
|
|
@@ -420,11 +418,11 @@ use of `default_delete` requires `T` to be a complete type.
|
|
| 420 |
``` cpp
|
| 421 |
unique_ptr& operator=(unique_ptr&& u) noexcept;
|
| 422 |
```
|
| 423 |
|
| 424 |
*Requires:* If `D` is not a reference type, `D` shall satisfy the
|
| 425 |
-
requirements of `MoveAssignable` (Table [[moveassignable]]) and
|
| 426 |
assignment of the deleter from an rvalue of type `D` shall not throw an
|
| 427 |
exception. Otherwise, `D` is a reference type; `remove_reference_t<D>`
|
| 428 |
shall satisfy the `CopyAssignable` requirements and assignment of the
|
| 429 |
deleter from an lvalue of type `D` shall not throw an exception.
|
| 430 |
|
|
@@ -445,12 +443,14 @@ deleter from an lvalue of type `E` shall be well-formed and shall not
|
|
| 445 |
throw an exception.
|
| 446 |
|
| 447 |
*Remarks:* This operator shall not participate in overload resolution
|
| 448 |
unless:
|
| 449 |
|
| 450 |
-
- `unique_ptr<U, E>::pointer` is implicitly convertible to `pointer`
|
| 451 |
-
|
|
|
|
|
|
|
| 452 |
|
| 453 |
*Effects:* Transfers ownership from `u` to `*this` as if by calling
|
| 454 |
`reset(u.release())` followed by
|
| 455 |
`get_deleter() = std::forward<E>(u.get_deleter())`.
|
| 456 |
|
|
@@ -458,13 +458,13 @@ unless:
|
|
| 458 |
|
| 459 |
``` cpp
|
| 460 |
unique_ptr& operator=(nullptr_t) noexcept;
|
| 461 |
```
|
| 462 |
|
| 463 |
-
*Effects:* `reset()`.
|
| 464 |
|
| 465 |
-
`get() == nullptr`
|
| 466 |
|
| 467 |
*Returns:* `*this`.
|
| 468 |
|
| 469 |
##### `unique_ptr` observers <a id="unique.ptr.single.observers">[[unique.ptr.single.observers]]</a>
|
| 470 |
|
|
@@ -482,11 +482,12 @@ pointer operator->() const noexcept;
|
|
| 482 |
|
| 483 |
*Requires:* `get() != nullptr`.
|
| 484 |
|
| 485 |
*Returns:* `get()`.
|
| 486 |
|
| 487 |
-
*Note
|
|
|
|
| 488 |
|
| 489 |
``` cpp
|
| 490 |
pointer get() const noexcept;
|
| 491 |
```
|
| 492 |
|
|
@@ -509,29 +510,33 @@ explicit operator bool() const noexcept;
|
|
| 509 |
|
| 510 |
``` cpp
|
| 511 |
pointer release() noexcept;
|
| 512 |
```
|
| 513 |
|
| 514 |
-
`get() == nullptr`.
|
| 515 |
|
| 516 |
*Returns:* The value `get()` had at the start of the call to `release`.
|
| 517 |
|
| 518 |
``` cpp
|
| 519 |
void reset(pointer p = pointer()) noexcept;
|
| 520 |
```
|
| 521 |
|
| 522 |
*Requires:* The expression `get_deleter()(get())` shall be well formed,
|
| 523 |
shall have well-defined behavior, and shall not throw exceptions.
|
| 524 |
|
| 525 |
-
*Effects:*
|
| 526 |
-
of the stored pointer, `old_p`, was not equal to
|
| 527 |
-
`get_deleter()(old_p)`.
|
| 528 |
-
because the call to `get_deleter()` may destroy `*this`.
|
| 529 |
|
| 530 |
-
*
|
| 531 |
-
call to `get_deleter()`
|
| 532 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 533 |
|
| 534 |
``` cpp
|
| 535 |
void swap(unique_ptr& u) noexcept;
|
| 536 |
```
|
| 537 |
|
|
@@ -546,41 +551,44 @@ deleters of `*this` and `u`.
|
|
| 546 |
|
| 547 |
``` cpp
|
| 548 |
namespace std {
|
| 549 |
template <class T, class D> class unique_ptr<T[], D> {
|
| 550 |
public:
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
| 554 |
|
| 555 |
// [unique.ptr.runtime.ctor], constructors
|
| 556 |
constexpr unique_ptr() noexcept;
|
| 557 |
-
explicit unique_ptr(
|
| 558 |
-
unique_ptr(
|
| 559 |
-
unique_ptr(
|
| 560 |
unique_ptr(unique_ptr&& u) noexcept;
|
| 561 |
-
|
|
|
|
|
|
|
| 562 |
|
| 563 |
// destructor
|
| 564 |
~unique_ptr();
|
| 565 |
|
| 566 |
// assignment
|
| 567 |
unique_ptr& operator=(unique_ptr&& u) noexcept;
|
|
|
|
|
|
|
| 568 |
unique_ptr& operator=(nullptr_t) noexcept;
|
| 569 |
|
| 570 |
// [unique.ptr.runtime.observers], observers
|
| 571 |
T& operator[](size_t i) const;
|
| 572 |
pointer get() const noexcept;
|
| 573 |
deleter_type& get_deleter() noexcept;
|
| 574 |
const deleter_type& get_deleter() const noexcept;
|
| 575 |
explicit operator bool() const noexcept;
|
| 576 |
|
| 577 |
-
// [unique.ptr.runtime.modifiers] modifiers
|
| 578 |
pointer release() noexcept;
|
| 579 |
-
void reset(
|
| 580 |
-
void reset(nullptr_t) noexcept;
|
| 581 |
-
template <class U> void reset(U) = delete;
|
| 582 |
void swap(unique_ptr& u) noexcept;
|
| 583 |
|
| 584 |
// disable copy from lvalue
|
| 585 |
unique_ptr(const unique_ptr&) = delete;
|
| 586 |
unique_ptr& operator=(const unique_ptr&) = delete;
|
|
@@ -589,36 +597,92 @@ namespace std {
|
|
| 589 |
```
|
| 590 |
|
| 591 |
A specialization for array types is provided with a slightly altered
|
| 592 |
interface.
|
| 593 |
|
| 594 |
-
- Conversions between different types of `unique_ptr<T[], D>`
|
| 595 |
-
|
| 596 |
-
|
|
|
|
| 597 |
- Pointers to types derived from `T` are rejected by the constructors,
|
| 598 |
and by `reset`.
|
| 599 |
- The observers `operator*` and `operator->` are not provided.
|
| 600 |
- The indexing observer `operator[]` is provided.
|
| 601 |
- The default deleter will call `delete[]`.
|
| 602 |
|
| 603 |
-
Descriptions are provided below only for
|
| 604 |
-
|
| 605 |
|
| 606 |
The template argument `T` shall be a complete type.
|
| 607 |
|
| 608 |
##### `unique_ptr` constructors <a id="unique.ptr.runtime.ctor">[[unique.ptr.runtime.ctor]]</a>
|
| 609 |
|
| 610 |
``` cpp
|
| 611 |
-
explicit unique_ptr(
|
| 612 |
-
unique_ptr(pointer p, see below d) noexcept;
|
| 613 |
-
unique_ptr(pointer p, see below d) noexcept;
|
| 614 |
```
|
| 615 |
|
| 616 |
-
|
| 617 |
-
that
|
| 618 |
-
|
| 619 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 620 |
|
| 621 |
##### `unique_ptr` observers <a id="unique.ptr.runtime.observers">[[unique.ptr.runtime.observers]]</a>
|
| 622 |
|
| 623 |
``` cpp
|
| 624 |
T& operator[](size_t i) const;
|
|
@@ -630,15 +694,27 @@ stored pointer points.
|
|
| 630 |
*Returns:* `get()[i]`.
|
| 631 |
|
| 632 |
##### `unique_ptr` modifiers <a id="unique.ptr.runtime.modifiers">[[unique.ptr.runtime.modifiers]]</a>
|
| 633 |
|
| 634 |
``` cpp
|
| 635 |
-
void reset(nullptr_t p) noexcept;
|
| 636 |
```
|
| 637 |
|
| 638 |
*Effects:* Equivalent to `reset(pointer())`.
|
| 639 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 640 |
#### `unique_ptr` creation <a id="unique.ptr.create">[[unique.ptr.create]]</a>
|
| 641 |
|
| 642 |
``` cpp
|
| 643 |
template <class T, class... Args> unique_ptr<T> make_unique(Args&&... args);
|
| 644 |
```
|
|
@@ -668,10 +744,13 @@ unless `T` is an array of known bound.
|
|
| 668 |
|
| 669 |
``` cpp
|
| 670 |
template <class T, class D> void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
|
| 671 |
```
|
| 672 |
|
|
|
|
|
|
|
|
|
|
| 673 |
*Effects:* Calls `x.swap(y)`.
|
| 674 |
|
| 675 |
``` cpp
|
| 676 |
template <class T1, class D1, class T2, class D2>
|
| 677 |
bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
|
|
@@ -689,20 +768,26 @@ template <class T1, class D1, class T2, class D2>
|
|
| 689 |
``` cpp
|
| 690 |
template <class T1, class D1, class T2, class D2>
|
| 691 |
bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
|
| 692 |
```
|
| 693 |
|
| 694 |
-
*Requires:* Let `CT`
|
| 695 |
-
`unique_ptr<T2, D2>::pointer>::type`. Then the specialization `less<CT>`
|
| 696 |
-
shall be a function object type ([[function.objects]]) that induces a
|
| 697 |
-
strict weak ordering ([[alg.sorting]]) on the pointer values.
|
| 698 |
|
| 699 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 700 |
|
| 701 |
*Remarks:* If `unique_ptr<T1, D1>::pointer` is not implicitly
|
| 702 |
-
convertible to `CT` or `unique_ptr<T2, D2>::pointer` is not implicitly
|
| 703 |
-
convertible to `CT`, the program is ill-formed.
|
| 704 |
|
| 705 |
``` cpp
|
| 706 |
template <class T1, class D1, class T2, class D2>
|
| 707 |
bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
|
| 708 |
```
|
|
@@ -787,96 +872,99 @@ template <class T, class D>
|
|
| 787 |
*Returns:* The first function template returns `!(x < nullptr)`. The
|
| 788 |
second function template returns `!(nullptr < x)`.
|
| 789 |
|
| 790 |
### Shared-ownership pointers <a id="util.smartptr">[[util.smartptr]]</a>
|
| 791 |
|
| 792 |
-
#### Class `bad_weak_ptr` <a id="util.smartptr.
|
| 793 |
|
| 794 |
``` cpp
|
| 795 |
namespace std {
|
| 796 |
-
class bad_weak_ptr: public
|
| 797 |
public:
|
| 798 |
bad_weak_ptr() noexcept;
|
| 799 |
};
|
| 800 |
-
}
|
| 801 |
```
|
| 802 |
|
| 803 |
An exception of type `bad_weak_ptr` is thrown by the `shared_ptr`
|
| 804 |
constructor taking a `weak_ptr`.
|
| 805 |
|
| 806 |
``` cpp
|
| 807 |
bad_weak_ptr() noexcept;
|
| 808 |
```
|
| 809 |
|
| 810 |
-
*Postconditions:* `what()` returns
|
| 811 |
|
| 812 |
#### Class template `shared_ptr` <a id="util.smartptr.shared">[[util.smartptr.shared]]</a>
|
| 813 |
|
| 814 |
The `shared_ptr` class template stores a pointer, usually obtained via
|
| 815 |
`new`. `shared_ptr` implements semantics of shared ownership; the last
|
| 816 |
remaining owner of the pointer is responsible for destroying the object,
|
| 817 |
or otherwise releasing the resources associated with the stored pointer.
|
| 818 |
-
A `shared_ptr`
|
| 819 |
|
| 820 |
``` cpp
|
| 821 |
namespace std {
|
| 822 |
template<class T> class shared_ptr {
|
| 823 |
public:
|
| 824 |
-
|
|
|
|
| 825 |
|
| 826 |
-
// [util.smartptr.shared.const], constructors
|
| 827 |
constexpr shared_ptr() noexcept;
|
| 828 |
template<class Y> explicit shared_ptr(Y* p);
|
| 829 |
template<class Y, class D> shared_ptr(Y* p, D d);
|
| 830 |
template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
|
| 831 |
template <class D> shared_ptr(nullptr_t p, D d);
|
| 832 |
template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
|
| 833 |
-
template<class Y> shared_ptr(const shared_ptr<Y>& r,
|
| 834 |
shared_ptr(const shared_ptr& r) noexcept;
|
| 835 |
template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
|
| 836 |
shared_ptr(shared_ptr&& r) noexcept;
|
| 837 |
template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
|
| 838 |
template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
|
| 839 |
-
template<class Y> shared_ptr(auto_ptr<Y>&& r);
|
| 840 |
template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
|
| 841 |
-
constexpr shared_ptr(nullptr_t) : shared_ptr() { }
|
| 842 |
|
| 843 |
-
// [util.smartptr.shared.dest], destructor
|
| 844 |
~shared_ptr();
|
| 845 |
|
| 846 |
-
// [util.smartptr.shared.assign], assignment
|
| 847 |
shared_ptr& operator=(const shared_ptr& r) noexcept;
|
| 848 |
template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
|
| 849 |
shared_ptr& operator=(shared_ptr&& r) noexcept;
|
| 850 |
template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r) noexcept;
|
| 851 |
-
template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r);
|
| 852 |
template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
|
| 853 |
|
| 854 |
-
// [util.smartptr.shared.mod], modifiers
|
| 855 |
void swap(shared_ptr& r) noexcept;
|
| 856 |
void reset() noexcept;
|
| 857 |
template<class Y> void reset(Y* p);
|
| 858 |
template<class Y, class D> void reset(Y* p, D d);
|
| 859 |
template<class Y, class D, class A> void reset(Y* p, D d, A a);
|
| 860 |
|
| 861 |
-
// [util.smartptr.shared.obs], observers
|
| 862 |
-
|
| 863 |
T& operator*() const noexcept;
|
| 864 |
T* operator->() const noexcept;
|
|
|
|
| 865 |
long use_count() const noexcept;
|
| 866 |
-
bool unique() const noexcept;
|
| 867 |
explicit operator bool() const noexcept;
|
| 868 |
-
template<class U> bool owner_before(shared_ptr<U>
|
| 869 |
-
template<class U> bool owner_before(weak_ptr<U>
|
| 870 |
};
|
| 871 |
|
|
|
|
|
|
|
|
|
|
| 872 |
// [util.smartptr.shared.create], shared_ptr creation
|
| 873 |
-
template<class T, class... Args>
|
|
|
|
| 874 |
template<class T, class A, class... Args>
|
| 875 |
shared_ptr<T> allocate_shared(const A& a, Args&&... args);
|
| 876 |
|
| 877 |
-
// [util.smartptr.shared.cmp], shared_ptr comparisons
|
| 878 |
template<class T, class U>
|
| 879 |
bool operator==(const shared_ptr<T>& a, const shared_ptr<U>& b) noexcept;
|
| 880 |
template<class T, class U>
|
| 881 |
bool operator!=(const shared_ptr<T>& a, const shared_ptr<U>& b) noexcept;
|
| 882 |
template<class T, class U>
|
|
@@ -911,218 +999,252 @@ namespace std {
|
|
| 911 |
template <class T>
|
| 912 |
bool operator>=(const shared_ptr<T>& a, nullptr_t) noexcept;
|
| 913 |
template <class T>
|
| 914 |
bool operator>=(nullptr_t, const shared_ptr<T>& b) noexcept;
|
| 915 |
|
| 916 |
-
// [util.smartptr.shared.spec], shared_ptr specialized algorithms
|
| 917 |
-
template<class T>
|
|
|
|
| 918 |
|
| 919 |
-
// [util.smartptr.shared.cast], shared_ptr casts
|
| 920 |
template<class T, class U>
|
| 921 |
shared_ptr<T> static_pointer_cast(const shared_ptr<U>& r) noexcept;
|
| 922 |
template<class T, class U>
|
| 923 |
shared_ptr<T> dynamic_pointer_cast(const shared_ptr<U>& r) noexcept;
|
| 924 |
template<class T, class U>
|
| 925 |
shared_ptr<T> const_pointer_cast(const shared_ptr<U>& r) noexcept;
|
|
|
|
|
|
|
| 926 |
|
| 927 |
-
// [util.smartptr.getdeleter], shared_ptr get_deleter
|
| 928 |
-
template<class D, class T>
|
|
|
|
| 929 |
|
| 930 |
-
// [util.smartptr.shared.io], shared_ptr I/O
|
| 931 |
template<class E, class T, class Y>
|
| 932 |
basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, const shared_ptr<Y>& p);
|
| 933 |
-
}
|
| 934 |
```
|
| 935 |
|
| 936 |
Specializations of `shared_ptr` shall be `CopyConstructible`,
|
| 937 |
`CopyAssignable`, and `LessThanComparable`, allowing their use in
|
| 938 |
standard containers. Specializations of `shared_ptr` shall be
|
| 939 |
-
convertible to `bool`, allowing their use in boolean
|
| 940 |
-
declarations in conditions. The template parameter `T`
|
| 941 |
-
may be an incomplete type.
|
|
|
|
|
|
|
| 942 |
|
| 943 |
``` cpp
|
| 944 |
if (shared_ptr<X> px = dynamic_pointer_cast<X>(py)) {
|
| 945 |
// do something with px
|
| 946 |
}
|
| 947 |
```
|
| 948 |
|
|
|
|
|
|
|
| 949 |
For purposes of determining the presence of a data race, member
|
| 950 |
functions shall access and modify only the `shared_ptr` and `weak_ptr`
|
| 951 |
objects themselves and not objects they refer to. Changes in
|
| 952 |
`use_count()` do not reflect modifications that can introduce data
|
| 953 |
races.
|
| 954 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 955 |
##### `shared_ptr` constructors <a id="util.smartptr.shared.const">[[util.smartptr.shared.const]]</a>
|
| 956 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 957 |
``` cpp
|
| 958 |
constexpr shared_ptr() noexcept;
|
| 959 |
```
|
| 960 |
|
| 961 |
-
*Effects:* Constructs an
|
| 962 |
|
| 963 |
*Postconditions:* `use_count() == 0 && get() == nullptr`.
|
| 964 |
|
| 965 |
``` cpp
|
| 966 |
template<class Y> explicit shared_ptr(Y* p);
|
| 967 |
```
|
| 968 |
|
| 969 |
-
*Requires:* `
|
| 970 |
-
|
| 971 |
-
defined behavior, and shall not throw exceptions.
|
| 972 |
|
| 973 |
-
*Effects:*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 974 |
|
| 975 |
*Postconditions:* `use_count() == 1 && get() == p`.
|
| 976 |
|
| 977 |
*Throws:* `bad_alloc`, or an *implementation-defined* exception when a
|
| 978 |
resource other than memory could not be obtained.
|
| 979 |
|
| 980 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 981 |
|
| 982 |
``` cpp
|
| 983 |
template<class Y, class D> shared_ptr(Y* p, D d);
|
| 984 |
template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
|
| 985 |
template <class D> shared_ptr(nullptr_t p, D d);
|
| 986 |
template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
|
| 987 |
```
|
| 988 |
|
| 989 |
-
*Requires:* `
|
| 990 |
-
`
|
| 991 |
-
|
| 992 |
-
|
| 993 |
-
an allocator ([[allocator.requirements]]). The copy constructor and
|
| 994 |
-
destructor of `A` shall not throw exceptions.
|
| 995 |
|
| 996 |
-
*Effects:* Constructs a `shared_ptr` object that
|
| 997 |
-
|
| 998 |
-
|
|
|
|
|
|
|
| 999 |
|
| 1000 |
*Postconditions:* `use_count() == 1 && get() == p`.
|
| 1001 |
|
| 1002 |
*Throws:* `bad_alloc`, or an *implementation-defined* exception when a
|
| 1003 |
resource other than memory could not be obtained.
|
| 1004 |
|
| 1005 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1006 |
|
| 1007 |
``` cpp
|
| 1008 |
-
template<class Y> shared_ptr(const shared_ptr<Y>& r,
|
| 1009 |
```
|
| 1010 |
|
| 1011 |
-
*Effects:* Constructs a `shared_ptr` instance that stores `p` and
|
| 1012 |
-
|
| 1013 |
|
| 1014 |
-
*Postconditions:* `get() == p && use_count() == r.use_count()`
|
| 1015 |
|
| 1016 |
-
To avoid the possibility of a dangling pointer, the user of
|
| 1017 |
-
constructor must ensure that `p` remains valid at least until the
|
| 1018 |
-
ownership group of `r` is destroyed.
|
| 1019 |
|
| 1020 |
-
This constructor allows creation of an
|
| 1021 |
-
with a non-null stored pointer.
|
| 1022 |
|
| 1023 |
``` cpp
|
| 1024 |
shared_ptr(const shared_ptr& r) noexcept;
|
| 1025 |
template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
|
| 1026 |
```
|
| 1027 |
|
| 1028 |
-
The second constructor shall not participate in overload
|
| 1029 |
-
unless `Y*` is
|
| 1030 |
|
| 1031 |
-
*Effects:* If `r` is
|
| 1032 |
-
otherwise, constructs a `shared_ptr` object that
|
| 1033 |
`r`.
|
| 1034 |
|
| 1035 |
*Postconditions:* `get() == r.get() && use_count() == r.use_count()`.
|
| 1036 |
|
| 1037 |
``` cpp
|
| 1038 |
shared_ptr(shared_ptr&& r) noexcept;
|
| 1039 |
template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
|
| 1040 |
```
|
| 1041 |
|
| 1042 |
-
The second constructor shall not participate in overload
|
| 1043 |
-
unless `Y*` is
|
| 1044 |
|
| 1045 |
-
*Effects:* Move
|
| 1046 |
|
| 1047 |
*Postconditions:* `*this` shall contain the old value of `r`. `r` shall
|
| 1048 |
-
be
|
| 1049 |
|
| 1050 |
``` cpp
|
| 1051 |
template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
|
| 1052 |
```
|
| 1053 |
|
| 1054 |
-
*
|
| 1055 |
-
|
| 1056 |
-
|
| 1057 |
-
`r` and stores a copy of the pointer stored in `r`.
|
| 1058 |
|
| 1059 |
*Postconditions:* `use_count() == r.use_count()`.
|
| 1060 |
|
| 1061 |
*Throws:* `bad_weak_ptr` when `r.expired()`.
|
| 1062 |
|
| 1063 |
-
|
| 1064 |
-
|
| 1065 |
-
``` cpp
|
| 1066 |
-
template<class Y> shared_ptr(auto_ptr<Y>&& r);
|
| 1067 |
-
```
|
| 1068 |
-
|
| 1069 |
-
*Requires:* `r.release()` shall be convertible to `T*`. `Y` shall be a
|
| 1070 |
-
complete type. The expression `delete r.release()` shall be well formed,
|
| 1071 |
-
shall have well defined behavior, and shall not throw exceptions.
|
| 1072 |
-
|
| 1073 |
-
*Effects:* Constructs a `shared_ptr` object that stores and *owns*
|
| 1074 |
-
`r.release()`.
|
| 1075 |
-
|
| 1076 |
-
*Postconditions:* `use_count() == 1` `&&` `r.get() == nullptr`.
|
| 1077 |
-
|
| 1078 |
-
*Throws:* `bad_alloc`, or an *implementation-defined* exception when a
|
| 1079 |
-
resource other than memory could not be obtained.
|
| 1080 |
-
|
| 1081 |
-
If an exception is thrown, the constructor has no effect.
|
| 1082 |
|
| 1083 |
``` cpp
|
| 1084 |
template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
|
| 1085 |
```
|
| 1086 |
|
| 1087 |
-
*
|
| 1088 |
-
`
|
| 1089 |
-
|
| 1090 |
|
| 1091 |
-
If
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1092 |
|
| 1093 |
##### `shared_ptr` destructor <a id="util.smartptr.shared.dest">[[util.smartptr.shared.dest]]</a>
|
| 1094 |
|
| 1095 |
``` cpp
|
| 1096 |
~shared_ptr();
|
| 1097 |
```
|
| 1098 |
|
| 1099 |
*Effects:*
|
| 1100 |
|
| 1101 |
-
- If `*this` is
|
| 1102 |
instance (`use_count() > 1`), there are no side effects.
|
| 1103 |
-
- Otherwise, if `*this`
|
| 1104 |
-
|
| 1105 |
-
- Otherwise, `*this`
|
| 1106 |
|
| 1107 |
-
Since the destruction of `*this` decreases the number of
|
| 1108 |
-
share ownership with `*this` by one, after `*this` has
|
| 1109 |
-
all `shared_ptr` instances that shared ownership with
|
| 1110 |
-
report a `use_count()` that is one less than its previous
|
|
|
|
| 1111 |
|
| 1112 |
##### `shared_ptr` assignment <a id="util.smartptr.shared.assign">[[util.smartptr.shared.assign]]</a>
|
| 1113 |
|
| 1114 |
``` cpp
|
| 1115 |
shared_ptr& operator=(const shared_ptr& r) noexcept;
|
| 1116 |
template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
|
| 1117 |
-
template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r);
|
| 1118 |
```
|
| 1119 |
|
| 1120 |
*Effects:* Equivalent to `shared_ptr(r).swap(*this)`.
|
| 1121 |
|
| 1122 |
*Returns:* `*this`.
|
| 1123 |
|
|
|
|
|
|
|
| 1124 |
The use count updates caused by the temporary object construction and
|
| 1125 |
destruction are not observable side effects, so the implementation may
|
| 1126 |
meet the effects (and the implied guarantees) via different means,
|
| 1127 |
without creating a temporary. In particular, in the example:
|
| 1128 |
|
|
@@ -1133,10 +1255,12 @@ p = p;
|
|
| 1133 |
q = p;
|
| 1134 |
```
|
| 1135 |
|
| 1136 |
both assignments may be no-ops.
|
| 1137 |
|
|
|
|
|
|
|
| 1138 |
``` cpp
|
| 1139 |
shared_ptr& operator=(shared_ptr&& r) noexcept;
|
| 1140 |
template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r) noexcept;
|
| 1141 |
```
|
| 1142 |
|
|
@@ -1148,11 +1272,11 @@ template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r) noexcept;
|
|
| 1148 |
template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
|
| 1149 |
```
|
| 1150 |
|
| 1151 |
*Effects:* Equivalent to `shared_ptr(std::move(r)).swap(*this)`.
|
| 1152 |
|
| 1153 |
-
*Returns:* `*this`
|
| 1154 |
|
| 1155 |
##### `shared_ptr` modifiers <a id="util.smartptr.shared.mod">[[util.smartptr.shared.mod]]</a>
|
| 1156 |
|
| 1157 |
``` cpp
|
| 1158 |
void swap(shared_ptr& r) noexcept;
|
|
@@ -1185,64 +1309,87 @@ template<class Y, class D, class A> void reset(Y* p, D d, A a);
|
|
| 1185 |
*Effects:* Equivalent to `shared_ptr(p, d, a).swap(*this)`.
|
| 1186 |
|
| 1187 |
##### `shared_ptr` observers <a id="util.smartptr.shared.obs">[[util.smartptr.shared.obs]]</a>
|
| 1188 |
|
| 1189 |
``` cpp
|
| 1190 |
-
|
| 1191 |
```
|
| 1192 |
|
| 1193 |
-
*Returns:*
|
| 1194 |
|
| 1195 |
``` cpp
|
| 1196 |
T& operator*() const noexcept;
|
| 1197 |
```
|
| 1198 |
|
| 1199 |
*Requires:* `get() != 0`.
|
| 1200 |
|
| 1201 |
*Returns:* `*get()`.
|
| 1202 |
|
| 1203 |
-
*Remarks:* When `T` is `void`, it is unspecified
|
| 1204 |
-
function is declared. If it is declared, it is
|
| 1205 |
-
return type is, except that the declaration
|
| 1206 |
-
the definition) of the function shall be well
|
|
|
|
| 1207 |
|
| 1208 |
``` cpp
|
| 1209 |
T* operator->() const noexcept;
|
| 1210 |
```
|
| 1211 |
|
| 1212 |
*Requires:* `get() != 0`.
|
| 1213 |
|
| 1214 |
*Returns:* `get()`.
|
| 1215 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1216 |
``` cpp
|
| 1217 |
long use_count() const noexcept;
|
| 1218 |
```
|
| 1219 |
|
| 1220 |
-
*Returns:*
|
| 1221 |
-
|
| 1222 |
|
| 1223 |
-
|
| 1224 |
|
| 1225 |
-
``
|
| 1226 |
-
|
| 1227 |
-
```
|
| 1228 |
|
| 1229 |
-
*
|
|
|
|
| 1230 |
|
| 1231 |
-
|
| 1232 |
-
|
| 1233 |
-
`
|
|
|
|
|
|
|
| 1234 |
|
| 1235 |
``` cpp
|
| 1236 |
explicit operator bool() const noexcept;
|
| 1237 |
```
|
| 1238 |
|
| 1239 |
*Returns:* `get() != 0`.
|
| 1240 |
|
| 1241 |
``` cpp
|
| 1242 |
-
template<class U> bool owner_before(shared_ptr<U>
|
| 1243 |
-
template<class U> bool owner_before(weak_ptr<U>
|
| 1244 |
```
|
| 1245 |
|
| 1246 |
*Returns:* An unspecified value such that
|
| 1247 |
|
| 1248 |
- `x.owner_before(y)` defines a strict weak ordering as defined
|
|
@@ -1253,59 +1400,66 @@ template<class U> bool owner_before(weak_ptr<U> const& b) const;
|
|
| 1253 |
ownership or are both empty.
|
| 1254 |
|
| 1255 |
##### `shared_ptr` creation <a id="util.smartptr.shared.create">[[util.smartptr.shared.create]]</a>
|
| 1256 |
|
| 1257 |
``` cpp
|
| 1258 |
-
template<class T, class... Args>
|
|
|
|
| 1259 |
template<class T, class A, class... Args>
|
| 1260 |
shared_ptr<T> allocate_shared(const A& a, Args&&... args);
|
| 1261 |
```
|
| 1262 |
|
| 1263 |
*Requires:* The expression `::new (pv) T(std::forward<Args>(args)...)`,
|
| 1264 |
where `pv` has type `void*` and points to storage suitable to hold an
|
| 1265 |
object of type `T`, shall be well formed. `A` shall be an
|
| 1266 |
-
|
| 1267 |
destructor of `A` shall not throw exceptions.
|
| 1268 |
|
| 1269 |
*Effects:* Allocates memory suitable for an object of type `T` and
|
| 1270 |
-
constructs an object in that memory via the placement new
|
| 1271 |
`::new (pv) T(std::forward<Args>(args)...)`. The template
|
| 1272 |
`allocate_shared` uses a copy of `a` to allocate memory. If an exception
|
| 1273 |
is thrown, the functions have no effect.
|
| 1274 |
|
| 1275 |
*Returns:* A `shared_ptr` instance that stores and owns the address of
|
| 1276 |
the newly constructed object of type `T`.
|
| 1277 |
|
| 1278 |
-
*Postconditions:* `get() != 0 && use_count() == 1`
|
| 1279 |
|
| 1280 |
*Throws:* `bad_alloc`, or an exception thrown from `A::allocate` or from
|
| 1281 |
the constructor of `T`.
|
| 1282 |
|
| 1283 |
-
*Remarks:*
|
| 1284 |
-
|
| 1285 |
-
|
|
|
|
| 1286 |
|
| 1287 |
-
|
| 1288 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1289 |
|
| 1290 |
##### `shared_ptr` comparison <a id="util.smartptr.shared.cmp">[[util.smartptr.shared.cmp]]</a>
|
| 1291 |
|
| 1292 |
``` cpp
|
| 1293 |
-
template<class T, class U>
|
|
|
|
| 1294 |
```
|
| 1295 |
|
| 1296 |
*Returns:* `a.get() == b.get()`.
|
| 1297 |
|
| 1298 |
``` cpp
|
| 1299 |
-
template<class T, class U>
|
|
|
|
| 1300 |
```
|
| 1301 |
|
| 1302 |
-
*Returns:* `less<
|
| 1303 |
-
pointer type (Clause [[expr]]) of `T*` and `U*`.
|
| 1304 |
|
| 1305 |
-
Defining a comparison
|
| 1306 |
-
keys in associative containers.
|
| 1307 |
|
| 1308 |
``` cpp
|
| 1309 |
template <class T>
|
| 1310 |
bool operator==(const shared_ptr<T>& a, nullptr_t) noexcept;
|
| 1311 |
template <class T>
|
|
@@ -1329,12 +1483,13 @@ template <class T>
|
|
| 1329 |
template <class T>
|
| 1330 |
bool operator<(nullptr_t, const shared_ptr<T>& a) noexcept;
|
| 1331 |
```
|
| 1332 |
|
| 1333 |
*Returns:* The first function template returns
|
| 1334 |
-
`less<T*>()(a.get(), nullptr)`. The second
|
| 1335 |
-
|
|
|
|
| 1336 |
|
| 1337 |
``` cpp
|
| 1338 |
template <class T>
|
| 1339 |
bool operator>(const shared_ptr<T>& a, nullptr_t) noexcept;
|
| 1340 |
template <class T>
|
|
@@ -1365,94 +1520,118 @@ template <class T>
|
|
| 1365 |
second function template returns `!(nullptr < a)`.
|
| 1366 |
|
| 1367 |
##### `shared_ptr` specialized algorithms <a id="util.smartptr.shared.spec">[[util.smartptr.shared.spec]]</a>
|
| 1368 |
|
| 1369 |
``` cpp
|
| 1370 |
-
template<class T>
|
|
|
|
| 1371 |
```
|
| 1372 |
|
| 1373 |
*Effects:* Equivalent to `a.swap(b)`.
|
| 1374 |
|
| 1375 |
##### `shared_ptr` casts <a id="util.smartptr.shared.cast">[[util.smartptr.shared.cast]]</a>
|
| 1376 |
|
| 1377 |
``` cpp
|
| 1378 |
-
template<class T, class U>
|
|
|
|
| 1379 |
```
|
| 1380 |
|
| 1381 |
-
*Requires:* The expression `static_cast<T*>(
|
| 1382 |
formed.
|
| 1383 |
|
| 1384 |
-
*Returns:*
|
| 1385 |
-
`shared_ptr<T>` object that stores `static_cast<T*>(r.get())` and
|
| 1386 |
-
*shares ownership* with `r`.
|
| 1387 |
|
| 1388 |
-
|
| 1389 |
-
|
|
|
|
| 1390 |
|
| 1391 |
-
The seemingly equivalent expression
|
| 1392 |
`shared_ptr<T>(static_cast<T*>(r.get()))` will eventually result in
|
| 1393 |
-
undefined behavior, attempting to delete the same object
|
|
|
|
| 1394 |
|
| 1395 |
``` cpp
|
| 1396 |
-
template<class T, class U>
|
|
|
|
| 1397 |
```
|
| 1398 |
|
| 1399 |
-
*Requires:* The expression `dynamic_cast<T*>(
|
| 1400 |
formed and shall have well defined behavior.
|
| 1401 |
|
| 1402 |
*Returns:*
|
| 1403 |
|
| 1404 |
-
- When `dynamic_cast<T*>(r.get())`
|
| 1405 |
-
|
| 1406 |
-
|
| 1407 |
-
- Otherwise, an *empty* `shared_ptr<T>` object.
|
| 1408 |
|
| 1409 |
-
|
| 1410 |
-
|
| 1411 |
-
The seemingly equivalent expression
|
| 1412 |
`shared_ptr<T>(dynamic_cast<T*>(r.get()))` will eventually result in
|
| 1413 |
-
undefined behavior, attempting to delete the same object
|
|
|
|
| 1414 |
|
| 1415 |
``` cpp
|
| 1416 |
-
template<class T, class U>
|
|
|
|
| 1417 |
```
|
| 1418 |
|
| 1419 |
-
*Requires:* The expression `const_cast<T*>(
|
| 1420 |
-
formed.
|
| 1421 |
|
| 1422 |
-
*Returns:*
|
| 1423 |
-
`shared_ptr<T>` object that stores `const_cast<T*>(r.get())` and shares
|
| 1424 |
-
ownership with `r`.
|
| 1425 |
|
| 1426 |
-
|
| 1427 |
-
|
|
|
|
| 1428 |
|
| 1429 |
-
The seemingly equivalent expression
|
| 1430 |
`shared_ptr<T>(const_cast<T*>(r.get()))` will eventually result in
|
| 1431 |
-
undefined behavior, attempting to delete the same object
|
|
|
|
| 1432 |
|
| 1433 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1434 |
|
| 1435 |
``` cpp
|
| 1436 |
-
template<class D, class T>
|
|
|
|
| 1437 |
```
|
| 1438 |
|
| 1439 |
-
*Returns:* If `p`
|
| 1440 |
-
|
| 1441 |
-
valid as long as there exists a `shared_ptr` instance that owns
|
| 1442 |
-
|
| 1443 |
-
|
| 1444 |
-
|
|
|
|
|
|
|
|
|
|
| 1445 |
|
| 1446 |
##### `shared_ptr` I/O <a id="util.smartptr.shared.io">[[util.smartptr.shared.io]]</a>
|
| 1447 |
|
| 1448 |
``` cpp
|
| 1449 |
template<class E, class T, class Y>
|
| 1450 |
-
basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y>
|
| 1451 |
```
|
| 1452 |
|
| 1453 |
-
*Effects:* `os << p.get();`
|
| 1454 |
|
| 1455 |
*Returns:* `os`.
|
| 1456 |
|
| 1457 |
#### Class template `weak_ptr` <a id="util.smartptr.weak">[[util.smartptr.weak]]</a>
|
| 1458 |
|
|
@@ -1462,27 +1641,27 @@ can be converted to a `shared_ptr` using the member function `lock`.
|
|
| 1462 |
|
| 1463 |
``` cpp
|
| 1464 |
namespace std {
|
| 1465 |
template<class T> class weak_ptr {
|
| 1466 |
public:
|
| 1467 |
-
|
| 1468 |
|
| 1469 |
// [util.smartptr.weak.const], constructors
|
| 1470 |
constexpr weak_ptr() noexcept;
|
| 1471 |
-
template<class Y> weak_ptr(shared_ptr<Y>
|
| 1472 |
-
weak_ptr(
|
| 1473 |
-
template<class Y> weak_ptr(weak_ptr<Y>
|
| 1474 |
weak_ptr(weak_ptr&& r) noexcept;
|
| 1475 |
template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept;
|
| 1476 |
|
| 1477 |
// [util.smartptr.weak.dest], destructor
|
| 1478 |
~weak_ptr();
|
| 1479 |
|
| 1480 |
// [util.smartptr.weak.assign], assignment
|
| 1481 |
-
weak_ptr& operator=(
|
| 1482 |
-
template<class Y> weak_ptr& operator=(weak_ptr<Y>
|
| 1483 |
-
template<class Y> weak_ptr& operator=(shared_ptr<Y>
|
| 1484 |
weak_ptr& operator=(weak_ptr&& r) noexcept;
|
| 1485 |
template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept;
|
| 1486 |
|
| 1487 |
// [util.smartptr.weak.mod], modifiers
|
| 1488 |
void swap(weak_ptr& r) noexcept;
|
|
@@ -1490,17 +1669,20 @@ namespace std {
|
|
| 1490 |
|
| 1491 |
// [util.smartptr.weak.obs], observers
|
| 1492 |
long use_count() const noexcept;
|
| 1493 |
bool expired() const noexcept;
|
| 1494 |
shared_ptr<T> lock() const noexcept;
|
| 1495 |
-
template<class U> bool owner_before(shared_ptr<U>
|
| 1496 |
-
template<class U> bool owner_before(weak_ptr<U>
|
| 1497 |
};
|
| 1498 |
|
|
|
|
|
|
|
|
|
|
| 1499 |
// [util.smartptr.weak.spec], specialized algorithms
|
| 1500 |
template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
|
| 1501 |
-
}
|
| 1502 |
```
|
| 1503 |
|
| 1504 |
Specializations of `weak_ptr` shall be `CopyConstructible` and
|
| 1505 |
`CopyAssignable`, allowing their use in standard containers. The
|
| 1506 |
template parameter `T` of `weak_ptr` may be an incomplete type.
|
|
@@ -1509,41 +1691,41 @@ template parameter `T` of `weak_ptr` may be an incomplete type.
|
|
| 1509 |
|
| 1510 |
``` cpp
|
| 1511 |
constexpr weak_ptr() noexcept;
|
| 1512 |
```
|
| 1513 |
|
| 1514 |
-
*Effects:* Constructs an
|
| 1515 |
|
| 1516 |
*Postconditions:* `use_count() == 0`.
|
| 1517 |
|
| 1518 |
``` cpp
|
| 1519 |
weak_ptr(const weak_ptr& r) noexcept;
|
| 1520 |
template<class Y> weak_ptr(const weak_ptr<Y>& r) noexcept;
|
| 1521 |
template<class Y> weak_ptr(const shared_ptr<Y>& r) noexcept;
|
| 1522 |
```
|
| 1523 |
|
| 1524 |
-
The second and third constructors shall not participate in
|
| 1525 |
-
resolution unless `Y*` is
|
| 1526 |
|
| 1527 |
-
*Effects:* If `r` is
|
| 1528 |
-
otherwise, constructs a `weak_ptr` object that
|
| 1529 |
-
|
| 1530 |
|
| 1531 |
*Postconditions:* `use_count() == r.use_count()`.
|
| 1532 |
|
| 1533 |
``` cpp
|
| 1534 |
weak_ptr(weak_ptr&& r) noexcept;
|
| 1535 |
template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept;
|
| 1536 |
```
|
| 1537 |
|
| 1538 |
-
The second constructor shall not participate in overload
|
| 1539 |
-
unless `Y*` is
|
| 1540 |
|
| 1541 |
-
*Effects:* Move
|
| 1542 |
|
| 1543 |
*Postconditions:* `*this` shall contain the old value of `r`. `r` shall
|
| 1544 |
-
be
|
| 1545 |
|
| 1546 |
##### `weak_ptr` destructor <a id="util.smartptr.weak.dest">[[util.smartptr.weak.dest]]</a>
|
| 1547 |
|
| 1548 |
``` cpp
|
| 1549 |
~weak_ptr();
|
|
@@ -1594,33 +1776,29 @@ void reset() noexcept;
|
|
| 1594 |
|
| 1595 |
``` cpp
|
| 1596 |
long use_count() const noexcept;
|
| 1597 |
```
|
| 1598 |
|
| 1599 |
-
*Returns:* `0` if `*this` is
|
| 1600 |
-
`shared_ptr` instances that
|
| 1601 |
-
|
| 1602 |
-
`use_count()` is not necessarily efficient.
|
| 1603 |
|
| 1604 |
``` cpp
|
| 1605 |
bool expired() const noexcept;
|
| 1606 |
```
|
| 1607 |
|
| 1608 |
*Returns:* `use_count() == 0`.
|
| 1609 |
|
| 1610 |
-
`expired()` may be faster than `use_count()`.
|
| 1611 |
-
|
| 1612 |
``` cpp
|
| 1613 |
shared_ptr<T> lock() const noexcept;
|
| 1614 |
```
|
| 1615 |
|
| 1616 |
-
*Returns:* `expired() ?
|
| 1617 |
executed atomically.
|
| 1618 |
|
| 1619 |
``` cpp
|
| 1620 |
-
template<class U> bool owner_before(shared_ptr<U>
|
| 1621 |
-
template<class U> bool owner_before(weak_ptr<U>
|
| 1622 |
```
|
| 1623 |
|
| 1624 |
*Returns:* An unspecified value such that
|
| 1625 |
|
| 1626 |
- `x.owner_before(y)` defines a strict weak ordering as defined
|
|
@@ -1631,11 +1809,12 @@ template<class U> bool owner_before(weak_ptr<U> const& b) const;
|
|
| 1631 |
ownership or are both empty.
|
| 1632 |
|
| 1633 |
##### `weak_ptr` specialized algorithms <a id="util.smartptr.weak.spec">[[util.smartptr.weak.spec]]</a>
|
| 1634 |
|
| 1635 |
``` cpp
|
| 1636 |
-
template<class T>
|
|
|
|
| 1637 |
```
|
| 1638 |
|
| 1639 |
*Effects:* Equivalent to `a.swap(b)`.
|
| 1640 |
|
| 1641 |
#### Class template `owner_less` <a id="util.smartptr.ownerless">[[util.smartptr.ownerless]]</a>
|
|
@@ -1643,130 +1822,125 @@ template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
|
|
| 1643 |
The class template `owner_less` allows ownership-based mixed comparisons
|
| 1644 |
of shared and weak pointers.
|
| 1645 |
|
| 1646 |
``` cpp
|
| 1647 |
namespace std {
|
| 1648 |
-
template<class T> struct owner_less;
|
| 1649 |
|
| 1650 |
template<class T> struct owner_less<shared_ptr<T>> {
|
| 1651 |
-
|
| 1652 |
-
|
| 1653 |
-
|
| 1654 |
-
bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const;
|
| 1655 |
-
bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
|
| 1656 |
-
bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
|
| 1657 |
};
|
| 1658 |
|
| 1659 |
template<class T> struct owner_less<weak_ptr<T>> {
|
| 1660 |
-
|
| 1661 |
-
|
| 1662 |
-
|
| 1663 |
-
|
| 1664 |
-
|
| 1665 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1666 |
};
|
| 1667 |
}
|
| 1668 |
```
|
| 1669 |
|
| 1670 |
-
`operator()(x,y)` shall return `x.owner_before(y)`.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1671 |
|
| 1672 |
- `operator()` defines a strict weak ordering as defined in
|
| 1673 |
[[alg.sorting]];
|
| 1674 |
- under the equivalence relation defined by `operator()`,
|
| 1675 |
`!operator()(a, b) && !operator()(b, a)`, two `shared_ptr` or
|
| 1676 |
`weak_ptr` instances are equivalent if and only if they share
|
| 1677 |
ownership or are both empty.
|
| 1678 |
|
|
|
|
|
|
|
| 1679 |
#### Class template `enable_shared_from_this` <a id="util.smartptr.enab">[[util.smartptr.enab]]</a>
|
| 1680 |
|
| 1681 |
A class `T` can inherit from `enable_shared_from_this<T>` to inherit the
|
| 1682 |
-
`shared_from_this` member functions that obtain a
|
| 1683 |
pointing to `*this`.
|
| 1684 |
|
|
|
|
|
|
|
| 1685 |
``` cpp
|
| 1686 |
-
struct X: public enable_shared_from_this<X> {
|
| 1687 |
-
};
|
| 1688 |
|
| 1689 |
int main() {
|
| 1690 |
shared_ptr<X> p(new X);
|
| 1691 |
shared_ptr<X> q = p->shared_from_this();
|
| 1692 |
assert(p == q);
|
| 1693 |
-
assert(!
|
| 1694 |
}
|
| 1695 |
```
|
| 1696 |
|
|
|
|
|
|
|
| 1697 |
``` cpp
|
| 1698 |
namespace std {
|
| 1699 |
template<class T> class enable_shared_from_this {
|
| 1700 |
protected:
|
| 1701 |
constexpr enable_shared_from_this() noexcept;
|
| 1702 |
-
enable_shared_from_this(
|
| 1703 |
-
enable_shared_from_this& operator=(
|
| 1704 |
~enable_shared_from_this();
|
| 1705 |
public:
|
| 1706 |
shared_ptr<T> shared_from_this();
|
| 1707 |
shared_ptr<T const> shared_from_this() const;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1708 |
};
|
| 1709 |
-
}
|
| 1710 |
```
|
| 1711 |
|
| 1712 |
The template parameter `T` of `enable_shared_from_this` may be an
|
| 1713 |
incomplete type.
|
| 1714 |
|
| 1715 |
``` cpp
|
| 1716 |
constexpr enable_shared_from_this() noexcept;
|
| 1717 |
enable_shared_from_this(const enable_shared_from_this<T>&) noexcept;
|
| 1718 |
```
|
| 1719 |
|
| 1720 |
-
*Effects:*
|
| 1721 |
|
| 1722 |
``` cpp
|
| 1723 |
enable_shared_from_this<T>& operator=(const enable_shared_from_this<T>&) noexcept;
|
| 1724 |
```
|
| 1725 |
|
| 1726 |
*Returns:* `*this`.
|
| 1727 |
|
| 1728 |
-
``
|
| 1729 |
-
~enable_shared_from_this();
|
| 1730 |
-
```
|
| 1731 |
-
|
| 1732 |
-
*Effects:* Destroys `*this`.
|
| 1733 |
|
| 1734 |
``` cpp
|
| 1735 |
shared_ptr<T> shared_from_this();
|
| 1736 |
shared_ptr<T const> shared_from_this() const;
|
| 1737 |
```
|
| 1738 |
|
| 1739 |
-
*
|
| 1740 |
-
class of `T`. `*this` shall be a subobject of an object `t` of type `T`.
|
| 1741 |
-
There shall be at least one `shared_ptr` instance `p` that *owns* `&t`.
|
| 1742 |
-
|
| 1743 |
-
*Returns:* A `shared_ptr<T>` object `r` that *shares ownership with*
|
| 1744 |
-
`p`.
|
| 1745 |
-
|
| 1746 |
-
*Postconditions:* `r.get() == this`.
|
| 1747 |
-
|
| 1748 |
-
A possible implementation is shown below:
|
| 1749 |
|
| 1750 |
``` cpp
|
| 1751 |
-
|
| 1752 |
-
|
| 1753 |
-
weak_ptr<T> __weak_this;
|
| 1754 |
-
protected:
|
| 1755 |
-
constexpr enable_shared_from_this() : __weak_this() { }
|
| 1756 |
-
enable_shared_from_this(enable_shared_from_this const &) { }
|
| 1757 |
-
enable_shared_from_this& operator=(enable_shared_from_this const &) { return *this; }
|
| 1758 |
-
~enable_shared_from_this() { }
|
| 1759 |
-
public:
|
| 1760 |
-
shared_ptr<T> shared_from_this() { return shared_ptr<T>(__weak_this); }
|
| 1761 |
-
shared_ptr<T const> shared_from_this() const { return shared_ptr<T const>(__weak_this); }
|
| 1762 |
-
};
|
| 1763 |
```
|
| 1764 |
|
| 1765 |
-
|
| 1766 |
-
presence of an `enable_shared_from_this` base and assign the newly
|
| 1767 |
-
created `shared_ptr` to its `__weak_this` member.
|
| 1768 |
|
| 1769 |
#### `shared_ptr` atomic access <a id="util.smartptr.shared.atomic">[[util.smartptr.shared.atomic]]</a>
|
| 1770 |
|
| 1771 |
Concurrent access to a `shared_ptr` object from multiple threads does
|
| 1772 |
not introduce a data race if the access is done exclusively via the
|
|
@@ -1818,11 +1992,11 @@ template<class T>
|
|
| 1818 |
void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
|
| 1819 |
```
|
| 1820 |
|
| 1821 |
*Requires:* `p` shall not be null.
|
| 1822 |
|
| 1823 |
-
*Effects:* `atomic_store_explicit(p, r, memory_order_seq_cst)`.
|
| 1824 |
|
| 1825 |
*Throws:* Nothing.
|
| 1826 |
|
| 1827 |
``` cpp
|
| 1828 |
template<class T>
|
|
@@ -1832,11 +2006,11 @@ template<class T>
|
|
| 1832 |
*Requires:* `p` shall not be null.
|
| 1833 |
|
| 1834 |
*Requires:* `mo` shall not be `memory_order_acquire` or
|
| 1835 |
`memory_order_acq_rel`.
|
| 1836 |
|
| 1837 |
-
*Effects:* `p->swap(r)`.
|
| 1838 |
|
| 1839 |
*Throws:* Nothing.
|
| 1840 |
|
| 1841 |
``` cpp
|
| 1842 |
template<class T>
|
|
@@ -1849,43 +2023,46 @@ template<class T>
|
|
| 1849 |
|
| 1850 |
*Throws:* Nothing.
|
| 1851 |
|
| 1852 |
``` cpp
|
| 1853 |
template<class T>
|
| 1854 |
-
shared_ptr<T> atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r,
|
| 1855 |
-
memory_order mo);
|
| 1856 |
```
|
| 1857 |
|
| 1858 |
*Requires:* `p` shall not be null.
|
| 1859 |
|
| 1860 |
-
*Effects:* `p->swap(r)`.
|
| 1861 |
|
| 1862 |
*Returns:* The previous value of `*p`.
|
| 1863 |
|
| 1864 |
*Throws:* Nothing.
|
| 1865 |
|
| 1866 |
``` cpp
|
| 1867 |
template<class T>
|
| 1868 |
-
bool atomic_compare_exchange_weak(
|
| 1869 |
-
shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
|
| 1870 |
```
|
| 1871 |
|
| 1872 |
*Requires:* `p` shall not be null and `v` shall not be null.
|
| 1873 |
|
| 1874 |
*Returns:*
|
| 1875 |
-
|
|
|
|
|
|
|
|
|
|
| 1876 |
|
| 1877 |
*Throws:* Nothing.
|
| 1878 |
|
| 1879 |
``` cpp
|
| 1880 |
template<class T>
|
| 1881 |
-
bool atomic_compare_exchange_strong(
|
| 1882 |
-
shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
|
| 1883 |
```
|
| 1884 |
|
| 1885 |
-
*Returns:*
|
| 1886 |
-
|
|
|
|
|
|
|
|
|
|
| 1887 |
|
| 1888 |
``` cpp
|
| 1889 |
template<class T>
|
| 1890 |
bool atomic_compare_exchange_weak_explicit(
|
| 1891 |
shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w,
|
|
@@ -1894,49 +2071,43 @@ template<class T>
|
|
| 1894 |
bool atomic_compare_exchange_strong_explicit(
|
| 1895 |
shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w,
|
| 1896 |
memory_order success, memory_order failure);
|
| 1897 |
```
|
| 1898 |
|
| 1899 |
-
*Requires:* `p` shall not be null and `v` shall not be null.
|
| 1900 |
-
|
| 1901 |
-
|
| 1902 |
-
`memory_order_acq_rel`, or stronger than `success`.
|
| 1903 |
|
| 1904 |
*Effects:* If `*p` is equivalent to `*v`, assigns `w` to `*p` and has
|
| 1905 |
synchronization semantics corresponding to the value of `success`,
|
| 1906 |
otherwise assigns `*p` to `*v` and has synchronization semantics
|
| 1907 |
corresponding to the value of `failure`.
|
| 1908 |
|
| 1909 |
*Returns:* `true` if `*p` was equivalent to `*v`, `false` otherwise.
|
| 1910 |
|
| 1911 |
*Throws:* Nothing.
|
| 1912 |
|
| 1913 |
-
*Remarks:*
|
| 1914 |
-
same pointer value and share ownership.
|
| 1915 |
-
|
| 1916 |
-
*Remarks:* the weak forms may fail spuriously.
|
| 1917 |
-
See [[atomics.types.operations]].
|
| 1918 |
|
| 1919 |
#### Smart pointer hash support <a id="util.smartptr.hash">[[util.smartptr.hash]]</a>
|
| 1920 |
|
| 1921 |
``` cpp
|
| 1922 |
template <class T, class D> struct hash<unique_ptr<T, D>>;
|
| 1923 |
```
|
| 1924 |
|
| 1925 |
-
|
| 1926 |
-
|
| 1927 |
-
`
|
| 1928 |
-
|
| 1929 |
-
|
| 1930 |
-
|
| 1931 |
-
well-formed and well-defined, and shall meet the requirements of class
|
| 1932 |
-
template `hash` ([[unord.hash]]).
|
| 1933 |
|
| 1934 |
``` cpp
|
| 1935 |
template <class T> struct hash<shared_ptr<T>>;
|
| 1936 |
```
|
| 1937 |
|
| 1938 |
-
|
| 1939 |
-
|
| 1940 |
-
`
|
| 1941 |
-
value as `hash<T*>()(p.get())`.
|
| 1942 |
|
|
|
|
| 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 `MoveConstructible` and `MoveAssignable`,
|
| 41 |
but is not `CopyConstructible` nor `CopyAssignable`. The template
|
| 42 |
parameter `T` of `unique_ptr` may be an incomplete type.
|
| 43 |
|
| 44 |
+
[*Note 2*: The uses of `unique_ptr` include providing exception safety
|
| 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[]>;
|
|
|
|
| 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[]> {
|
| 149 |
constexpr default_delete() noexcept = default;
|
| 150 |
+
template <class U> default_delete(const default_delete<U[]>&) noexcept;
|
| 151 |
+
template <class U> void operator()(U* ptr) const;
|
| 152 |
};
|
| 153 |
}
|
| 154 |
```
|
| 155 |
|
| 156 |
``` cpp
|
| 157 |
+
template <class U> default_delete(const default_delete<U[]>& other) noexcept;
|
| 158 |
```
|
| 159 |
|
| 160 |
+
*Effects:* constructs a `default_delete` object from another
|
| 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 {
|
| 181 |
public:
|
| 182 |
+
using pointer = see below;
|
| 183 |
+
using element_type = T;
|
| 184 |
+
using deleter_type = D;
|
| 185 |
|
| 186 |
// [unique.ptr.single.ctor], constructors
|
| 187 |
constexpr unique_ptr() noexcept;
|
| 188 |
explicit unique_ptr(pointer p) noexcept;
|
| 189 |
unique_ptr(pointer p, see below d1) noexcept;
|
| 190 |
unique_ptr(pointer p, see below d2) noexcept;
|
| 191 |
unique_ptr(unique_ptr&& u) noexcept;
|
| 192 |
+
constexpr unique_ptr(nullptr_t) noexcept;
|
|
|
|
| 193 |
template <class U, class E>
|
| 194 |
unique_ptr(unique_ptr<U, E>&& u) noexcept;
|
|
|
|
|
|
|
| 195 |
|
| 196 |
// [unique.ptr.single.dtor], destructor
|
| 197 |
~unique_ptr();
|
| 198 |
|
| 199 |
// [unique.ptr.single.asgn], assignment
|
|
|
|
| 207 |
pointer get() const noexcept;
|
| 208 |
deleter_type& get_deleter() noexcept;
|
| 209 |
const deleter_type& get_deleter() const noexcept;
|
| 210 |
explicit operator bool() const noexcept;
|
| 211 |
|
| 212 |
+
// [unique.ptr.single.modifiers], modifiers
|
| 213 |
pointer release() noexcept;
|
| 214 |
void reset(pointer p = pointer()) noexcept;
|
| 215 |
void swap(unique_ptr& u) noexcept;
|
| 216 |
|
| 217 |
// disable copy from lvalue
|
|
|
|
| 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]]), lvalue reference to function, or lvalue reference
|
| 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 satisfy the
|
| 233 |
+
requirements of `Destructible` (Table [[tab:destructible]]).
|
| 234 |
|
| 235 |
+
If the *qualified-id* `remove_reference_t<D>::pointer` is valid and
|
| 236 |
+
denotes a type ([[temp.deduct]]), then `unique_ptr<T,
|
| 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 satisfy the requirements of `NullablePointer` (
|
| 241 |
[[nullablepointer.requirements]]).
|
| 242 |
|
| 243 |
+
[*Example 1*: Given an allocator type `X` ([[allocator.requirements]])
|
| 244 |
+
and letting `A` be a synonym for `allocator_traits<X>`, the types
|
| 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 |
##### `unique_ptr` constructors <a id="unique.ptr.single.ctor">[[unique.ptr.single.ctor]]</a>
|
| 250 |
|
| 251 |
``` cpp
|
| 252 |
constexpr unique_ptr() noexcept;
|
| 253 |
+
constexpr unique_ptr(nullptr_t) noexcept;
|
| 254 |
```
|
| 255 |
|
| 256 |
*Requires:* `D` shall satisfy the requirements of `DefaultConstructible`
|
| 257 |
+
(Table [[tab:defaultconstructible]]), and that construction shall not
|
| 258 |
+
throw an exception.
|
| 259 |
|
| 260 |
*Effects:* Constructs a `unique_ptr` object that owns nothing,
|
| 261 |
value-initializing the stored pointer and the stored deleter.
|
| 262 |
|
| 263 |
*Postconditions:* `get() == nullptr`. `get_deleter()` returns a
|
| 264 |
reference to the stored deleter.
|
| 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 |
*Requires:* `D` shall satisfy the requirements of `DefaultConstructible`
|
| 275 |
+
(Table [[tab:defaultconstructible]]), and that construction shall not
|
| 276 |
+
throw an exception.
|
| 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 |
*Postconditions:* `get() == p`. `get_deleter()` returns a reference to
|
| 282 |
the stored deleter.
|
| 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 A& d) noexcept;
|
| 302 |
+
unique_ptr(pointer p, A&& d) noexcept;
|
| 303 |
```
|
| 304 |
|
| 305 |
+
If `D` is an lvalue reference type `A&`, then the signatures are:
|
| 306 |
|
| 307 |
``` cpp
|
| 308 |
+
unique_ptr(pointer p, A& d) noexcept;
|
| 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 |
``` cpp
|
| 315 |
+
unique_ptr(pointer p, const A& d) noexcept;
|
| 316 |
+
unique_ptr(pointer p, const A&& d) = delete;
|
| 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 |
+
*Remarks:* These constructors shall not participate in overload
|
| 324 |
+
resolution unless `is_constructible_v<D, decltype(d)>` is `true`.
|
| 325 |
|
| 326 |
*Postconditions:* `get() == p`. `get_deleter()` returns a reference to
|
| 327 |
the stored deleter. If `D` is a reference type then `get_deleter()`
|
| 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 MoveConstructible
|
| 340 |
unique_ptr<int, D> p2(new int, d); // D must be CopyConstructible
|
| 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 |
|
| 346 |
+
— *end example*]
|
| 347 |
+
|
| 348 |
``` cpp
|
| 349 |
unique_ptr(unique_ptr&& u) noexcept;
|
| 350 |
```
|
| 351 |
|
| 352 |
*Requires:* If `D` is not a reference type, `D` shall satisfy the
|
| 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 |
*Effects:* Constructs a `unique_ptr` by transferring ownership from `u`
|
| 358 |
to `*this`. If `D` is a reference type, this deleter is copy constructed
|
| 359 |
from `u`’s deleter; otherwise, this deleter is move constructed from
|
| 360 |
+
`u`’s deleter.
|
| 361 |
+
|
| 362 |
+
[*Note 1*: The deleter constructor can be implemented with
|
| 363 |
+
`std::forward<D>`. — *end note*]
|
| 364 |
|
| 365 |
*Postconditions:* `get()` yields the value `u.get()` yielded before the
|
| 366 |
construction. `get_deleter()` returns a reference to the stored deleter
|
| 367 |
that was constructed from `u.get_deleter()`. If `D` is a reference type
|
| 368 |
then `get_deleter()` and `u.get_deleter()` both reference the same
|
|
|
|
| 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 |
*Postconditions:* `get()` yields the value `u.get()` yielded before the
|
| 398 |
construction. `get_deleter()` returns a reference to the stored deleter
|
| 399 |
that was constructed from `u.get_deleter()`.
|
| 400 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 401 |
##### `unique_ptr` destructor <a id="unique.ptr.single.dtor">[[unique.ptr.single.dtor]]</a>
|
| 402 |
|
| 403 |
``` cpp
|
| 404 |
~unique_ptr();
|
| 405 |
```
|
| 406 |
|
| 407 |
*Requires:* The expression `get_deleter()(get())` shall be well formed,
|
| 408 |
+
shall have well-defined behavior, and shall not throw exceptions.
|
| 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 |
##### `unique_ptr` assignment <a id="unique.ptr.single.asgn">[[unique.ptr.single.asgn]]</a>
|
|
|
|
| 418 |
``` cpp
|
| 419 |
unique_ptr& operator=(unique_ptr&& u) noexcept;
|
| 420 |
```
|
| 421 |
|
| 422 |
*Requires:* If `D` is not a reference type, `D` shall satisfy the
|
| 423 |
+
requirements of `MoveAssignable` (Table [[tab:moveassignable]]) and
|
| 424 |
assignment of the deleter from an rvalue of type `D` shall not throw an
|
| 425 |
exception. Otherwise, `D` is a reference type; `remove_reference_t<D>`
|
| 426 |
shall satisfy the `CopyAssignable` requirements and assignment of the
|
| 427 |
deleter from an lvalue of type `D` shall not throw an exception.
|
| 428 |
|
|
|
|
| 443 |
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 |
*Effects:* Transfers ownership from `u` to `*this` as if by calling
|
| 454 |
`reset(u.release())` followed by
|
| 455 |
`get_deleter() = std::forward<E>(u.get_deleter())`.
|
| 456 |
|
|
|
|
| 458 |
|
| 459 |
``` cpp
|
| 460 |
unique_ptr& operator=(nullptr_t) noexcept;
|
| 461 |
```
|
| 462 |
|
| 463 |
+
*Effects:* As if by `reset()`.
|
| 464 |
|
| 465 |
+
*Postconditions:* `get() == nullptr`.
|
| 466 |
|
| 467 |
*Returns:* `*this`.
|
| 468 |
|
| 469 |
##### `unique_ptr` observers <a id="unique.ptr.single.observers">[[unique.ptr.single.observers]]</a>
|
| 470 |
|
|
|
|
| 482 |
|
| 483 |
*Requires:* `get() != nullptr`.
|
| 484 |
|
| 485 |
*Returns:* `get()`.
|
| 486 |
|
| 487 |
+
[*Note 4*: The use of this function typically requires that `T` be a
|
| 488 |
+
complete type. — *end note*]
|
| 489 |
|
| 490 |
``` cpp
|
| 491 |
pointer get() const noexcept;
|
| 492 |
```
|
| 493 |
|
|
|
|
| 510 |
|
| 511 |
``` cpp
|
| 512 |
pointer release() noexcept;
|
| 513 |
```
|
| 514 |
|
| 515 |
+
*Postconditions:* `get() == nullptr`.
|
| 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 |
*Requires:* The expression `get_deleter()(get())` shall be well formed,
|
| 524 |
shall have well-defined behavior, and shall not throw exceptions.
|
| 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 |
+
*Postconditions:* `get() == p`.
|
| 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 |
|
|
|
|
| 551 |
|
| 552 |
``` cpp
|
| 553 |
namespace std {
|
| 554 |
template <class T, class D> class unique_ptr<T[], D> {
|
| 555 |
public:
|
| 556 |
+
using pointer = see below;
|
| 557 |
+
using element_type = T;
|
| 558 |
+
using deleter_type = D;
|
| 559 |
|
| 560 |
// [unique.ptr.runtime.ctor], constructors
|
| 561 |
constexpr unique_ptr() noexcept;
|
| 562 |
+
template <class U> explicit unique_ptr(U p) noexcept;
|
| 563 |
+
template <class U> unique_ptr(U p, see below d) noexcept;
|
| 564 |
+
template <class U> unique_ptr(U p, see below d) noexcept;
|
| 565 |
unique_ptr(unique_ptr&& u) noexcept;
|
| 566 |
+
template <class U, class E>
|
| 567 |
+
unique_ptr(unique_ptr<U, E>&& u) noexcept;
|
| 568 |
+
constexpr unique_ptr(nullptr_t) noexcept;
|
| 569 |
|
| 570 |
// destructor
|
| 571 |
~unique_ptr();
|
| 572 |
|
| 573 |
// assignment
|
| 574 |
unique_ptr& operator=(unique_ptr&& u) noexcept;
|
| 575 |
+
template <class U, class E>
|
| 576 |
+
unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
|
| 577 |
unique_ptr& operator=(nullptr_t) noexcept;
|
| 578 |
|
| 579 |
// [unique.ptr.runtime.observers], observers
|
| 580 |
T& operator[](size_t i) const;
|
| 581 |
pointer get() const noexcept;
|
| 582 |
deleter_type& get_deleter() noexcept;
|
| 583 |
const deleter_type& get_deleter() const noexcept;
|
| 584 |
explicit operator bool() const noexcept;
|
| 585 |
|
| 586 |
+
// [unique.ptr.runtime.modifiers], modifiers
|
| 587 |
pointer release() noexcept;
|
| 588 |
+
template <class U> void reset(U p) noexcept;
|
| 589 |
+
void reset(nullptr_t = nullptr) noexcept;
|
|
|
|
| 590 |
void swap(unique_ptr& u) noexcept;
|
| 591 |
|
| 592 |
// disable copy from lvalue
|
| 593 |
unique_ptr(const unique_ptr&) = delete;
|
| 594 |
unique_ptr& operator=(const unique_ptr&) = delete;
|
|
|
|
| 597 |
```
|
| 598 |
|
| 599 |
A specialization for array types is provided with a slightly altered
|
| 600 |
interface.
|
| 601 |
|
| 602 |
+
- Conversions between different types of `unique_ptr<T[], D>` that would
|
| 603 |
+
be disallowed for the corresponding pointer-to-array types, and
|
| 604 |
+
conversions to or from the non-array forms of `unique_ptr`, produce an
|
| 605 |
+
ill-formed program.
|
| 606 |
- Pointers to types derived from `T` are rejected by the constructors,
|
| 607 |
and by `reset`.
|
| 608 |
- The observers `operator*` and `operator->` are not provided.
|
| 609 |
- The indexing observer `operator[]` is provided.
|
| 610 |
- The default deleter will call `delete[]`.
|
| 611 |
|
| 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 |
##### `unique_ptr` constructors <a id="unique.ptr.runtime.ctor">[[unique.ptr.runtime.ctor]]</a>
|
| 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` except that it
|
| 625 |
+
additionally shall not participate in overload resolution unless
|
| 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 |
+
|
| 631 |
+
``` cpp
|
| 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 |
+
except that they shall not participate in overload resolution unless
|
| 639 |
+
either
|
| 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 <class U, class E>
|
| 648 |
+
unique_ptr(unique_ptr<U, E>&& u) noexcept;
|
| 649 |
+
```
|
| 650 |
+
|
| 651 |
+
This constructor behaves the same as in the primary template, except
|
| 652 |
+
that it shall not participate in overload resolution unless all of the
|
| 653 |
+
following conditions hold, where `UP` is `unique_ptr<U, E>`:
|
| 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 overload-resolution specification of the
|
| 663 |
+
primary template — *end note*]
|
| 664 |
+
|
| 665 |
+
##### `unique_ptr` assignment <a id="unique.ptr.runtime.asgn">[[unique.ptr.runtime.asgn]]</a>
|
| 666 |
+
|
| 667 |
+
``` cpp
|
| 668 |
+
template <class U, class E>
|
| 669 |
+
unique_ptr& operator=(unique_ptr<U, E>&& u)noexcept;
|
| 670 |
+
```
|
| 671 |
+
|
| 672 |
+
This operator behaves the same as in the primary template, except that
|
| 673 |
+
it shall not participate in overload resolution unless all of the
|
| 674 |
+
following conditions hold, where `UP` is `unique_ptr<U, E>`:
|
| 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 overload-resolution specification of the
|
| 683 |
+
primary template — *end note*]
|
| 684 |
|
| 685 |
##### `unique_ptr` observers <a id="unique.ptr.runtime.observers">[[unique.ptr.runtime.observers]]</a>
|
| 686 |
|
| 687 |
``` cpp
|
| 688 |
T& operator[](size_t i) const;
|
|
|
|
| 694 |
*Returns:* `get()[i]`.
|
| 695 |
|
| 696 |
##### `unique_ptr` modifiers <a id="unique.ptr.runtime.modifiers">[[unique.ptr.runtime.modifiers]]</a>
|
| 697 |
|
| 698 |
``` cpp
|
| 699 |
+
void reset(nullptr_t p = nullptr) noexcept;
|
| 700 |
```
|
| 701 |
|
| 702 |
*Effects:* Equivalent to `reset(pointer())`.
|
| 703 |
|
| 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, except that it shall not participate in overload resolution
|
| 710 |
+
unless either
|
| 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 |
#### `unique_ptr` creation <a id="unique.ptr.create">[[unique.ptr.create]]</a>
|
| 717 |
|
| 718 |
``` cpp
|
| 719 |
template <class T, class... Args> unique_ptr<T> make_unique(Args&&... args);
|
| 720 |
```
|
|
|
|
| 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 |
+
*Remarks:* This function shall not participate in overload resolution
|
| 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);
|
|
|
|
| 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 |
+
*Requires:* Let *`CT`* denote
|
|
|
|
|
|
|
|
|
|
| 774 |
|
| 775 |
+
``` cpp
|
| 776 |
+
common_type_t<typename unique_ptr<T1, D1>::pointer,
|
| 777 |
+
typename unique_ptr<T2, D2>::pointer>
|
| 778 |
+
```
|
| 779 |
+
|
| 780 |
+
Then the specialization `less<`*`CT`*`>` shall be a function object
|
| 781 |
+
type ([[function.objects]]) that induces a strict weak
|
| 782 |
+
ordering ([[alg.sorting]]) on the pointer values.
|
| 783 |
+
|
| 784 |
+
*Returns:* `less<`*`CT`*`>()(x.get(), y.get())`.
|
| 785 |
|
| 786 |
*Remarks:* If `unique_ptr<T1, D1>::pointer` is not implicitly
|
| 787 |
+
convertible to *`CT`* or `unique_ptr<T2, D2>::pointer` is not implicitly
|
| 788 |
+
convertible to *`CT`*, the program is ill-formed.
|
| 789 |
|
| 790 |
``` cpp
|
| 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 |
```
|
|
|
|
| 872 |
*Returns:* The first function template returns `!(x < nullptr)`. The
|
| 873 |
second function template returns `!(nullptr < x)`.
|
| 874 |
|
| 875 |
### Shared-ownership pointers <a id="util.smartptr">[[util.smartptr]]</a>
|
| 876 |
|
| 877 |
+
#### Class `bad_weak_ptr` <a id="util.smartptr.weak.bad">[[util.smartptr.weak.bad]]</a>
|
| 878 |
|
| 879 |
``` cpp
|
| 880 |
namespace std {
|
| 881 |
+
class bad_weak_ptr : public exception {
|
| 882 |
public:
|
| 883 |
bad_weak_ptr() noexcept;
|
| 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 |
bad_weak_ptr() noexcept;
|
| 893 |
```
|
| 894 |
|
| 895 |
+
*Postconditions:* `what()` returns an *implementation-defined* NTBS.
|
| 896 |
|
| 897 |
#### Class template `shared_ptr` <a id="util.smartptr.shared">[[util.smartptr.shared]]</a>
|
| 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.
|
| 903 |
+
A `shared_ptr` is said to be empty if it does not own a pointer.
|
| 904 |
|
| 905 |
``` cpp
|
| 906 |
namespace std {
|
| 907 |
template<class T> class shared_ptr {
|
| 908 |
public:
|
| 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> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
|
| 934 |
shared_ptr& operator=(shared_ptr&& r) noexcept;
|
| 935 |
template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r) noexcept;
|
|
|
|
| 936 |
template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
|
| 937 |
|
| 938 |
+
// [util.smartptr.shared.mod], modifiers
|
| 939 |
void swap(shared_ptr& r) noexcept;
|
| 940 |
void reset() noexcept;
|
| 941 |
template<class Y> void reset(Y* p);
|
| 942 |
template<class Y, class D> void reset(Y* p, D d);
|
| 943 |
template<class Y, class D, class A> void reset(Y* p, D d, A a);
|
| 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> bool owner_before(const shared_ptr<U>& b) const noexcept;
|
| 953 |
+
template<class U> bool owner_before(const weak_ptr<U>& b) const noexcept;
|
| 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>
|
|
|
|
| 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 `CopyConstructible`,
|
| 1029 |
`CopyAssignable`, and `LessThanComparable`, allowing their use in
|
| 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. The template parameter `T`
|
| 1033 |
+
of `shared_ptr` may be an incomplete type.
|
| 1034 |
+
|
| 1035 |
+
[*Example 1*:
|
| 1036 |
|
| 1037 |
``` cpp
|
| 1038 |
if (shared_ptr<X> px = dynamic_pointer_cast<X>(py)) {
|
| 1039 |
// do something with px
|
| 1040 |
}
|
| 1041 |
```
|
| 1042 |
|
| 1043 |
+
— *end example*]
|
| 1044 |
+
|
| 1045 |
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 [[util.smartptr]], a pointer type `Y*` is
|
| 1052 |
+
said to be *compatible with* a pointer type `T*` when either `Y*` is
|
| 1053 |
+
convertible to `T*` or `Y` is `U[N]` and `T` is cv `U[]`.
|
| 1054 |
+
|
| 1055 |
##### `shared_ptr` constructors <a id="util.smartptr.shared.const">[[util.smartptr.shared.const]]</a>
|
| 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` ([[util.smartptr.enab]]), then
|
| 1061 |
+
`remove_cv_t<Y>*` shall be implicitly convertible to `T*` and the
|
| 1062 |
+
constructor evaluates the statement:
|
| 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 |
+
*Effects:* Constructs an empty `shared_ptr` object.
|
| 1078 |
|
| 1079 |
*Postconditions:* `use_count() == 0 && get() == nullptr`.
|
| 1080 |
|
| 1081 |
``` cpp
|
| 1082 |
template<class Y> explicit shared_ptr(Y* p);
|
| 1083 |
```
|
| 1084 |
|
| 1085 |
+
*Requires:* `Y` shall be a complete type. The expression `delete[] p`,
|
| 1086 |
+
when `T` is an array type, or `delete p`, when `T` is not an array type,
|
| 1087 |
+
shall have well-defined behavior, and shall not throw exceptions.
|
| 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 |
*Postconditions:* `use_count() == 1 && get() == p`.
|
| 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 |
+
*Requires:* Construction of `d` and a deleter of type `D` initialized
|
| 1117 |
+
with `std::move(d)` shall not throw exceptions. The expression `d(p)`
|
| 1118 |
+
shall have well-defined behavior and shall not throw exceptions. `A`
|
| 1119 |
+
shall be an allocator ([[allocator.requirements]]).
|
|
|
|
|
|
|
| 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 |
*Postconditions:* `use_count() == 1 && get() == p`.
|
| 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 |
+
*Postconditions:* `get() == p && use_count() == r.use_count()`.
|
| 1149 |
|
| 1150 |
+
[*Note 1*: To avoid the possibility of a dangling pointer, the user of
|
| 1151 |
+
this constructor must ensure that `p` remains valid at least until the
|
| 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 |
+
*Remarks:* The second constructor shall not participate in overload
|
| 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 |
*Postconditions:* `get() == r.get() && use_count() == r.use_count()`.
|
| 1170 |
|
| 1171 |
``` cpp
|
| 1172 |
shared_ptr(shared_ptr&& r) noexcept;
|
| 1173 |
template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
|
| 1174 |
```
|
| 1175 |
|
| 1176 |
+
*Remarks:* The second constructor shall not participate in overload
|
| 1177 |
+
resolution unless `Y*` is compatible with `T*`.
|
| 1178 |
|
| 1179 |
+
*Effects:* Move constructs a `shared_ptr` instance from `r`.
|
| 1180 |
|
| 1181 |
*Postconditions:* `*this` shall contain the old value of `r`. `r` shall
|
| 1182 |
+
be empty. `r.get() == nullptr`.
|
| 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 |
*Postconditions:* `use_count() == r.use_count()`.
|
| 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 |
+
*Remarks:* This constructor shall not participate in overload resolution
|
| 1204 |
+
unless `Y*` is compatible with `T*` and `unique_ptr<Y, D>::pointer` is
|
| 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 |
##### `shared_ptr` destructor <a id="util.smartptr.shared.dest">[[util.smartptr.shared.dest]]</a>
|
| 1214 |
|
| 1215 |
``` cpp
|
| 1216 |
~shared_ptr();
|
| 1217 |
```
|
| 1218 |
|
| 1219 |
*Effects:*
|
| 1220 |
|
| 1221 |
+
- If `*this` is empty or shares ownership with another `shared_ptr`
|
| 1222 |
instance (`use_count() > 1`), there are no side effects.
|
| 1223 |
+
- Otherwise, if `*this` owns an object `p` and a deleter `d`, `d(p)` is
|
| 1224 |
+
called.
|
| 1225 |
+
- Otherwise, `*this` owns a pointer `p`, and `delete p` is called.
|
| 1226 |
|
| 1227 |
+
[*Note 1*: Since the destruction of `*this` decreases the number of
|
| 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 |
##### `shared_ptr` assignment <a id="util.smartptr.shared.assign">[[util.smartptr.shared.assign]]</a>
|
| 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 3*:
|
| 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:
|
| 1250 |
|
|
|
|
| 1255 |
q = p;
|
| 1256 |
```
|
| 1257 |
|
| 1258 |
both assignments may be no-ops.
|
| 1259 |
|
| 1260 |
+
— *end note*]
|
| 1261 |
+
|
| 1262 |
``` cpp
|
| 1263 |
shared_ptr& operator=(shared_ptr&& r) noexcept;
|
| 1264 |
template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r) noexcept;
|
| 1265 |
```
|
| 1266 |
|
|
|
|
| 1272 |
template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
|
| 1273 |
```
|
| 1274 |
|
| 1275 |
*Effects:* Equivalent to `shared_ptr(std::move(r)).swap(*this)`.
|
| 1276 |
|
| 1277 |
+
*Returns:* `*this`.
|
| 1278 |
|
| 1279 |
##### `shared_ptr` modifiers <a id="util.smartptr.shared.mod">[[util.smartptr.shared.mod]]</a>
|
| 1280 |
|
| 1281 |
``` cpp
|
| 1282 |
void swap(shared_ptr& r) noexcept;
|
|
|
|
| 1309 |
*Effects:* Equivalent to `shared_ptr(p, d, a).swap(*this)`.
|
| 1310 |
|
| 1311 |
##### `shared_ptr` observers <a id="util.smartptr.shared.obs">[[util.smartptr.shared.obs]]</a>
|
| 1312 |
|
| 1313 |
``` cpp
|
| 1314 |
+
element_type* get() const noexcept;
|
| 1315 |
```
|
| 1316 |
|
| 1317 |
+
*Returns:* The stored pointer.
|
| 1318 |
|
| 1319 |
``` cpp
|
| 1320 |
T& operator*() const noexcept;
|
| 1321 |
```
|
| 1322 |
|
| 1323 |
*Requires:* `get() != 0`.
|
| 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 well
|
| 1331 |
+
formed.
|
| 1332 |
|
| 1333 |
``` cpp
|
| 1334 |
T* operator->() const noexcept;
|
| 1335 |
```
|
| 1336 |
|
| 1337 |
*Requires:* `get() != 0`.
|
| 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 formed.
|
| 1345 |
+
|
| 1346 |
+
``` cpp
|
| 1347 |
+
element_type& operator[](ptrdiff_t i) const;
|
| 1348 |
+
```
|
| 1349 |
+
|
| 1350 |
+
*Requires:* `get() != 0 && i >= 0`. If `T` is `U[N]`, `i < N`.
|
| 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 formed.
|
| 1358 |
+
|
| 1359 |
+
*Throws:* Nothing.
|
| 1360 |
+
|
| 1361 |
``` cpp
|
| 1362 |
long use_count() const noexcept;
|
| 1363 |
```
|
| 1364 |
|
| 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 4*: `get() == nullptr` does not imply a specific return value of
|
| 1371 |
+
`use_count()`. — *end note*]
|
|
|
|
| 1372 |
|
| 1373 |
+
[*Note 5*: `weak_ptr<T>::lock()` can affect the return value of
|
| 1374 |
+
`use_count()`. — *end note*]
|
| 1375 |
|
| 1376 |
+
[*Note 6*: When multiple threads can affect the return value of
|
| 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 |
|
| 1382 |
``` cpp
|
| 1383 |
explicit operator bool() const noexcept;
|
| 1384 |
```
|
| 1385 |
|
| 1386 |
*Returns:* `get() != 0`.
|
| 1387 |
|
| 1388 |
``` cpp
|
| 1389 |
+
template<class U> bool owner_before(const shared_ptr<U>& b) const noexcept;
|
| 1390 |
+
template<class U> bool owner_before(const weak_ptr<U>& b) const noexcept;
|
| 1391 |
```
|
| 1392 |
|
| 1393 |
*Returns:* An unspecified value such that
|
| 1394 |
|
| 1395 |
- `x.owner_before(y)` defines a strict weak ordering as defined
|
|
|
|
| 1400 |
ownership or are both empty.
|
| 1401 |
|
| 1402 |
##### `shared_ptr` creation <a id="util.smartptr.shared.create">[[util.smartptr.shared.create]]</a>
|
| 1403 |
|
| 1404 |
``` cpp
|
| 1405 |
+
template<class T, class... Args>
|
| 1406 |
+
shared_ptr<T> make_shared(Args&&... args);
|
| 1407 |
template<class T, class A, class... Args>
|
| 1408 |
shared_ptr<T> allocate_shared(const A& a, Args&&... args);
|
| 1409 |
```
|
| 1410 |
|
| 1411 |
*Requires:* The expression `::new (pv) T(std::forward<Args>(args)...)`,
|
| 1412 |
where `pv` has type `void*` and points to storage suitable to hold an
|
| 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 suitable for an object of type `T` and
|
| 1418 |
+
constructs an object in that memory via the placement *new-expression*
|
| 1419 |
`::new (pv) T(std::forward<Args>(args)...)`. The template
|
| 1420 |
`allocate_shared` uses a copy of `a` to allocate memory. If an exception
|
| 1421 |
is thrown, the functions have no effect.
|
| 1422 |
|
| 1423 |
*Returns:* A `shared_ptr` instance that stores and owns the address of
|
| 1424 |
the newly constructed object of type `T`.
|
| 1425 |
|
| 1426 |
+
*Postconditions:* `get() != 0 && use_count() == 1`.
|
| 1427 |
|
| 1428 |
*Throws:* `bad_alloc`, or an exception thrown from `A::allocate` or from
|
| 1429 |
the constructor of `T`.
|
| 1430 |
|
| 1431 |
+
*Remarks:* The `shared_ptr` constructor called by this function enables
|
| 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 |
+
[*Note 7*: This provides efficiency equivalent to an intrusive smart
|
| 1437 |
+
pointer. — *end note*]
|
| 1438 |
+
|
| 1439 |
+
[*Note 8*: These functions will typically allocate more memory than
|
| 1440 |
+
`sizeof(T)` to allow for internal bookkeeping structures such as the
|
| 1441 |
+
reference counts. — *end note*]
|
| 1442 |
|
| 1443 |
##### `shared_ptr` comparison <a id="util.smartptr.shared.cmp">[[util.smartptr.shared.cmp]]</a>
|
| 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>
|
|
|
|
| 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>
|
|
|
|
| 1520 |
second function template returns `!(nullptr < a)`.
|
| 1521 |
|
| 1522 |
##### `shared_ptr` specialized algorithms <a id="util.smartptr.shared.spec">[[util.smartptr.shared.spec]]</a>
|
| 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 |
##### `shared_ptr` casts <a id="util.smartptr.shared.cast">[[util.smartptr.shared.cast]]</a>
|
| 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 |
+
*Requires:* The expression `static_cast<T*>((U*)0)` shall be well
|
| 1539 |
formed.
|
| 1540 |
|
| 1541 |
+
*Returns:*
|
|
|
|
|
|
|
| 1542 |
|
| 1543 |
+
``` cpp
|
| 1544 |
+
shared_ptr<T>(r, static_cast<typename shared_ptr<T>::element_type*>(r.get()))
|
| 1545 |
+
```
|
| 1546 |
|
| 1547 |
+
[*Note 10*: The seemingly equivalent expression
|
| 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 |
+
*Requires:* The expression `dynamic_cast<T*>((U*)0)` shall be well
|
| 1558 |
formed and shall have well defined behavior.
|
| 1559 |
|
| 1560 |
*Returns:*
|
| 1561 |
|
| 1562 |
+
- When `dynamic_cast<typename shared_ptr<T>::element_type*>(r.get())`
|
| 1563 |
+
returns a nonzero value `p`, `shared_ptr<T>(r, p)`.
|
| 1564 |
+
- Otherwise, `shared_ptr<T>()`.
|
|
|
|
| 1565 |
|
| 1566 |
+
[*Note 11*: The seemingly equivalent expression
|
|
|
|
|
|
|
| 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 |
+
*Requires:* The expression `const_cast<T*>((U*)0)` shall be well formed.
|
|
|
|
| 1577 |
|
| 1578 |
+
*Returns:*
|
|
|
|
|
|
|
| 1579 |
|
| 1580 |
+
``` cpp
|
| 1581 |
+
shared_ptr<T>(r, const_cast<typename shared_ptr<T>::element_type*>(r.get()))
|
| 1582 |
+
```
|
| 1583 |
|
| 1584 |
+
[*Note 12*: The seemingly equivalent expression
|
| 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 |
+
*Requires:* The expression `reinterpret_cast<T*>((U*)0)` shall be well
|
| 1595 |
+
formed.
|
| 1596 |
+
|
| 1597 |
+
*Returns:*
|
| 1598 |
+
|
| 1599 |
+
``` cpp
|
| 1600 |
+
shared_ptr<T>(r, reinterpret_cast<typename shared_ptr<T>::element_type*>(r.get()))
|
| 1601 |
+
```
|
| 1602 |
+
|
| 1603 |
+
[*Note 13*: The seemingly equivalent expression
|
| 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 |
+
##### `get_deleter` <a id="util.smartptr.getdeleter">[[util.smartptr.getdeleter]]</a>
|
| 1609 |
|
| 1610 |
``` cpp
|
| 1611 |
+
template<class D, class T>
|
| 1612 |
+
D* get_deleter(const shared_ptr<T>& p) noexcept;
|
| 1613 |
```
|
| 1614 |
|
| 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 14*: It is unspecified whether the pointer remains valid longer
|
| 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 |
##### `shared_ptr` I/O <a id="util.smartptr.shared.io">[[util.smartptr.shared.io]]</a>
|
| 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 |
#### Class template `weak_ptr` <a id="util.smartptr.weak">[[util.smartptr.weak]]</a>
|
| 1637 |
|
|
|
|
| 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> weak_ptr(const shared_ptr<Y>& r) noexcept;
|
| 1651 |
+
weak_ptr(const weak_ptr& r) noexcept;
|
| 1652 |
+
template<class Y> weak_ptr(const weak_ptr<Y>& r) noexcept;
|
| 1653 |
weak_ptr(weak_ptr&& r) noexcept;
|
| 1654 |
template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept;
|
| 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> weak_ptr& operator=(const weak_ptr<Y>& r) noexcept;
|
| 1662 |
+
template<class Y> weak_ptr& operator=(const shared_ptr<Y>& r) noexcept;
|
| 1663 |
weak_ptr& operator=(weak_ptr&& r) noexcept;
|
| 1664 |
template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept;
|
| 1665 |
|
| 1666 |
// [util.smartptr.weak.mod], modifiers
|
| 1667 |
void swap(weak_ptr& r) 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> bool owner_before(const shared_ptr<U>& b) const;
|
| 1675 |
+
template<class U> bool owner_before(const weak_ptr<U>& b) const;
|
| 1676 |
};
|
| 1677 |
|
| 1678 |
+
template<class T> weak_ptr(shared_ptr<T>) -> weak_ptr<T>;
|
| 1679 |
+
|
| 1680 |
+
|
| 1681 |
// [util.smartptr.weak.spec], specialized algorithms
|
| 1682 |
template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
|
| 1683 |
+
}
|
| 1684 |
```
|
| 1685 |
|
| 1686 |
Specializations of `weak_ptr` shall be `CopyConstructible` and
|
| 1687 |
`CopyAssignable`, allowing their use in standard containers. The
|
| 1688 |
template parameter `T` of `weak_ptr` may be an incomplete type.
|
|
|
|
| 1691 |
|
| 1692 |
``` cpp
|
| 1693 |
constexpr weak_ptr() noexcept;
|
| 1694 |
```
|
| 1695 |
|
| 1696 |
+
*Effects:* Constructs an empty `weak_ptr` object.
|
| 1697 |
|
| 1698 |
*Postconditions:* `use_count() == 0`.
|
| 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 |
+
*Remarks:* The second and third constructors shall not participate in
|
| 1707 |
+
overload resolution unless `Y*` is compatible with `T*`.
|
| 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 |
*Postconditions:* `use_count() == r.use_count()`.
|
| 1714 |
|
| 1715 |
``` cpp
|
| 1716 |
weak_ptr(weak_ptr&& r) noexcept;
|
| 1717 |
template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept;
|
| 1718 |
```
|
| 1719 |
|
| 1720 |
+
*Remarks:* The second constructor shall not participate in overload
|
| 1721 |
+
resolution unless `Y*` is compatible with `T*`.
|
| 1722 |
|
| 1723 |
+
*Effects:* Move constructs a `weak_ptr` instance from `r`.
|
| 1724 |
|
| 1725 |
*Postconditions:* `*this` shall contain the old value of `r`. `r` shall
|
| 1726 |
+
be empty. `r.use_count() == 0`.
|
| 1727 |
|
| 1728 |
##### `weak_ptr` destructor <a id="util.smartptr.weak.dest">[[util.smartptr.weak.dest]]</a>
|
| 1729 |
|
| 1730 |
``` cpp
|
| 1731 |
~weak_ptr();
|
|
|
|
| 1776 |
|
| 1777 |
``` cpp
|
| 1778 |
long use_count() const noexcept;
|
| 1779 |
```
|
| 1780 |
|
| 1781 |
+
*Returns:* `0` if `*this` is empty; otherwise, the number of
|
| 1782 |
+
`shared_ptr` instances that share ownership with `*this`.
|
|
|
|
|
|
|
| 1783 |
|
| 1784 |
``` cpp
|
| 1785 |
bool expired() const noexcept;
|
| 1786 |
```
|
| 1787 |
|
| 1788 |
*Returns:* `use_count() == 0`.
|
| 1789 |
|
|
|
|
|
|
|
| 1790 |
``` cpp
|
| 1791 |
shared_ptr<T> lock() const noexcept;
|
| 1792 |
```
|
| 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
|
|
|
|
| 1809 |
ownership or are both empty.
|
| 1810 |
|
| 1811 |
##### `weak_ptr` specialized algorithms <a id="util.smartptr.weak.spec">[[util.smartptr.weak.spec]]</a>
|
| 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 |
#### Class template `owner_less` <a id="util.smartptr.ownerless">[[util.smartptr.ownerless]]</a>
|
|
|
|
| 1822 |
The class template `owner_less` allows ownership-based mixed comparisons
|
| 1823 |
of shared and weak pointers.
|
| 1824 |
|
| 1825 |
``` cpp
|
| 1826 |
namespace std {
|
| 1827 |
+
template<class T = void> struct owner_less;
|
| 1828 |
|
| 1829 |
template<class T> struct owner_less<shared_ptr<T>> {
|
| 1830 |
+
bool operator()(const shared_ptr<T>&, const shared_ptr<T>&) const noexcept;
|
| 1831 |
+
bool operator()(const shared_ptr<T>&, const weak_ptr<T>&) const noexcept;
|
| 1832 |
+
bool operator()(const weak_ptr<T>&, const shared_ptr<T>&) const noexcept;
|
|
|
|
|
|
|
|
|
|
| 1833 |
};
|
| 1834 |
|
| 1835 |
template<class T> struct owner_less<weak_ptr<T>> {
|
| 1836 |
+
bool operator()(const weak_ptr<T>&, const weak_ptr<T>&) const noexcept;
|
| 1837 |
+
bool operator()(const shared_ptr<T>&, const weak_ptr<T>&) const noexcept;
|
| 1838 |
+
bool operator()(const weak_ptr<T>&, const shared_ptr<T>&) const noexcept;
|
| 1839 |
+
};
|
| 1840 |
+
|
| 1841 |
+
template<> struct owner_less<void> {
|
| 1842 |
+
template<class T, class U>
|
| 1843 |
+
bool operator()(const shared_ptr<T>&, const shared_ptr<U>&) const noexcept;
|
| 1844 |
+
template<class T, class U>
|
| 1845 |
+
bool operator()(const shared_ptr<T>&, const weak_ptr<U>&) const noexcept;
|
| 1846 |
+
template<class T, class U>
|
| 1847 |
+
bool operator()(const weak_ptr<T>&, const shared_ptr<U>&) const noexcept;
|
| 1848 |
+
template<class T, class U>
|
| 1849 |
+
bool operator()(const weak_ptr<T>&, const weak_ptr<U>&) const noexcept;
|
| 1850 |
+
|
| 1851 |
+
using is_transparent = unspecified;
|
| 1852 |
};
|
| 1853 |
}
|
| 1854 |
```
|
| 1855 |
|
| 1856 |
+
`operator()(x, y)` shall return `x.owner_before(y)`.
|
| 1857 |
+
|
| 1858 |
+
[*Note 1*:
|
| 1859 |
+
|
| 1860 |
+
Note that
|
| 1861 |
|
| 1862 |
- `operator()` defines a strict weak ordering as defined in
|
| 1863 |
[[alg.sorting]];
|
| 1864 |
- under the equivalence relation defined by `operator()`,
|
| 1865 |
`!operator()(a, b) && !operator()(b, a)`, two `shared_ptr` or
|
| 1866 |
`weak_ptr` instances are equivalent if and only if they share
|
| 1867 |
ownership or are both empty.
|
| 1868 |
|
| 1869 |
+
— *end note*]
|
| 1870 |
+
|
| 1871 |
#### Class template `enable_shared_from_this` <a id="util.smartptr.enab">[[util.smartptr.enab]]</a>
|
| 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 |
|
| 1877 |
+
[*Example 1*:
|
| 1878 |
+
|
| 1879 |
``` cpp
|
| 1880 |
+
struct X: public enable_shared_from_this<X> { };
|
|
|
|
| 1881 |
|
| 1882 |
int main() {
|
| 1883 |
shared_ptr<X> p(new X);
|
| 1884 |
shared_ptr<X> q = p->shared_from_this();
|
| 1885 |
assert(p == q);
|
| 1886 |
+
assert(!p.owner_before(q) && !q.owner_before(p)); // p and q share ownership
|
| 1887 |
}
|
| 1888 |
```
|
| 1889 |
|
| 1890 |
+
— *end example*]
|
| 1891 |
+
|
| 1892 |
``` cpp
|
| 1893 |
namespace std {
|
| 1894 |
template<class T> class enable_shared_from_this {
|
| 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 |
```
|
| 1910 |
|
| 1911 |
The template parameter `T` of `enable_shared_from_this` may be an
|
| 1912 |
incomplete type.
|
| 1913 |
|
| 1914 |
``` cpp
|
| 1915 |
constexpr enable_shared_from_this() noexcept;
|
| 1916 |
enable_shared_from_this(const enable_shared_from_this<T>&) noexcept;
|
| 1917 |
```
|
| 1918 |
|
| 1919 |
+
*Effects:* Value-initializes `weak_this`.
|
| 1920 |
|
| 1921 |
``` cpp
|
| 1922 |
enable_shared_from_this<T>& operator=(const enable_shared_from_this<T>&) noexcept;
|
| 1923 |
```
|
| 1924 |
|
| 1925 |
*Returns:* `*this`.
|
| 1926 |
|
| 1927 |
+
[*Note 1*: `weak_this` is not changed. — *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1928 |
|
| 1929 |
``` cpp
|
| 1930 |
shared_ptr<T> shared_from_this();
|
| 1931 |
shared_ptr<T const> shared_from_this() const;
|
| 1932 |
```
|
| 1933 |
|
| 1934 |
+
*Returns:* `shared_ptr<T>(weak_this)`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1935 |
|
| 1936 |
``` cpp
|
| 1937 |
+
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 |
#### `shared_ptr` atomic access <a id="util.smartptr.shared.atomic">[[util.smartptr.shared.atomic]]</a>
|
| 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
|
|
|
|
| 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>
|
|
|
|
| 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>
|
|
|
|
| 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,
|
|
|
|
| 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 ([[unord.hash]]) if and only if `hash<typename UP::pointer>` is
|
| 2101 |
+
enabled. When enabled, for an object `p` of type `UP`, `hash<UP>()(p)`
|
| 2102 |
+
shall evaluate to the same value as
|
| 2103 |
+
`hash<typename UP::pointer>()(p.get())`. The member functions are not
|
| 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 |
+
shall evaluate to the same value as
|
| 2112 |
+
`hash<typename shared_ptr<T>::element_type*>()(p.get())`.
|
|
|
|
| 2113 |
|