From Jason Turner

[util.smartptr.shared]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmp4jkfpwnx/{from.md → to.md} +73 -66
tmp/tmp4jkfpwnx/{from.md → to.md} RENAMED
@@ -1,6 +1,8 @@
1
- ### Class template `shared_ptr` <a id="util.smartptr.shared">[[util.smartptr.shared]]</a>
 
 
2
 
3
  The `shared_ptr` class template stores a pointer, usually obtained via
4
  `new`. `shared_ptr` implements semantics of shared ownership; the last
5
  remaining owner of the pointer is responsible for destroying the object,
6
  or otherwise releasing the resources associated with the stored pointer.
@@ -90,11 +92,11 @@ in standard containers. Specializations of `shared_ptr` shall be
90
  contextually convertible to `bool`, allowing their use in boolean
91
  expressions and declarations in conditions.
92
 
93
  The template parameter `T` of `shared_ptr` may be an incomplete type.
94
 
95
- [*Note 1*: `T` may be a function type. — *end note*]
96
 
97
  [*Example 1*:
98
 
99
  ``` cpp
100
  if (shared_ptr<X> px = dynamic_pointer_cast<X>(py)) {
@@ -112,11 +114,11 @@ races.
112
 
113
  For the purposes of subclause [[smartptr]], a pointer type `Y*` is said
114
  to be *compatible with* a pointer type `T*` when either `Y*` is
115
  convertible to `T*` or `Y` is `U[N]` and `T` is cv `U[]`.
116
 
117
- #### Constructors <a id="util.smartptr.shared.const">[[util.smartptr.shared.const]]</a>
118
 
119
  In the constructor definitions below, enables `shared_from_this` with
120
  `p`, for a pointer `p` of type `Y*`, means that if `Y` has an
121
  unambiguous and accessible base class that is a specialization of
122
  `enable_shared_from_this` [[util.smartptr.enab]], then `remove_cv_t<Y>*`
@@ -140,18 +142,18 @@ constexpr shared_ptr() noexcept;
140
 
141
  ``` cpp
142
  template<class Y> explicit shared_ptr(Y* p);
143
  ```
144
 
145
- *Mandates:* `Y` is a complete type.
146
-
147
  *Constraints:* When `T` is an array type, the expression `delete[] p` is
148
  well-formed and either `T` is `U[N]` and `Y(*)[N]` is convertible to
149
  `T*`, or `T` is `U[]` and `Y(*)[]` is convertible to `T*`. When `T` is
150
  not an array type, the expression `delete p` is well-formed and `Y*` is
151
  convertible to `T*`.
152
 
 
 
153
  *Preconditions:* The expression `delete[] p`, when `T` is an array type,
154
  or `delete p`, when `T` is not an array type, has well-defined behavior,
155
  and does not throw exceptions.
156
 
157
  *Effects:* When `T` is not an array type, constructs a `shared_ptr`
@@ -162,11 +164,11 @@ with `p`. If an exception is thrown, `delete p` is called when `T` is
162
  not an array type, `delete[] p` otherwise.
163
 
164
  *Ensures:* `use_count() == 1 && get() == p`.
165
 
166
  *Throws:* `bad_alloc`, or an *implementation-defined* exception when a
167
- resource other than memory could not be obtained.
168
 
169
  ``` cpp
170
  template<class Y, class D> shared_ptr(Y* p, D d);
171
  template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
172
  template<class D> shared_ptr(nullptr_t p, D d);
@@ -182,22 +184,23 @@ well-formed expression. For the first two overloads:
182
  - If `T` is not an array type, then `Y*` is convertible to `T*`.
183
 
184
  *Preconditions:* Construction of `d` and a deleter of type `D`
185
  initialized with `std::move(d)` do not throw exceptions. The expression
186
  `d(p)` has well-defined behavior and does not throw exceptions. `A`
187
- meets the *Cpp17Allocator* requirements ([[cpp17.allocator]]).
 
188
 
189
  *Effects:* Constructs a `shared_ptr` object that owns the object `p` and
190
  the deleter `d`. When `T` is not an array type, the first and second
191
  constructors enable `shared_from_this` with `p`. The second and fourth
192
  constructors shall use a copy of `a` to allocate memory for internal
193
  use. If an exception is thrown, `d(p)` is called.
194
 
195
  *Ensures:* `use_count() == 1 && get() == p`.
196
 
197
  *Throws:* `bad_alloc`, or an *implementation-defined* exception when a
198
- resource other than memory could not be obtained.
199
 
200
  ``` cpp
201
  template<class Y> shared_ptr(const shared_ptr<Y>& r, element_type* p) noexcept;
202
  template<class Y> shared_ptr(shared_ptr<Y>&& r, element_type* p) noexcept;
203
  ```
@@ -206,13 +209,13 @@ template<class Y> shared_ptr(shared_ptr<Y>&& r, element_type* p) noexcept;
206
  ownership with the initial value of `r`.
207
 
208
  *Ensures:* `get() == p`. For the second overload, `r` is empty and
209
  `r.get() == nullptr`.
210
 
211
- [*Note 1*: To avoid the possibility of a dangling pointer, the user of
212
- this constructor should ensure that `p` remains valid at least until the
213
- ownership group of `r` is destroyed. — *end note*]
214
 
215
  [*Note 2*: This constructor allows creation of an empty `shared_ptr`
216
  instance with a non-null stored pointer. — *end note*]
217
 
218
  ``` cpp
@@ -235,12 +238,12 @@ template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
235
 
236
  *Constraints:* For the second constructor, `Y*` is compatible with `T*`.
237
 
238
  *Effects:* Move constructs a `shared_ptr` instance from `r`.
239
 
240
- *Ensures:* `*this` shall contain the old value of `r`. `r` shall be
241
- empty. `r.get() == nullptr`.
242
 
243
  ``` cpp
244
  template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
245
  ```
246
 
@@ -261,15 +264,15 @@ template<class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
261
  *Constraints:* `Y*` is compatible with `T*` and
262
  `unique_ptr<Y, D>::pointer` is convertible to `element_type*`.
263
 
264
  *Effects:* If `r.get() == nullptr`, equivalent to `shared_ptr()`.
265
  Otherwise, if `D` is not a reference type, equivalent to
266
- `shared_ptr(r.release(), r.get_deleter())`. Otherwise, equivalent to
267
- `shared_ptr(r.release(), ref(r.get_deleter()))`. If an exception is
268
- thrown, the constructor has no effect.
269
 
270
- #### Destructor <a id="util.smartptr.shared.dest">[[util.smartptr.shared.dest]]</a>
271
 
272
  ``` cpp
273
  ~shared_ptr();
274
  ```
275
 
@@ -279,42 +282,42 @@ thrown, the constructor has no effect.
279
  instance (`use_count() > 1`), there are no side effects.
280
  - Otherwise, if `*this` owns an object `p` and a deleter `d`, `d(p)` is
281
  called.
282
  - Otherwise, `*this` owns a pointer `p`, and `delete p` is called.
283
 
284
- [*Note 1*: Since the destruction of `*this` decreases the number of
285
  instances that share ownership with `*this` by one, after `*this` has
286
  been destroyed all `shared_ptr` instances that shared ownership with
287
  `*this` will report a `use_count()` that is one less than its previous
288
  value. — *end note*]
289
 
290
- #### Assignment <a id="util.smartptr.shared.assign">[[util.smartptr.shared.assign]]</a>
291
 
292
  ``` cpp
293
  shared_ptr& operator=(const shared_ptr& r) noexcept;
294
  template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
295
  ```
296
 
297
  *Effects:* Equivalent to `shared_ptr(r).swap(*this)`.
298
 
299
  *Returns:* `*this`.
300
 
301
- [*Note 1*:
302
 
303
  The use count updates caused by the temporary object construction and
304
- destruction are not observable side effects, so the implementation may
305
  meet the effects (and the implied guarantees) via different means,
306
  without creating a temporary. In particular, in the example:
307
 
308
  ``` cpp
309
  shared_ptr<int> p(new int);
310
  shared_ptr<void> q(p);
311
  p = p;
312
  q = p;
313
  ```
314
 
315
- both assignments may be no-ops.
316
 
317
  — *end note*]
318
 
319
  ``` cpp
320
  shared_ptr& operator=(shared_ptr&& r) noexcept;
@@ -331,11 +334,11 @@ template<class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
331
 
332
  *Effects:* Equivalent to `shared_ptr(std::move(r)).swap(*this)`.
333
 
334
  *Returns:* `*this`.
335
 
336
- #### Modifiers <a id="util.smartptr.shared.mod">[[util.smartptr.shared.mod]]</a>
337
 
338
  ``` cpp
339
  void swap(shared_ptr& r) noexcept;
340
  ```
341
 
@@ -363,11 +366,11 @@ template<class Y, class D> void reset(Y* p, D d);
363
  template<class Y, class D, class A> void reset(Y* p, D d, A a);
364
  ```
365
 
366
  *Effects:* Equivalent to `shared_ptr(p, d, a).swap(*this)`.
367
 
368
- #### Observers <a id="util.smartptr.shared.obs">[[util.smartptr.shared.obs]]</a>
369
 
370
  ``` cpp
371
  element_type* get() const noexcept;
372
  ```
373
 
@@ -375,11 +378,11 @@ element_type* get() const noexcept;
375
 
376
  ``` cpp
377
  T& operator*() const noexcept;
378
  ```
379
 
380
- *Preconditions:* `get() != 0`.
381
 
382
  *Returns:* `*get()`.
383
 
384
  *Remarks:* When `T` is an array type or cv `void`, it is unspecified
385
  whether this member function is declared. If it is declared, it is
@@ -389,11 +392,11 @@ well-formed.
389
 
390
  ``` cpp
391
  T* operator->() const noexcept;
392
  ```
393
 
394
- *Preconditions:* `get() != 0`.
395
 
396
  *Returns:* `get()`.
397
 
398
  *Remarks:* When `T` is an array type, it is unspecified whether this
399
  member function is declared. If it is declared, it is unspecified what
@@ -402,47 +405,47 @@ necessarily the definition) of the function shall be well-formed.
402
 
403
  ``` cpp
404
  element_type& operator[](ptrdiff_t i) const;
405
  ```
406
 
407
- *Preconditions:* `get() != 0 && i >= 0`. If `T` is `U[N]`, `i < N`.
 
408
 
409
  *Returns:* `get()[i]`.
410
 
411
- *Remarks:* When `T` is not an array type, it is unspecified whether this
412
- member function is declared. If it is declared, it is unspecified what
413
- its return type is, except that the declaration (although not
414
- necessarily the definition) of the function shall be well-formed.
415
-
416
  *Throws:* Nothing.
417
 
 
 
 
 
 
418
  ``` cpp
419
  long use_count() const noexcept;
420
  ```
421
 
 
 
422
  *Returns:* The number of `shared_ptr` objects, `*this` included, that
423
  share ownership with `*this`, or `0` when `*this` is empty.
424
 
425
- *Synchronization:* None.
426
-
427
- [*Note 1*: `get() == nullptr` does not imply a specific return value of
428
  `use_count()`. — *end note*]
429
 
430
- [*Note 2*: `weak_ptr<T>::lock()` can affect the return value of
431
  `use_count()`. — *end note*]
432
 
433
- [*Note 3*: When multiple threads can affect the return value of
434
- `use_count()`, the result should be treated as approximate. In
435
- particular, `use_count() == 1` does not imply that accesses through a
436
- previously destroyed `shared_ptr` have in any sense
437
- completed. — *end note*]
438
 
439
  ``` cpp
440
  explicit operator bool() const noexcept;
441
  ```
442
 
443
- *Returns:* `get() != 0`.
444
 
445
  ``` cpp
446
  template<class U> bool owner_before(const shared_ptr<U>& b) const noexcept;
447
  template<class U> bool owner_before(const weak_ptr<U>& b) const noexcept;
448
  ```
@@ -454,11 +457,11 @@ template<class U> bool owner_before(const weak_ptr<U>& b) const noexcept;
454
  - under the equivalence relation defined by `owner_before`,
455
  `!a.owner_before(b) && !b.owner_before(a)`, two `shared_ptr` or
456
  `weak_ptr` instances are equivalent if and only if they share
457
  ownership or are both empty.
458
 
459
- #### Creation <a id="util.smartptr.shared.create">[[util.smartptr.shared.create]]</a>
460
 
461
  The common requirements that apply to all `make_shared`,
462
  `allocate_shared`, `make_shared_for_overwrite`, and
463
  `allocate_shared_for_overwrite` overloads, unless specified otherwise,
464
  are described below.
@@ -472,34 +475,34 @@ template<class T, ...>
472
  shared_ptr<T> make_shared_for_overwrite(args);
473
  template<class T, class A, ...>
474
  shared_ptr<T> allocate_shared_for_overwrite(const A& a, args);
475
  ```
476
 
477
- *Preconditions:* `A` meets the *Cpp17Allocator* requirements
478
- ([[cpp17.allocator]]).
479
 
480
  *Effects:* Allocates memory for an object of type `T` (or `U[N]` when
481
  `T` is `U[]`, where `N` is determined from *args* as specified by the
482
  concrete overload). The object is initialized from *args* as specified
483
  by the concrete overload. The `allocate_shared` and
484
  `allocate_shared_for_overwrite` templates use a copy of `a` (rebound for
485
  an unspecified `value_type`) to allocate memory. If an exception is
486
  thrown, the functions have no effect.
487
 
 
 
 
488
  *Returns:* A `shared_ptr` instance that stores and owns the address of
489
  the newly constructed object.
490
 
491
- *Ensures:* `r.get() != 0 && r.use_count() == 1`, where `r` is the return
492
- value.
493
-
494
  *Throws:* `bad_alloc`, or an exception thrown from `allocate` or from
495
  the initialization of the object.
496
 
497
  *Remarks:*
498
 
499
  - Implementations should perform no more than one memory allocation.
500
- \[*Note 1*: This provides efficiency equivalent to an intrusive smart
501
  pointer. — *end note*]
502
  - When an object of an array type `U` is specified to have an initial
503
  value of `u` (of the same type), this shall be interpreted to mean
504
  that each array element of the object has as its initial value the
505
  corresponding element from `u`.
@@ -551,11 +554,11 @@ the initialization of the object.
551
  expression `allocator_traits<A2>::destroy(a2, pv)` where `pv` points
552
  to that object of type `remove_cv_t<U>` and `a2` of type `A2` is a
553
  rebound copy of the allocator `a` passed to `allocate_shared` such
554
  that its `value_type` is `remove_cv_t<U>`.
555
 
556
- [*Note 1*: These functions will typically allocate more memory than
557
  `sizeof(T)` to allow for internal bookkeeping structures such as
558
  reference counts. — *end note*]
559
 
560
  ``` cpp
561
  template<class T, class... Args>
@@ -565,11 +568,11 @@ template<class T, class A, class... Args>
565
  ```
566
 
567
  *Constraints:* `T` is not an array type.
568
 
569
  *Returns:* A `shared_ptr` to an object of type `T` with an initial value
570
- `T(forward<Args>(args)...)`.
571
 
572
  *Remarks:* The `shared_ptr` constructors called by these functions
573
  enable `shared_from_this` with the address of the newly constructed
574
  object of type `T`.
575
 
@@ -725,11 +728,11 @@ shared_ptr<double[]> p = make_shared_for_overwrite<double[]>(1024);
725
  // shared_ptr to a default-initialized double[1024], where each element has an indeterminate value
726
  ```
727
 
728
  — *end example*]
729
 
730
- #### Comparison <a id="util.smartptr.shared.cmp">[[util.smartptr.shared.cmp]]</a>
731
 
732
  ``` cpp
733
  template<class T, class U>
734
  bool operator==(const shared_ptr<T>& a, const shared_ptr<U>& b) noexcept;
735
  ```
@@ -748,30 +751,34 @@ template<class T, class U>
748
  strong_ordering operator<=>(const shared_ptr<T>& a, const shared_ptr<U>& b) noexcept;
749
  ```
750
 
751
  *Returns:* `compare_three_way()(a.get(), b.get())`.
752
 
753
- [*Note 1*: Defining a comparison function allows `shared_ptr` objects
754
- to be used as keys in associative containers. — *end note*]
755
 
756
  ``` cpp
757
  template<class T>
758
  strong_ordering operator<=>(const shared_ptr<T>& a, nullptr_t) noexcept;
759
  ```
760
 
761
- *Returns:* `compare_three_way()(a.get(), nullptr)`.
762
 
763
- #### Specialized algorithms <a id="util.smartptr.shared.spec">[[util.smartptr.shared.spec]]</a>
 
 
 
 
764
 
765
  ``` cpp
766
  template<class T>
767
  void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
768
  ```
769
 
770
  *Effects:* Equivalent to `a.swap(b)`.
771
 
772
- #### Casts <a id="util.smartptr.shared.cast">[[util.smartptr.shared.cast]]</a>
773
 
774
  ``` cpp
775
  template<class T, class U>
776
  shared_ptr<T> static_pointer_cast(const shared_ptr<U>& r) noexcept;
777
  template<class T, class U>
@@ -788,11 +795,11 @@ shared_ptr<T>(R, static_cast<typename shared_ptr<T>::element_type*>(r.get()))
788
  ```
789
 
790
  where *`R`* is `r` for the first overload, and `std::move(r)` for the
791
  second.
792
 
793
- [*Note 1*: The seemingly equivalent expression
794
  `shared_ptr<T>(static_cast<T*>(r.get()))` will eventually result in
795
  undefined behavior, attempting to delete the same object
796
  twice. — *end note*]
797
 
798
  ``` cpp
@@ -802,12 +809,12 @@ template<class T, class U>
802
  shared_ptr<T> dynamic_pointer_cast(shared_ptr<U>&& r) noexcept;
803
  ```
804
 
805
  *Mandates:* The expression `dynamic_cast<T*>((U*)nullptr)` is
806
  well-formed. The expression
807
- `dynamic_cast<typename shared_ptr<T>::element_type*>(r.get())` is well
808
- formed.
809
 
810
  *Preconditions:* The expression
811
  `dynamic_cast<typename shared_ptr<T>::element_type*>(r.get())` has
812
  well-defined behavior.
813
 
@@ -816,11 +823,11 @@ well-defined behavior.
816
  - When `dynamic_cast<typename shared_ptr<T>::element_type*>(r.get())`
817
  returns a non-null value `p`, `shared_ptr<T>(`*`R`*`, p)`, where *`R`*
818
  is `r` for the first overload, and `std::move(r)` for the second.
819
  - Otherwise, `shared_ptr<T>()`.
820
 
821
- [*Note 2*: The seemingly equivalent expression
822
  `shared_ptr<T>(dynamic_cast<T*>(r.get()))` will eventually result in
823
  undefined behavior, attempting to delete the same object
824
  twice. — *end note*]
825
 
826
  ``` cpp
@@ -839,11 +846,11 @@ shared_ptr<T>(R, const_cast<typename shared_ptr<T>::element_type*>(r.get()))
839
  ```
840
 
841
  where *`R`* is `r` for the first overload, and `std::move(r)` for the
842
  second.
843
 
844
- [*Note 3*: The seemingly equivalent expression
845
  `shared_ptr<T>(const_cast<T*>(r.get()))` will eventually result in
846
  undefined behavior, attempting to delete the same object
847
  twice. — *end note*]
848
 
849
  ``` cpp
@@ -863,16 +870,16 @@ shared_ptr<T>(R, reinterpret_cast<typename shared_ptr<T>::element_type*>(r.get()
863
  ```
864
 
865
  where *`R`* is `r` for the first overload, and `std::move(r)` for the
866
  second.
867
 
868
- [*Note 4*: The seemingly equivalent expression
869
  `shared_ptr<T>(reinterpret_cast<T*>(r.get()))` will eventually result in
870
  undefined behavior, attempting to delete the same object
871
  twice. — *end note*]
872
 
873
- #### `get_deleter` <a id="util.smartptr.getdeleter">[[util.smartptr.getdeleter]]</a>
874
 
875
  ``` cpp
876
  template<class D, class T>
877
  D* get_deleter(const shared_ptr<T>& p) noexcept;
878
  ```
@@ -880,16 +887,16 @@ template<class D, class T>
880
  *Returns:* If `p` owns a deleter `d` of type cv-unqualified `D`, returns
881
  `addressof(d)`; otherwise returns `nullptr`. The returned pointer
882
  remains valid as long as there exists a `shared_ptr` instance that owns
883
  `d`.
884
 
885
- [*Note 1*: It is unspecified whether the pointer remains valid longer
886
  than that. This can happen if the implementation doesn’t destroy the
887
  deleter until all `weak_ptr` instances that share ownership with `p`
888
  have been destroyed. — *end note*]
889
 
890
- #### I/O <a id="util.smartptr.shared.io">[[util.smartptr.shared.io]]</a>
891
 
892
  ``` cpp
893
  template<class E, class T, class Y>
894
  basic_ostream<E, T>& operator<<(basic_ostream<E, T>& os, const shared_ptr<Y>& p);
895
  ```
 
1
+ #### Class template `shared_ptr` <a id="util.smartptr.shared">[[util.smartptr.shared]]</a>
2
+
3
+ ##### General <a id="util.smartptr.shared.general">[[util.smartptr.shared.general]]</a>
4
 
5
  The `shared_ptr` class template stores a pointer, usually obtained via
6
  `new`. `shared_ptr` implements semantics of shared ownership; the last
7
  remaining owner of the pointer is responsible for destroying the object,
8
  or otherwise releasing the resources associated with the stored pointer.
 
92
  contextually convertible to `bool`, allowing their use in boolean
93
  expressions and declarations in conditions.
94
 
95
  The template parameter `T` of `shared_ptr` may be an incomplete type.
96
 
97
+ [*Note 1*: `T` can be a function type. — *end note*]
98
 
99
  [*Example 1*:
100
 
101
  ``` cpp
102
  if (shared_ptr<X> px = dynamic_pointer_cast<X>(py)) {
 
114
 
115
  For the purposes of subclause [[smartptr]], a pointer type `Y*` is said
116
  to be *compatible with* a pointer type `T*` when either `Y*` is
117
  convertible to `T*` or `Y` is `U[N]` and `T` is cv `U[]`.
118
 
119
+ ##### Constructors <a id="util.smartptr.shared.const">[[util.smartptr.shared.const]]</a>
120
 
121
  In the constructor definitions below, enables `shared_from_this` with
122
  `p`, for a pointer `p` of type `Y*`, means that if `Y` has an
123
  unambiguous and accessible base class that is a specialization of
124
  `enable_shared_from_this` [[util.smartptr.enab]], then `remove_cv_t<Y>*`
 
142
 
143
  ``` cpp
144
  template<class Y> explicit shared_ptr(Y* p);
145
  ```
146
 
 
 
147
  *Constraints:* When `T` is an array type, the expression `delete[] p` is
148
  well-formed and either `T` is `U[N]` and `Y(*)[N]` is convertible to
149
  `T*`, or `T` is `U[]` and `Y(*)[]` is convertible to `T*`. When `T` is
150
  not an array type, the expression `delete p` is well-formed and `Y*` is
151
  convertible to `T*`.
152
 
153
+ *Mandates:* `Y` is a complete type.
154
+
155
  *Preconditions:* The expression `delete[] p`, when `T` is an array type,
156
  or `delete p`, when `T` is not an array type, has well-defined behavior,
157
  and does not throw exceptions.
158
 
159
  *Effects:* When `T` is not an array type, constructs a `shared_ptr`
 
164
  not an array type, `delete[] p` otherwise.
165
 
166
  *Ensures:* `use_count() == 1 && get() == p`.
167
 
168
  *Throws:* `bad_alloc`, or an *implementation-defined* exception when a
169
+ resource other than memory cannot be obtained.
170
 
171
  ``` cpp
172
  template<class Y, class D> shared_ptr(Y* p, D d);
173
  template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
174
  template<class D> shared_ptr(nullptr_t p, D d);
 
184
  - If `T` is not an array type, then `Y*` is convertible to `T*`.
185
 
186
  *Preconditions:* Construction of `d` and a deleter of type `D`
187
  initialized with `std::move(d)` do not throw exceptions. The expression
188
  `d(p)` has well-defined behavior and does not throw exceptions. `A`
189
+ meets the *Cpp17Allocator*
190
+ requirements [[allocator.requirements.general]].
191
 
192
  *Effects:* Constructs a `shared_ptr` object that owns the object `p` and
193
  the deleter `d`. When `T` is not an array type, the first and second
194
  constructors enable `shared_from_this` with `p`. The second and fourth
195
  constructors shall use a copy of `a` to allocate memory for internal
196
  use. If an exception is thrown, `d(p)` is called.
197
 
198
  *Ensures:* `use_count() == 1 && get() == p`.
199
 
200
  *Throws:* `bad_alloc`, or an *implementation-defined* exception when a
201
+ resource other than memory cannot be obtained.
202
 
203
  ``` cpp
204
  template<class Y> shared_ptr(const shared_ptr<Y>& r, element_type* p) noexcept;
205
  template<class Y> shared_ptr(shared_ptr<Y>&& r, element_type* p) noexcept;
206
  ```
 
209
  ownership with the initial value of `r`.
210
 
211
  *Ensures:* `get() == p`. For the second overload, `r` is empty and
212
  `r.get() == nullptr`.
213
 
214
+ [*Note 1*: Use of this constructor leads to a dangling pointer unless
215
+ `p` remains valid at least until the ownership group of `r` is
216
+ destroyed. — *end note*]
217
 
218
  [*Note 2*: This constructor allows creation of an empty `shared_ptr`
219
  instance with a non-null stored pointer. — *end note*]
220
 
221
  ``` cpp
 
238
 
239
  *Constraints:* For the second constructor, `Y*` is compatible with `T*`.
240
 
241
  *Effects:* Move constructs a `shared_ptr` instance from `r`.
242
 
243
+ *Ensures:* `*this` contains the old value of `r`. `r` is empty, and
244
+ `r.get() == nullptr`.
245
 
246
  ``` cpp
247
  template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
248
  ```
249
 
 
264
  *Constraints:* `Y*` is compatible with `T*` and
265
  `unique_ptr<Y, D>::pointer` is convertible to `element_type*`.
266
 
267
  *Effects:* If `r.get() == nullptr`, equivalent to `shared_ptr()`.
268
  Otherwise, if `D` is not a reference type, equivalent to
269
+ `shared_ptr(r.release(), std::move(r.get_deleter()))`. Otherwise,
270
+ equivalent to `shared_ptr(r.release(), ref(r.get_deleter()))`. If an
271
+ exception is thrown, the constructor has no effect.
272
 
273
+ ##### Destructor <a id="util.smartptr.shared.dest">[[util.smartptr.shared.dest]]</a>
274
 
275
  ``` cpp
276
  ~shared_ptr();
277
  ```
278
 
 
282
  instance (`use_count() > 1`), there are no side effects.
283
  - Otherwise, if `*this` owns an object `p` and a deleter `d`, `d(p)` is
284
  called.
285
  - Otherwise, `*this` owns a pointer `p`, and `delete p` is called.
286
 
287
+ [*Note 2*: Since the destruction of `*this` decreases the number of
288
  instances that share ownership with `*this` by one, after `*this` has
289
  been destroyed all `shared_ptr` instances that shared ownership with
290
  `*this` will report a `use_count()` that is one less than its previous
291
  value. — *end note*]
292
 
293
+ ##### Assignment <a id="util.smartptr.shared.assign">[[util.smartptr.shared.assign]]</a>
294
 
295
  ``` cpp
296
  shared_ptr& operator=(const shared_ptr& r) noexcept;
297
  template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
298
  ```
299
 
300
  *Effects:* Equivalent to `shared_ptr(r).swap(*this)`.
301
 
302
  *Returns:* `*this`.
303
 
304
+ [*Note 3*:
305
 
306
  The use count updates caused by the temporary object construction and
307
+ destruction are not observable side effects, so the implementation can
308
  meet the effects (and the implied guarantees) via different means,
309
  without creating a temporary. In particular, in the example:
310
 
311
  ``` cpp
312
  shared_ptr<int> p(new int);
313
  shared_ptr<void> q(p);
314
  p = p;
315
  q = p;
316
  ```
317
 
318
+ both assignments can be no-ops.
319
 
320
  — *end note*]
321
 
322
  ``` cpp
323
  shared_ptr& operator=(shared_ptr&& r) noexcept;
 
334
 
335
  *Effects:* Equivalent to `shared_ptr(std::move(r)).swap(*this)`.
336
 
337
  *Returns:* `*this`.
338
 
339
+ ##### Modifiers <a id="util.smartptr.shared.mod">[[util.smartptr.shared.mod]]</a>
340
 
341
  ``` cpp
342
  void swap(shared_ptr& r) noexcept;
343
  ```
344
 
 
366
  template<class Y, class D, class A> void reset(Y* p, D d, A a);
367
  ```
368
 
369
  *Effects:* Equivalent to `shared_ptr(p, d, a).swap(*this)`.
370
 
371
+ ##### Observers <a id="util.smartptr.shared.obs">[[util.smartptr.shared.obs]]</a>
372
 
373
  ``` cpp
374
  element_type* get() const noexcept;
375
  ```
376
 
 
378
 
379
  ``` cpp
380
  T& operator*() const noexcept;
381
  ```
382
 
383
+ *Preconditions:* `get() != nullptr`.
384
 
385
  *Returns:* `*get()`.
386
 
387
  *Remarks:* When `T` is an array type or cv `void`, it is unspecified
388
  whether this member function is declared. If it is declared, it is
 
392
 
393
  ``` cpp
394
  T* operator->() const noexcept;
395
  ```
396
 
397
+ *Preconditions:* `get() != nullptr`.
398
 
399
  *Returns:* `get()`.
400
 
401
  *Remarks:* When `T` is an array type, it is unspecified whether this
402
  member function is declared. If it is declared, it is unspecified what
 
405
 
406
  ``` cpp
407
  element_type& operator[](ptrdiff_t i) const;
408
  ```
409
 
410
+ *Preconditions:* `get() != nullptr && i >= 0`. If `T` is `U[N]`,
411
+ `i < N`.
412
 
413
  *Returns:* `get()[i]`.
414
 
 
 
 
 
 
415
  *Throws:* Nothing.
416
 
417
+ *Remarks:* When `T` is not an array type, it is unspecified whether this
418
+ member function is declared. If it is declared, it is unspecified what
419
+ its return type is, except that the declaration (although not
420
+ necessarily the definition) of the function shall be well-formed.
421
+
422
  ``` cpp
423
  long use_count() const noexcept;
424
  ```
425
 
426
+ *Synchronization:* None.
427
+
428
  *Returns:* The number of `shared_ptr` objects, `*this` included, that
429
  share ownership with `*this`, or `0` when `*this` is empty.
430
 
431
+ [*Note 4*: `get() == nullptr` does not imply a specific return value of
 
 
432
  `use_count()`. — *end note*]
433
 
434
+ [*Note 5*: `weak_ptr<T>::lock()` can affect the return value of
435
  `use_count()`. — *end note*]
436
 
437
+ [*Note 6*: When multiple threads might affect the return value of
438
+ `use_count()`, the result is approximate. In particular,
439
+ `use_count() == 1` does not imply that accesses through a previously
440
+ destroyed `shared_ptr` have in any sense completed. — *end note*]
 
441
 
442
  ``` cpp
443
  explicit operator bool() const noexcept;
444
  ```
445
 
446
+ *Returns:* `get() != nullptr`.
447
 
448
  ``` cpp
449
  template<class U> bool owner_before(const shared_ptr<U>& b) const noexcept;
450
  template<class U> bool owner_before(const weak_ptr<U>& b) const noexcept;
451
  ```
 
457
  - under the equivalence relation defined by `owner_before`,
458
  `!a.owner_before(b) && !b.owner_before(a)`, two `shared_ptr` or
459
  `weak_ptr` instances are equivalent if and only if they share
460
  ownership or are both empty.
461
 
462
+ ##### Creation <a id="util.smartptr.shared.create">[[util.smartptr.shared.create]]</a>
463
 
464
  The common requirements that apply to all `make_shared`,
465
  `allocate_shared`, `make_shared_for_overwrite`, and
466
  `allocate_shared_for_overwrite` overloads, unless specified otherwise,
467
  are described below.
 
475
  shared_ptr<T> make_shared_for_overwrite(args);
476
  template<class T, class A, ...>
477
  shared_ptr<T> allocate_shared_for_overwrite(const A& a, args);
478
  ```
479
 
480
+ *Preconditions:* `A` meets the *Cpp17Allocator*
481
+ requirements [[allocator.requirements.general]].
482
 
483
  *Effects:* Allocates memory for an object of type `T` (or `U[N]` when
484
  `T` is `U[]`, where `N` is determined from *args* as specified by the
485
  concrete overload). The object is initialized from *args* as specified
486
  by the concrete overload. The `allocate_shared` and
487
  `allocate_shared_for_overwrite` templates use a copy of `a` (rebound for
488
  an unspecified `value_type`) to allocate memory. If an exception is
489
  thrown, the functions have no effect.
490
 
491
+ *Ensures:* `r.get() != nullptr && r.use_count() == 1`, where `r` is the
492
+ return value.
493
+
494
  *Returns:* A `shared_ptr` instance that stores and owns the address of
495
  the newly constructed object.
496
 
 
 
 
497
  *Throws:* `bad_alloc`, or an exception thrown from `allocate` or from
498
  the initialization of the object.
499
 
500
  *Remarks:*
501
 
502
  - Implementations should perform no more than one memory allocation.
503
+ \[*Note 3*: This provides efficiency equivalent to an intrusive smart
504
  pointer. — *end note*]
505
  - When an object of an array type `U` is specified to have an initial
506
  value of `u` (of the same type), this shall be interpreted to mean
507
  that each array element of the object has as its initial value the
508
  corresponding element from `u`.
 
554
  expression `allocator_traits<A2>::destroy(a2, pv)` where `pv` points
555
  to that object of type `remove_cv_t<U>` and `a2` of type `A2` is a
556
  rebound copy of the allocator `a` passed to `allocate_shared` such
557
  that its `value_type` is `remove_cv_t<U>`.
558
 
559
+ [*Note 7*: These functions will typically allocate more memory than
560
  `sizeof(T)` to allow for internal bookkeeping structures such as
561
  reference counts. — *end note*]
562
 
563
  ``` cpp
564
  template<class T, class... Args>
 
568
  ```
569
 
570
  *Constraints:* `T` is not an array type.
571
 
572
  *Returns:* A `shared_ptr` to an object of type `T` with an initial value
573
+ `T(std::forward<Args>(args)...)`.
574
 
575
  *Remarks:* The `shared_ptr` constructors called by these functions
576
  enable `shared_from_this` with the address of the newly constructed
577
  object of type `T`.
578
 
 
728
  // shared_ptr to a default-initialized double[1024], where each element has an indeterminate value
729
  ```
730
 
731
  — *end example*]
732
 
733
+ ##### Comparison <a id="util.smartptr.shared.cmp">[[util.smartptr.shared.cmp]]</a>
734
 
735
  ``` cpp
736
  template<class T, class U>
737
  bool operator==(const shared_ptr<T>& a, const shared_ptr<U>& b) noexcept;
738
  ```
 
751
  strong_ordering operator<=>(const shared_ptr<T>& a, const shared_ptr<U>& b) noexcept;
752
  ```
753
 
754
  *Returns:* `compare_three_way()(a.get(), b.get())`.
755
 
756
+ [*Note 8*: Defining a comparison operator function allows `shared_ptr`
757
+ objects to be used as keys in associative containers. — *end note*]
758
 
759
  ``` cpp
760
  template<class T>
761
  strong_ordering operator<=>(const shared_ptr<T>& a, nullptr_t) noexcept;
762
  ```
763
 
764
+ *Returns:*
765
 
766
+ ``` cpp
767
+ compare_three_way()(a.get(), static_cast<typename shared_ptr<T>::element_type*>(nullptr).
768
+ ```
769
+
770
+ ##### Specialized algorithms <a id="util.smartptr.shared.spec">[[util.smartptr.shared.spec]]</a>
771
 
772
  ``` cpp
773
  template<class T>
774
  void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
775
  ```
776
 
777
  *Effects:* Equivalent to `a.swap(b)`.
778
 
779
+ ##### Casts <a id="util.smartptr.shared.cast">[[util.smartptr.shared.cast]]</a>
780
 
781
  ``` cpp
782
  template<class T, class U>
783
  shared_ptr<T> static_pointer_cast(const shared_ptr<U>& r) noexcept;
784
  template<class T, class U>
 
795
  ```
796
 
797
  where *`R`* is `r` for the first overload, and `std::move(r)` for the
798
  second.
799
 
800
+ [*Note 9*: The seemingly equivalent expression
801
  `shared_ptr<T>(static_cast<T*>(r.get()))` will eventually result in
802
  undefined behavior, attempting to delete the same object
803
  twice. — *end note*]
804
 
805
  ``` cpp
 
809
  shared_ptr<T> dynamic_pointer_cast(shared_ptr<U>&& r) noexcept;
810
  ```
811
 
812
  *Mandates:* The expression `dynamic_cast<T*>((U*)nullptr)` is
813
  well-formed. The expression
814
+ `dynamic_cast<typename shared_ptr<T>::element_type*>(r.get())` is
815
+ well-formed.
816
 
817
  *Preconditions:* The expression
818
  `dynamic_cast<typename shared_ptr<T>::element_type*>(r.get())` has
819
  well-defined behavior.
820
 
 
823
  - When `dynamic_cast<typename shared_ptr<T>::element_type*>(r.get())`
824
  returns a non-null value `p`, `shared_ptr<T>(`*`R`*`, p)`, where *`R`*
825
  is `r` for the first overload, and `std::move(r)` for the second.
826
  - Otherwise, `shared_ptr<T>()`.
827
 
828
+ [*Note 10*: The seemingly equivalent expression
829
  `shared_ptr<T>(dynamic_cast<T*>(r.get()))` will eventually result in
830
  undefined behavior, attempting to delete the same object
831
  twice. — *end note*]
832
 
833
  ``` cpp
 
846
  ```
847
 
848
  where *`R`* is `r` for the first overload, and `std::move(r)` for the
849
  second.
850
 
851
+ [*Note 11*: The seemingly equivalent expression
852
  `shared_ptr<T>(const_cast<T*>(r.get()))` will eventually result in
853
  undefined behavior, attempting to delete the same object
854
  twice. — *end note*]
855
 
856
  ``` cpp
 
870
  ```
871
 
872
  where *`R`* is `r` for the first overload, and `std::move(r)` for the
873
  second.
874
 
875
+ [*Note 12*: The seemingly equivalent expression
876
  `shared_ptr<T>(reinterpret_cast<T*>(r.get()))` will eventually result in
877
  undefined behavior, attempting to delete the same object
878
  twice. — *end note*]
879
 
880
+ ##### `get_deleter` <a id="util.smartptr.getdeleter">[[util.smartptr.getdeleter]]</a>
881
 
882
  ``` cpp
883
  template<class D, class T>
884
  D* get_deleter(const shared_ptr<T>& p) noexcept;
885
  ```
 
887
  *Returns:* If `p` owns a deleter `d` of type cv-unqualified `D`, returns
888
  `addressof(d)`; otherwise returns `nullptr`. The returned pointer
889
  remains valid as long as there exists a `shared_ptr` instance that owns
890
  `d`.
891
 
892
+ [*Note 13*: It is unspecified whether the pointer remains valid longer
893
  than that. This can happen if the implementation doesn’t destroy the
894
  deleter until all `weak_ptr` instances that share ownership with `p`
895
  have been destroyed. — *end note*]
896
 
897
+ ##### I/O <a id="util.smartptr.shared.io">[[util.smartptr.shared.io]]</a>
898
 
899
  ``` cpp
900
  template<class E, class T, class Y>
901
  basic_ostream<E, T>& operator<<(basic_ostream<E, T>& os, const shared_ptr<Y>& p);
902
  ```