Brian Silverman | 355f11d | 2018-08-04 23:57:00 -0700 | [diff] [blame^] | 1 | //// |
| 2 | Copyright 1999 Greg Colvin and Beman Dawes |
| 3 | Copyright 2002 Darin Adler |
| 4 | Copyright 2002-2017 Peter Dimov |
| 5 | |
| 6 | Distributed under the Boost Software License, Version 1.0. |
| 7 | |
| 8 | See accompanying file LICENSE_1_0.txt or copy at |
| 9 | http://www.boost.org/LICENSE_1_0.txt |
| 10 | //// |
| 11 | |
| 12 | [#shared_ptr] |
| 13 | # shared_ptr: Shared Ownership |
| 14 | :toc: |
| 15 | :toc-title: |
| 16 | :idprefix: shared_ptr_ |
| 17 | |
| 18 | ## Description |
| 19 | |
| 20 | The `shared_ptr` class template stores a pointer to a dynamically allocated object, typically with a {cpp} `new`-expression. |
| 21 | The object pointed to is guaranteed to be deleted when the last `shared_ptr` pointing to it is destroyed or reset. |
| 22 | |
| 23 | .Using shared_ptr |
| 24 | ``` |
| 25 | shared_ptr<X> p1( new X ); |
| 26 | shared_ptr<void> p2( new int(5) ); |
| 27 | ``` |
| 28 | |
| 29 | `shared_ptr` deletes the exact pointer that has been passed at construction time, complete with its original type, regardless |
| 30 | of the template parameter. In the second example above, when `p2` is destroyed or reset, it will call `delete` on the original |
| 31 | `int*` that has been passed to the constructor, even though `p2` itself is of type `shared_ptr<void>` and stores a pointer of |
| 32 | type `void*`. |
| 33 | |
| 34 | Every `shared_ptr` meets the `CopyConstructible`, `MoveConstructible`, `CopyAssignable` and `MoveAssignable` requirements of the |
| 35 | {cpp} Standard Library, and can be used in standard library containers. Comparison operators are supplied so that `shared_ptr` |
| 36 | works with the standard library's associative containers. |
| 37 | |
| 38 | Because the implementation uses reference counting, cycles of `shared_ptr` instances will not be reclaimed. For example, if `main()` |
| 39 | holds a `shared_ptr` to `A`, which directly or indirectly holds a `shared_ptr` back to `A`, `A`'s use count will be 2. Destruction |
| 40 | of the original `shared_ptr` will leave `A` dangling with a use count of 1. Use `<<weak_ptr,weak_ptr>>` to "break cycles." |
| 41 | |
| 42 | The class template is parameterized on `T`, the type of the object pointed to. `shared_ptr` and most of its member functions place |
| 43 | no requirements on `T`; it is allowed to be an incomplete type, or `void`. Member functions that do place additional requirements |
| 44 | (constructors, `reset`) are explicitly documented below. |
| 45 | |
| 46 | `shared_ptr<T>` can be implicitly converted to `shared_ptr<U>` whenever `T*` can be implicitly converted to `U*`. In particular, |
| 47 | `shared_ptr<T>` is implicitly convertible to `shared_ptr<T const>`, to `shared_ptr<U>` where `U` is an accessible base of `T`, |
| 48 | and to `shared_ptr<void>`. |
| 49 | |
| 50 | `shared_ptr` is now part of the C++11 Standard, as `std::shared_ptr`. |
| 51 | |
| 52 | Starting with Boost release 1.53, `shared_ptr` can be used to hold a pointer to a dynamically allocated array. This is accomplished |
| 53 | by using an array type (`T[]` or `T[N]`) as the template parameter. There is almost no difference between using an unsized array, |
| 54 | `T[]`, and a sized array, `T[N]`; the latter just enables `operator[]` to perform a range check on the index. |
| 55 | |
| 56 | .Using shared_ptr with arrays |
| 57 | ``` |
| 58 | shared_ptr<double[1024]> p1( new double[1024] ); |
| 59 | shared_ptr<double[]> p2( new double[n] ); |
| 60 | ``` |
| 61 | |
| 62 | ## Best Practices |
| 63 | |
| 64 | A simple guideline that nearly eliminates the possibility of memory leaks is: always use a named smart pointer variable to hold the result |
| 65 | of `new`. Every occurence of the `new` keyword in the code should have the form: |
| 66 | |
| 67 | shared_ptr<T> p(new Y); |
| 68 | |
| 69 | It is, of course, acceptable to use another smart pointer in place of `shared_ptr` above; having `T` and `Y` be the same type, or passing |
| 70 | arguments to the constructor of `Y` is also OK. |
| 71 | |
| 72 | If you observe this guideline, it naturally follows that you will have no explicit `delete` statements; `try`/`catch` constructs will be rare. |
| 73 | |
| 74 | Avoid using unnamed `shared_ptr` temporaries to save typing; to see why this is dangerous, consider this example: |
| 75 | |
| 76 | .Exception-safe and -unsafe use of shared_ptr |
| 77 | ``` |
| 78 | void f(shared_ptr<int>, int); |
| 79 | int g(); |
| 80 | |
| 81 | void ok() |
| 82 | { |
| 83 | shared_ptr<int> p( new int(2) ); |
| 84 | f( p, g() ); |
| 85 | } |
| 86 | |
| 87 | void bad() |
| 88 | { |
| 89 | f( shared_ptr<int>( new int(2) ), g() ); |
| 90 | } |
| 91 | ``` |
| 92 | |
| 93 | The function `ok` follows the guideline to the letter, whereas `bad` constructs the temporary `shared_ptr` in place, admitting the possibility of |
| 94 | a memory leak. Since function arguments are evaluated in unspecified order, it is possible for `new int(2)` to be evaluated first, `g()` second, |
| 95 | and we may never get to the `shared_ptr` constructor if `g` throws an exception. See http://www.gotw.ca/gotw/056.htm[Herb Sutter's treatment] of |
| 96 | the issue for more information. |
| 97 | |
| 98 | The exception safety problem described above may also be eliminated by using the `<<make_shared,make_shared>>` or `allocate_shared` factory |
| 99 | functions defined in `<boost/smart_ptr/make_shared.hpp>`. These factory functions also provide an efficiency benefit by consolidating allocations. |
| 100 | |
| 101 | ## Synopsis |
| 102 | |
| 103 | `shared_ptr` is defined in `<boost/smart_ptr/shared_ptr.hpp>`. |
| 104 | |
| 105 | ``` |
| 106 | namespace boost { |
| 107 | |
| 108 | class bad_weak_ptr: public std::exception; |
| 109 | |
| 110 | template<class T> class weak_ptr; |
| 111 | |
| 112 | template<class T> class shared_ptr { |
| 113 | public: |
| 114 | |
| 115 | typedef /*see below*/ element_type; |
| 116 | |
| 117 | constexpr shared_ptr() noexcept; |
| 118 | constexpr shared_ptr(std::nullptr_t) noexcept; |
| 119 | |
| 120 | template<class Y> explicit shared_ptr(Y * p); |
| 121 | template<class Y, class D> shared_ptr(Y * p, D d); |
| 122 | template<class Y, class D, class A> shared_ptr(Y * p, D d, A a); |
| 123 | template<class D> shared_ptr(std::nullptr_t p, D d); |
| 124 | template<class D, class A> shared_ptr(std::nullptr_t p, D d, A a); |
| 125 | |
| 126 | ~shared_ptr() noexcept; |
| 127 | |
| 128 | shared_ptr(shared_ptr const & r) noexcept; |
| 129 | template<class Y> shared_ptr(shared_ptr<Y> const & r) noexcept; |
| 130 | |
| 131 | shared_ptr(shared_ptr && r) noexcept; |
| 132 | template<class Y> shared_ptr(shared_ptr<Y> && r) noexcept; |
| 133 | |
| 134 | template<class Y> shared_ptr(shared_ptr<Y> const & r, element_type * p) noexcept; |
| 135 | |
| 136 | template<class Y> shared_ptr(shared_ptr<Y> && r, element_type * p) noexcept; |
| 137 | |
| 138 | template<class Y> explicit shared_ptr(weak_ptr<Y> const & r); |
| 139 | |
| 140 | template<class Y> explicit shared_ptr(std::auto_ptr<Y> & r); |
| 141 | template<class Y> shared_ptr(std::auto_ptr<Y> && r); |
| 142 | |
| 143 | template<class Y, class D> shared_ptr(std::unique_ptr<Y, D> && r); |
| 144 | |
| 145 | shared_ptr & operator=(shared_ptr const & r) noexcept; |
| 146 | template<class Y> shared_ptr & operator=(shared_ptr<Y> const & r) noexcept; |
| 147 | |
| 148 | shared_ptr & operator=(shared_ptr const && r) noexcept; |
| 149 | template<class Y> shared_ptr & operator=(shared_ptr<Y> const && r) noexcept; |
| 150 | |
| 151 | template<class Y> shared_ptr & operator=(std::auto_ptr<Y> & r); |
| 152 | template<class Y> shared_ptr & operator=(std::auto_ptr<Y> && r); |
| 153 | |
| 154 | template<class Y, class D> shared_ptr & operator=(std::unique_ptr<Y, D> && r); |
| 155 | |
| 156 | shared_ptr & operator=(std::nullptr_t) noexcept; |
| 157 | |
| 158 | void reset() noexcept; |
| 159 | |
| 160 | template<class Y> void reset(Y * p); |
| 161 | template<class Y, class D> void reset(Y * p, D d); |
| 162 | template<class Y, class D, class A> void reset(Y * p, D d, A a); |
| 163 | |
| 164 | template<class Y> void reset(shared_ptr<Y> const & r, element_type * p) noexcept; |
| 165 | template<class Y> void reset(shared_ptr<Y> && r, element_type * p) noexcept; |
| 166 | |
| 167 | T & operator*() const noexcept; // only valid when T is not an array type |
| 168 | T * operator->() const noexcept; // only valid when T is not an array type |
| 169 | |
| 170 | // only valid when T is an array type |
| 171 | element_type & operator[](std::ptrdiff_t i) const noexcept; |
| 172 | |
| 173 | element_type * get() const noexcept; |
| 174 | |
| 175 | bool unique() const noexcept; |
| 176 | long use_count() const noexcept; |
| 177 | |
| 178 | explicit operator bool() const noexcept; |
| 179 | |
| 180 | void swap(shared_ptr & b) noexcept; |
| 181 | |
| 182 | template<class Y> bool owner_before(shared_ptr<Y> const & rhs) const noexcept; |
| 183 | template<class Y> bool owner_before(weak_ptr<Y> const & rhs) const noexcept; |
| 184 | }; |
| 185 | |
| 186 | template<class T, class U> |
| 187 | bool operator==(shared_ptr<T> const & a, shared_ptr<U> const & b) noexcept; |
| 188 | |
| 189 | template<class T, class U> |
| 190 | bool operator!=(shared_ptr<T> const & a, shared_ptr<U> const & b) noexcept; |
| 191 | |
| 192 | template<class T, class U> |
| 193 | bool operator<(shared_ptr<T> const & a, shared_ptr<U> const & b) noexcept; |
| 194 | |
| 195 | template<class T> bool operator==(shared_ptr<T> const & p, std::nullptr_t) noexcept; |
| 196 | template<class T> bool operator==(std::nullptr_t, shared_ptr<T> const & p) noexcept; |
| 197 | |
| 198 | template<class T> bool operator!=(shared_ptr<T> const & p, std::nullptr_t) noexcept; |
| 199 | template<class T> bool operator!=(std::nullptr_t, shared_ptr<T> const & p) noexcept; |
| 200 | |
| 201 | template<class T> void swap(shared_ptr<T> & a, shared_ptr<T> & b) noexcept; |
| 202 | |
| 203 | template<class T> |
| 204 | typename shared_ptr<T>::element_type * |
| 205 | get_pointer(shared_ptr<T> const & p) noexcept; |
| 206 | |
| 207 | template<class T, class U> |
| 208 | shared_ptr<T> static_pointer_cast(shared_ptr<U> const & r) noexcept; |
| 209 | |
| 210 | template<class T, class U> |
| 211 | shared_ptr<T> const_pointer_cast(shared_ptr<U> const & r) noexcept; |
| 212 | |
| 213 | template<class T, class U> |
| 214 | shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const & r) noexcept; |
| 215 | |
| 216 | template<class T, class U> |
| 217 | shared_ptr<T> reinterpret_pointer_cast(shared_ptr<U> const & r) noexcept; |
| 218 | |
| 219 | template<class E, class T, class Y> |
| 220 | std::basic_ostream<E, T> & |
| 221 | operator<< (std::basic_ostream<E, T> & os, shared_ptr<Y> const & p); |
| 222 | |
| 223 | template<class D, class T> D * get_deleter(shared_ptr<T> const & p) noexcept; |
| 224 | |
| 225 | template<class T> bool atomic_is_lock_free( shared_ptr<T> const * p ) noexcept; |
| 226 | |
| 227 | template<class T> shared_ptr<T> atomic_load( shared_ptr<T> const * p ) noexcept; |
| 228 | template<class T> |
| 229 | shared_ptr<T> atomic_load_explicit( shared_ptr<T> const * p, int ) noexcept; |
| 230 | |
| 231 | template<class T> |
| 232 | void atomic_store( shared_ptr<T> * p, shared_ptr<T> r ) noexcept; |
| 233 | template<class T> |
| 234 | void atomic_store_explicit( shared_ptr<T> * p, shared_ptr<T> r, int ) noexcept; |
| 235 | |
| 236 | template<class T> |
| 237 | shared_ptr<T> atomic_exchange( shared_ptr<T> * p, shared_ptr<T> r ) noexcept; |
| 238 | template<class T> |
| 239 | shared_ptr<T> atomic_exchange_explicit( |
| 240 | shared_ptr<T> * p, shared_ptr<T> r, int ) noexcept; |
| 241 | |
| 242 | template<class T> |
| 243 | bool atomic_compare_exchange( |
| 244 | shared_ptr<T> * p, shared_ptr<T> * v, shared_ptr<T> w ) noexcept; |
| 245 | template<class T> |
| 246 | bool atomic_compare_exchange_explicit( |
| 247 | shared_ptr<T> * p, shared_ptr<T> * v, shared_ptr<T> w, int, int ) noexcept; |
| 248 | } |
| 249 | ``` |
| 250 | |
| 251 | ## Members |
| 252 | |
| 253 | ### element_type |
| 254 | ``` |
| 255 | typedef ... element_type; |
| 256 | ``` |
| 257 | `element_type` is `T` when `T` is not an array type, and `U` when `T` is `U[]` or `U[N]`. |
| 258 | |
| 259 | ### default constructor |
| 260 | ``` |
| 261 | constexpr shared_ptr() noexcept; |
| 262 | ``` |
| 263 | ``` |
| 264 | constexpr shared_ptr(std::nullptr_t) noexcept; |
| 265 | ``` |
| 266 | [none] |
| 267 | * {blank} |
| 268 | + |
| 269 | Effects:: Constructs an empty `shared_ptr`. |
| 270 | Postconditions:: `use_count() == 0 && get() == 0`. |
| 271 | |
| 272 | ### pointer constructor |
| 273 | ``` |
| 274 | template<class Y> explicit shared_ptr(Y * p); |
| 275 | ``` |
| 276 | [none] |
| 277 | * {blank} |
| 278 | + |
| 279 | Requires:: `Y` must be a complete type. The expression `delete[] p`, when `T` is an array type, or `delete p`, when `T` is not an array type, |
| 280 | must be well-formed, well-defined, and not throw exceptions. When `T` is `U[N]`, `Y(\*)[N]` must be convertible to `T*`; when `T` is `U[]`, `Y(\*)[]` |
| 281 | must be convertible to `T*`; otherwise, `Y\*` must be convertible to `T*`. |
| 282 | |
| 283 | Effects:: When `T` is not an array type, constructs a `shared_ptr` that owns the pointer `p`. Otherwise, constructs a `shared_ptr` that owns `p` and |
| 284 | a deleter of an unspecified type that calls `delete[] p`. |
| 285 | |
| 286 | Postconditions:: `use_count() == 1 && get() == p`. If `T` is not an array type and `p` is unambiguously convertible to `enable_shared_from_this<V>*` |
| 287 | for some `V`, `p\->shared_from_this()` returns a copy of `*this`. |
| 288 | |
| 289 | Throws:: `std::bad_alloc`, or an implementation-defined exception when a resource other than memory could not be obtained. |
| 290 | |
| 291 | Exception safety:: If an exception is thrown, the constructor calls `delete[] p`, when `T` is an array type, or `delete p`, when `T` is not an array type. |
| 292 | |
| 293 | NOTE: `p` must be a pointer to an object that was allocated via a {cpp} `new` expression or be 0. The postcondition that use count is 1 holds even if `p` |
| 294 | is 0; invoking `delete` on a pointer that has a value of 0 is harmless. |
| 295 | |
| 296 | NOTE: This constructor is a template in order to remember the actual pointer type passed. The destructor will call delete with the same pointer, complete |
| 297 | with its original type, even when `T` does not have a virtual destructor, or is `void`. |
| 298 | |
| 299 | ### constructors taking a deleter |
| 300 | ``` |
| 301 | template<class Y, class D> shared_ptr(Y * p, D d); |
| 302 | ``` |
| 303 | ``` |
| 304 | template<class Y, class D, class A> shared_ptr(Y * p, D d, A a); |
| 305 | ``` |
| 306 | ``` |
| 307 | template<class D> shared_ptr(std::nullptr_t p, D d); |
| 308 | ``` |
| 309 | ``` |
| 310 | template<class D, class A> shared_ptr(std::nullptr_t p, D d, A a); |
| 311 | ``` |
| 312 | [none] |
| 313 | * {blank} |
| 314 | + |
| 315 | Requires:: `D` must be `CopyConstructible`. The copy constructor and destructor of `D` must not throw. The expression `d(p)` must be well-formed, well-defined, |
| 316 | and not throw exceptions. `A` must be an `Allocator`, as described in section Allocator Requirements [allocator.requirements] of the {cpp} Standard. |
| 317 | When `T` is `U[N]`, `Y(\*)[N]` must be convertible to `T*`; when `T` is `U[]`, `Y(\*)[]` must be convertible to `T*`; otherwise, `Y\*` must be convertible to `T*`. |
| 318 | |
| 319 | Effects:: Constructs a `shared_ptr` that owns the pointer `p` and the deleter `d`. The constructors taking an allocator a allocate memory using a copy of `a`. |
| 320 | |
| 321 | Postconditions:: `use_count() == 1 && get() == p`. If `T` is not an array type and `p` is unambiguously convertible to `enable_shared_from_this<V>*` for some `V`, |
| 322 | `p\->shared_from_this()` returns a copy of `*this`. |
| 323 | |
| 324 | Throws:: `std::bad_alloc`, or an implementation-defined exception when a resource other than memory could not be obtained. |
| 325 | |
| 326 | Exception safety:: If an exception is thrown, `d(p)` is called. |
| 327 | |
| 328 | NOTE: When the the time comes to delete the object pointed to by `p`, the stored copy of `d` is invoked with the stored copy of `p` as an argument. |
| 329 | |
| 330 | NOTE: Custom deallocators allow a factory function returning a `shared_ptr` to insulate the user from its memory allocation strategy. Since the deallocator |
| 331 | is not part of the type, changing the allocation strategy does not break source or binary compatibility, and does not require a client recompilation. For example, |
| 332 | a "no-op" deallocator is useful when returning a `shared_ptr` to a statically allocated object, and other variations allow a `shared_ptr` to be used as a wrapper |
| 333 | for another smart pointer, easing interoperability. |
| 334 | |
| 335 | NOTE: The requirement that the copy constructor of `D` does not throw comes from the pass by value. If the copy constructor throws, the pointer would leak. |
| 336 | |
| 337 | ### copy and converting constructors |
| 338 | ``` |
| 339 | shared_ptr(shared_ptr const & r) noexcept; |
| 340 | ``` |
| 341 | ``` |
| 342 | template<class Y> shared_ptr(shared_ptr<Y> const & r) noexcept; |
| 343 | ``` |
| 344 | [none] |
| 345 | * {blank} |
| 346 | + |
| 347 | Requires:: `Y*` should be convertible to `T*`. |
| 348 | |
| 349 | Effects:: If `r` is empty, constructs an empty `shared_ptr`; otherwise, constructs a `shared_ptr` that shares ownership with `r`. |
| 350 | |
| 351 | Postconditions:: `get() == r.get() && use_count() == r.use_count()`. |
| 352 | |
| 353 | ### move constructors |
| 354 | ``` |
| 355 | shared_ptr(shared_ptr && r) noexcept; |
| 356 | ``` |
| 357 | ``` |
| 358 | template<class Y> shared_ptr(shared_ptr<Y> && r) noexcept; |
| 359 | ``` |
| 360 | [none] |
| 361 | * {blank} |
| 362 | + |
| 363 | Requires:: `Y*` should be convertible to `T*`. |
| 364 | |
| 365 | Effects:: Move-constructs a `shared_ptr` from `r`. |
| 366 | |
| 367 | Postconditions:: `*this` contains the old value of `r`. `r` is empty and `r.get() == 0`. |
| 368 | |
| 369 | ### aliasing constructor |
| 370 | ``` |
| 371 | template<class Y> shared_ptr(shared_ptr<Y> const & r, element_type * p) noexcept; |
| 372 | ``` |
| 373 | [none] |
| 374 | * {blank} |
| 375 | + |
| 376 | Effects:: constructs a `shared_ptr` that shares ownership with `r` and stores `p`. |
| 377 | |
| 378 | Postconditions:: `get() == p && use_count() == r.use_count()`. |
| 379 | |
| 380 | ### aliasing move constructor |
| 381 | ``` |
| 382 | template<class Y> shared_ptr(shared_ptr<Y> && r, element_type * p) noexcept; |
| 383 | ``` |
| 384 | [none] |
| 385 | * {blank} |
| 386 | + |
| 387 | Effects:: Move-constructs a `shared_ptr` from `r`, while storing `p` instead. |
| 388 | |
| 389 | Postconditions:: `get() == p` and `use_count()` equals the old count of `r`. `r` is empty and `r.get() == 0`. |
| 390 | |
| 391 | ### weak_ptr constructor |
| 392 | ``` |
| 393 | template<class Y> explicit shared_ptr(weak_ptr<Y> const & r); |
| 394 | ``` |
| 395 | [none] |
| 396 | * {blank} |
| 397 | + |
| 398 | Requires:: `Y*` should be convertible to `T*`. |
| 399 | |
| 400 | Effects:: Constructs a `shared_ptr` that shares ownership with `r` and stores a copy of the pointer stored in `r`. |
| 401 | |
| 402 | Postconditions:: `use_count() == r.use_count()`. |
| 403 | |
| 404 | Throws:: `bad_weak_ptr` when `r.use_count() == 0`. |
| 405 | |
| 406 | Exception safety:: If an exception is thrown, the constructor has no effect. |
| 407 | |
| 408 | ### auto_ptr constructors |
| 409 | ``` |
| 410 | template<class Y> shared_ptr(std::auto_ptr<Y> & r); |
| 411 | ``` |
| 412 | ``` |
| 413 | template<class Y> shared_ptr(std::auto_ptr<Y> && r); |
| 414 | ``` |
| 415 | [none] |
| 416 | * {blank} |
| 417 | + |
| 418 | Requires:: `Y*` should be convertible to `T*`. |
| 419 | |
| 420 | Effects:: Constructs a `shared_ptr`, as if by storing a copy of `r.release()`. |
| 421 | |
| 422 | Postconditions:: `use_count() == 1`. |
| 423 | |
| 424 | Throws:: `std::bad_alloc`, or an implementation-defined exception when a resource other than memory could not be obtained. |
| 425 | |
| 426 | Exception safety:: If an exception is thrown, the constructor has no effect. |
| 427 | |
| 428 | ### unique_ptr constructor |
| 429 | ``` |
| 430 | template<class Y, class D> shared_ptr(std::unique_ptr<Y, D> && r); |
| 431 | ``` |
| 432 | [none] |
| 433 | * {blank} |
| 434 | + |
| 435 | Requires:: `Y*` should be convertible to `T*`. |
| 436 | |
| 437 | Effects:: |
| 438 | - When `r.get() == 0`, equivalent to `shared_ptr()`; |
| 439 | - When `D` is not a reference type, equivalent to `shared_ptr(r.release(), r.get_deleter())`; |
| 440 | - Otherwise, equivalent to `shared_ptr(r.release(), del)`, where `del` is a deleter that stores the reference `rd` returned |
| 441 | from `r.get_deleter()` and `del(p)` calls `rd(p)`. |
| 442 | |
| 443 | Throws:: `std::bad_alloc`, or an implementation-defined exception when a resource other than memory could not be obtained. |
| 444 | |
| 445 | Exception safety:: If an exception is thrown, the constructor has no effect. |
| 446 | |
| 447 | ### destructor |
| 448 | ``` |
| 449 | ~shared_ptr() noexcept; |
| 450 | ``` |
| 451 | [none] |
| 452 | * {blank} |
| 453 | + |
| 454 | Effects:: |
| 455 | - If `*this` is empty, or shares ownership with another `shared_ptr` instance (`use_count() > 1`), there are no side effects. |
| 456 | - Otherwise, if `*this` owns a pointer `p` and a deleter `d`, `d(p)` is called. |
| 457 | - Otherwise, `*this` owns a pointer `p`, and `delete p` is called. |
| 458 | |
| 459 | ### assignment |
| 460 | ``` |
| 461 | shared_ptr & operator=(shared_ptr const & r) noexcept; |
| 462 | ``` |
| 463 | ``` |
| 464 | template<class Y> shared_ptr & operator=(shared_ptr<Y> const & r) noexcept; |
| 465 | ``` |
| 466 | ``` |
| 467 | template<class Y> shared_ptr & operator=(std::auto_ptr<Y> & r); |
| 468 | ``` |
| 469 | [none] |
| 470 | * {blank} |
| 471 | + |
| 472 | Effects:: Equivalent to `shared_ptr(r).swap(*this)`. |
| 473 | Returns:: `*this`. |
| 474 | |
| 475 | NOTE: The use count updates caused by the temporary object construction and destruction are not considered observable side effects, |
| 476 | and the implementation is free to meet the effects (and the implied guarantees) via different means, without creating a temporary. |
| 477 | |
| 478 | [NOTE] |
| 479 | ==== |
| 480 | In particular, in the example: |
| 481 | ``` |
| 482 | shared_ptr<int> p(new int); |
| 483 | shared_ptr<void> q(p); |
| 484 | p = p; |
| 485 | q = p; |
| 486 | ``` |
| 487 | both assignments may be no-ops. |
| 488 | ==== |
| 489 | |
| 490 | ``` |
| 491 | shared_ptr & operator=(shared_ptr && r) noexcept; |
| 492 | ``` |
| 493 | ``` |
| 494 | template<class Y> shared_ptr & operator=(shared_ptr<Y> && r) noexcept; |
| 495 | ``` |
| 496 | ``` |
| 497 | template<class Y> shared_ptr & operator=(std::auto_ptr<Y> && r); |
| 498 | ``` |
| 499 | ``` |
| 500 | template<class Y, class D> shared_ptr & operator=(std::unique_ptr<Y, D> && r); |
| 501 | ``` |
| 502 | [none] |
| 503 | * {blank} |
| 504 | + |
| 505 | Effects:: Equivalent to `shared_ptr(std::move(r)).swap(*this)`. |
| 506 | Returns:: `*this`. |
| 507 | |
| 508 | ``` |
| 509 | shared_ptr & operator=(std::nullptr_t) noexcept; |
| 510 | ``` |
| 511 | [none] |
| 512 | * {blank} |
| 513 | + |
| 514 | Effects:: Equivalent to `shared_ptr().swap(*this)`. |
| 515 | Returns:: `*this`. |
| 516 | |
| 517 | ### reset |
| 518 | ``` |
| 519 | void reset() noexcept; |
| 520 | ``` |
| 521 | [none] |
| 522 | * {blank} |
| 523 | + |
| 524 | Effects:: Equivalent to `shared_ptr().swap(*this)`. |
| 525 | |
| 526 | ``` |
| 527 | template<class Y> void reset(Y * p); |
| 528 | ``` |
| 529 | [none] |
| 530 | * {blank} |
| 531 | + |
| 532 | Effects:: Equivalent to `shared_ptr(p).swap(*this)`. |
| 533 | |
| 534 | ``` |
| 535 | template<class Y, class D> void reset(Y * p, D d); |
| 536 | ``` |
| 537 | [none] |
| 538 | * {blank} |
| 539 | + |
| 540 | Effects:: Equivalent to `shared_ptr(p, d).swap(*this)`. |
| 541 | |
| 542 | ``` |
| 543 | template<class Y, class D, class A> void reset(Y * p, D d, A a); |
| 544 | ``` |
| 545 | [none] |
| 546 | * {blank} |
| 547 | + |
| 548 | Effects:: Equivalent to `shared_ptr(p, d, a).swap(*this)`. |
| 549 | |
| 550 | ``` |
| 551 | template<class Y> void reset(shared_ptr<Y> const & r, element_type * p) noexcept; |
| 552 | ``` |
| 553 | [none] |
| 554 | * {blank} |
| 555 | + |
| 556 | Effects:: Equivalent to `shared_ptr(r, p).swap(*this)`. |
| 557 | |
| 558 | ``` |
| 559 | template<class Y> void reset(shared_ptr<Y> && r, element_type * p) noexcept; |
| 560 | ``` |
| 561 | [none] |
| 562 | * {blank} |
| 563 | + |
| 564 | Effects:: Equivalent to `shared_ptr(std::move(r), p).swap(*this)`. |
| 565 | |
| 566 | ### indirection |
| 567 | ``` |
| 568 | T & operator*() const noexcept; |
| 569 | ``` |
| 570 | [none] |
| 571 | * {blank} |
| 572 | + |
| 573 | Requires:: `T` should not be an array type. The stored pointer must not be 0. |
| 574 | Returns:: `*get()`. |
| 575 | |
| 576 | ``` |
| 577 | T * operator->() const noexcept; |
| 578 | ``` |
| 579 | [none] |
| 580 | * {blank} |
| 581 | + |
| 582 | Requires:: `T` should not be an array type. The stored pointer must not be 0. |
| 583 | Returns:: `get()`. |
| 584 | |
| 585 | ``` |
| 586 | element_type & operator[](std::ptrdiff_t i) const noexcept; |
| 587 | ``` |
| 588 | [none] |
| 589 | * {blank} |
| 590 | + |
| 591 | Requires:: `T` should be an array type. The stored pointer must not be 0. `i >= 0`. If `T` is `U[N]`, `i < N`. |
| 592 | Returns:: `get()[i]`. |
| 593 | |
| 594 | ### get |
| 595 | |
| 596 | ``` |
| 597 | element_type * get() const noexcept; |
| 598 | ``` |
| 599 | [none] |
| 600 | * {blank} |
| 601 | + |
| 602 | Returns:: The stored pointer. |
| 603 | |
| 604 | ### unique |
| 605 | ``` |
| 606 | bool unique() const noexcept; |
| 607 | ``` |
| 608 | [none] |
| 609 | * {blank} |
| 610 | + |
| 611 | Returns:: `use_count() == 1`. |
| 612 | |
| 613 | ### use_count |
| 614 | ``` |
| 615 | long use_count() const noexcept; |
| 616 | ``` |
| 617 | [none] |
| 618 | * {blank} |
| 619 | + |
| 620 | Returns:: The number of `shared_ptr` objects, `*this` included, that share ownership with `*this`, or 0 when `*this` is empty. |
| 621 | |
| 622 | ### conversions |
| 623 | ``` |
| 624 | explicit operator bool() const noexcept; |
| 625 | ``` |
| 626 | [none] |
| 627 | * {blank} |
| 628 | + |
| 629 | Returns:: `get() != 0`. |
| 630 | |
| 631 | NOTE: This conversion operator allows `shared_ptr` objects to be used in boolean contexts, like `if(p && p\->valid()) {}`. |
| 632 | |
| 633 | NOTE: The conversion to `bool` is not merely syntactic sugar. It allows `shared_ptr` variables to be declared in conditions when using |
| 634 | `dynamic_pointer_cast` or `weak_ptr::lock`. |
| 635 | |
| 636 | NOTE: On C++03 compilers, the return value is of an unspecified type. |
| 637 | |
| 638 | ### swap |
| 639 | ``` |
| 640 | void swap(shared_ptr & b) noexcept; |
| 641 | ``` |
| 642 | [none] |
| 643 | * {blank} |
| 644 | + |
| 645 | Effects:: Exchanges the contents of the two smart pointers. |
| 646 | |
| 647 | ### owner_before |
| 648 | ``` |
| 649 | template<class Y> bool owner_before(shared_ptr<Y> const & rhs) const noexcept; |
| 650 | ``` |
| 651 | ``` |
| 652 | template<class Y> bool owner_before(weak_ptr<Y> const & rhs) const noexcept; |
| 653 | ``` |
| 654 | [none] |
| 655 | * {blank} |
| 656 | + |
| 657 | Effects:: See the description of `operator<`. |
| 658 | |
| 659 | ## Free Functions |
| 660 | |
| 661 | ### comparison |
| 662 | ``` |
| 663 | template<class T, class U> |
| 664 | bool operator==(shared_ptr<T> const & a, shared_ptr<U> const & b) noexcept; |
| 665 | ``` |
| 666 | [none] |
| 667 | * {blank} |
| 668 | + |
| 669 | Returns:: `a.get() == b.get()`. |
| 670 | |
| 671 | ``` |
| 672 | template<class T, class U> |
| 673 | bool operator!=(shared_ptr<T> const & a, shared_ptr<U> const & b) noexcept; |
| 674 | ``` |
| 675 | [none] |
| 676 | * {blank} |
| 677 | + |
| 678 | Returns:: `a.get() != b.get()`. |
| 679 | |
| 680 | ``` |
| 681 | template<class T> bool operator==(shared_ptr<T> const & p, std::nullptr_t) noexcept; |
| 682 | ``` |
| 683 | ``` |
| 684 | template<class T> bool operator==(std::nullptr_t, shared_ptr<T> const & p) noexcept; |
| 685 | ``` |
| 686 | [none] |
| 687 | * {blank} |
| 688 | + |
| 689 | Returns:: `p.get() == 0`. |
| 690 | |
| 691 | ``` |
| 692 | template<class T> bool operator!=(shared_ptr<T> const & p, std::nullptr_t) noexcept; |
| 693 | ``` |
| 694 | ``` |
| 695 | template<class T> bool operator!=(std::nullptr_t, shared_ptr<T> const & p) noexcept; |
| 696 | ``` |
| 697 | [none] |
| 698 | * {blank} |
| 699 | + |
| 700 | Returns:: `p.get() != 0`. |
| 701 | |
| 702 | ``` |
| 703 | template<class T, class U> |
| 704 | bool operator<(shared_ptr<T> const & a, shared_ptr<U> const & b) noexcept; |
| 705 | ``` |
| 706 | [none] |
| 707 | * {blank} |
| 708 | + |
| 709 | Returns:: An unspecified value such that |
| 710 | - `operator<` is a strict weak ordering as described in section [lib.alg.sorting] of the {cpp} standard; |
| 711 | - under the equivalence relation defined by `operator<`, `!(a < b) && !(b < a)`, two `shared_ptr` instances |
| 712 | are equivalent if and only if they share ownership or are both empty. |
| 713 | |
| 714 | NOTE: Allows `shared_ptr` objects to be used as keys in associative containers. |
| 715 | |
| 716 | NOTE: The rest of the comparison operators are omitted by design. |
| 717 | |
| 718 | ### swap |
| 719 | ``` |
| 720 | template<class T> void swap(shared_ptr<T> & a, shared_ptr<T> & b) noexcept; |
| 721 | ``` |
| 722 | [none] |
| 723 | * {blank} |
| 724 | + |
| 725 | Effects:: Equivalent to `a.swap(b)`. |
| 726 | |
| 727 | ### get_pointer |
| 728 | ``` |
| 729 | template<class T> |
| 730 | typename shared_ptr<T>::element_type * |
| 731 | get_pointer(shared_ptr<T> const & p) noexcept; |
| 732 | ``` |
| 733 | [none] |
| 734 | * {blank} |
| 735 | + |
| 736 | Returns:: `p.get()`. |
| 737 | |
| 738 | NOTE: Provided as an aid to generic programming. Used by `mem_fn`. |
| 739 | |
| 740 | ### static_pointer_cast |
| 741 | ``` |
| 742 | template<class T, class U> |
| 743 | shared_ptr<T> static_pointer_cast(shared_ptr<U> const & r) noexcept; |
| 744 | ``` |
| 745 | [none] |
| 746 | * {blank} |
| 747 | + |
| 748 | Requires:: The expression `static_cast<T*>( (U*)0 )` must be well-formed. |
| 749 | Returns:: `shared_ptr<T>( r, static_cast<typename shared_ptr<T>::element_type*>(r.get()) )`. |
| 750 | |
| 751 | CAUTION: The seemingly equivalent expression `shared_ptr<T>(static_cast<T*>(r.get()))` will eventually |
| 752 | result in undefined behavior, attempting to delete the same object twice. |
| 753 | |
| 754 | ### const_pointer_cast |
| 755 | ``` |
| 756 | template<class T, class U> |
| 757 | shared_ptr<T> const_pointer_cast(shared_ptr<U> const & r) noexcept; |
| 758 | ``` |
| 759 | [none] |
| 760 | * {blank} |
| 761 | + |
| 762 | Requires:: The expression `const_cast<T*>( (U*)0 )` must be well-formed. |
| 763 | Returns:: `shared_ptr<T>( r, const_cast<typename shared_ptr<T>::element_type*>(r.get()) )`. |
| 764 | |
| 765 | ### dynamic_pointer_cast |
| 766 | ``` |
| 767 | template<class T, class U> |
| 768 | shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const & r) noexcept; |
| 769 | ``` |
| 770 | [none] |
| 771 | * {blank} |
| 772 | + |
| 773 | Requires:: The expression `dynamic_cast<T*>( (U*)0 )` must be well-formed. |
| 774 | Returns:: |
| 775 | - When `dynamic_cast<typename shared_ptr<T>::element_type*>(r.get())` returns a nonzero value `p`, `shared_ptr<T>(r, p)`; |
| 776 | - Otherwise, `shared_ptr<T>()`. |
| 777 | |
| 778 | ### reinterpret_pointer_cast |
| 779 | ``` |
| 780 | template<class T, class U> |
| 781 | shared_ptr<T> reinterpret_pointer_cast(shared_ptr<U> const & r) noexcept; |
| 782 | ``` |
| 783 | [none] |
| 784 | * {blank} |
| 785 | + |
| 786 | Requires:: The expression `reinterpret_cast<T*>( (U*)0 )` must be well-formed. |
| 787 | Returns:: `shared_ptr<T>( r, reinterpret_cast<typename shared_ptr<T>::element_type*>(r.get()) )`. |
| 788 | |
| 789 | ### operator<< |
| 790 | ``` |
| 791 | template<class E, class T, class Y> |
| 792 | std::basic_ostream<E, T> & |
| 793 | operator<< (std::basic_ostream<E, T> & os, shared_ptr<Y> const & p); |
| 794 | ``` |
| 795 | [none] |
| 796 | * {blank} |
| 797 | + |
| 798 | Effects:: `os << p.get();`. |
| 799 | Returns:: `os`. |
| 800 | |
| 801 | ### get_deleter |
| 802 | ``` |
| 803 | template<class D, class T> |
| 804 | D * get_deleter(shared_ptr<T> const & p) noexcept; |
| 805 | ``` |
| 806 | [none] |
| 807 | * {blank} |
| 808 | + |
| 809 | Returns:: If `*this` owns a deleter `d` of type (cv-unqualified) `D`, returns `&d`; otherwise returns 0. |
| 810 | |
| 811 | ### Atomic Access |
| 812 | |
| 813 | NOTE: The function in this section are atomic with respect to the first `shared_ptr` argument, |
| 814 | identified by `*p`. Concurrent access to the same `shared_ptr` instance is not a data race, if |
| 815 | done exclusively by the functions in this section. |
| 816 | |
| 817 | ``` |
| 818 | template<class T> bool atomic_is_lock_free( shared_ptr<T> const * p ) noexcept; |
| 819 | ``` |
| 820 | [none] |
| 821 | * {blank} |
| 822 | + |
| 823 | Returns:: `false`. |
| 824 | |
| 825 | NOTE: This implementation is not lock-free. |
| 826 | |
| 827 | ``` |
| 828 | template<class T> shared_ptr<T> atomic_load( shared_ptr<T> const * p ) noexcept; |
| 829 | ``` |
| 830 | ``` |
| 831 | template<class T> shared_ptr<T> atomic_load_explicit( shared_ptr<T> const * p, int ) noexcept; |
| 832 | ``` |
| 833 | [none] |
| 834 | * {blank} |
| 835 | + |
| 836 | Returns:: `*p`. |
| 837 | |
| 838 | NOTE: The `int` argument is the `memory_order`, but this implementation does not use it, as it's lock-based |
| 839 | and therefore always sequentially consistent. |
| 840 | |
| 841 | ``` |
| 842 | template<class T> |
| 843 | void atomic_store( shared_ptr<T> * p, shared_ptr<T> r ) noexcept; |
| 844 | ``` |
| 845 | ``` |
| 846 | template<class T> |
| 847 | void atomic_store_explicit( shared_ptr<T> * p, shared_ptr<T> r, int ) noexcept; |
| 848 | ``` |
| 849 | [none] |
| 850 | * {blank} |
| 851 | + |
| 852 | Effects:: `p\->swap(r)`. |
| 853 | |
| 854 | ``` |
| 855 | template<class T> |
| 856 | shared_ptr<T> atomic_exchange( shared_ptr<T> * p, shared_ptr<T> r ) noexcept; |
| 857 | ``` |
| 858 | ``` |
| 859 | template<class T> |
| 860 | shared_ptr<T> atomic_exchange_explicit( |
| 861 | shared_ptr<T> * p, shared_ptr<T> r, int ) noexcept; |
| 862 | ``` |
| 863 | [none] |
| 864 | * {blank} |
| 865 | + |
| 866 | Effects:: `p\->swap(r)`. |
| 867 | Returns:: The old value of `*p`. |
| 868 | |
| 869 | ``` |
| 870 | template<class T> |
| 871 | bool atomic_compare_exchange( |
| 872 | shared_ptr<T> * p, shared_ptr<T> * v, shared_ptr<T> w ) noexcept; |
| 873 | ``` |
| 874 | ``` |
| 875 | template<class T> |
| 876 | bool atomic_compare_exchange_explicit( |
| 877 | shared_ptr<T> * p, shared_ptr<T> * v, shared_ptr<T> w, int, int ) noexcept; |
| 878 | ``` |
| 879 | [none] |
| 880 | * {blank} |
| 881 | + |
| 882 | Effects:: If `*p` is equivalent to `*v`, assigns `w` to `*p`, otherwise assigns `*p` to `*v`. |
| 883 | Returns:: `true` if `*p` was equivalent to `*v`, `false` otherwise. |
| 884 | Remarks:: Two `shared_ptr` instances are equivalent if they store the same pointer value and _share ownership_. |
| 885 | |
| 886 | |
| 887 | ## Example |
| 888 | |
| 889 | See link:../../example/shared_ptr_example.cpp[shared_ptr_example.cpp] for a complete example program. The program builds a |
| 890 | `std::vector` and `std::set` of `shared_ptr` objects. |
| 891 | |
| 892 | Note that after the containers have been populated, some of the `shared_ptr` objects will have a use count of 1 rather than |
| 893 | a use count of 2, since the set is a `std::set` rather than a `std::multiset`, and thus does not contain duplicate entries. |
| 894 | Furthermore, the use count may be even higher at various times while `push_back` and `insert` container operations are performed. |
| 895 | More complicated yet, the container operations may throw exceptions under a variety of circumstances. Getting the memory management |
| 896 | and exception handling in this example right without a smart pointer would be a nightmare. |
| 897 | |
| 898 | ## Handle/Body Idiom |
| 899 | |
| 900 | One common usage of `shared_ptr` is to implement a handle/body (also called pimpl) idiom which avoids exposing the body (implementation) |
| 901 | in the header file. |
| 902 | |
| 903 | The link:../../example/shared_ptr_example2_test.cpp[shared_ptr_example2_test.cpp] sample program includes a header file, |
| 904 | link:../../example/shared_ptr_example2.hpp[shared_ptr_example2.hpp], which uses a `shared_ptr` to an incomplete type to hide the implementation. |
| 905 | The instantiation of member functions which require a complete type occurs in the link:../../example/shared_ptr_example2.cpp[shared_ptr_example2.cpp] |
| 906 | implementation file. Note that there is no need for an explicit destructor. Unlike `~scoped_ptr`, `~shared_ptr` does not require that `T` be a complete type. |
| 907 | |
| 908 | ## Thread Safety |
| 909 | |
| 910 | `shared_ptr` objects offer the same level of thread safety as built-in types. A `shared_ptr` instance can be "read" (accessed using only const operations) |
| 911 | simultaneously by multiple threads. Different `shared_ptr` instances can be "written to" (accessed using mutable operations such as `operator=` or `reset`) |
| 912 | simultaneously by multiple threads (even when these instances are copies, and share the same reference count underneath.) |
| 913 | |
| 914 | Any other simultaneous accesses result in undefined behavior. |
| 915 | |
| 916 | Examples: |
| 917 | ``` |
| 918 | shared_ptr<int> p(new int(42)); |
| 919 | ``` |
| 920 | |
| 921 | .Reading a `shared_ptr` from two threads |
| 922 | ``` |
| 923 | // thread A |
| 924 | shared_ptr<int> p2(p); // reads p |
| 925 | |
| 926 | // thread B |
| 927 | shared_ptr<int> p3(p); // OK, multiple reads are safe |
| 928 | ``` |
| 929 | |
| 930 | .Writing different `shared_ptr` instances from two threads |
| 931 | ``` |
| 932 | // thread A |
| 933 | p.reset(new int(1912)); // writes p |
| 934 | |
| 935 | // thread B |
| 936 | p2.reset(); // OK, writes p2 |
| 937 | ``` |
| 938 | |
| 939 | .Reading and writing a `shared_ptr` from two threads |
| 940 | ``` |
| 941 | // thread A |
| 942 | p = p3; // reads p3, writes p |
| 943 | |
| 944 | // thread B |
| 945 | p3.reset(); // writes p3; undefined, simultaneous read/write |
| 946 | ``` |
| 947 | |
| 948 | .Reading and destroying a `shared_ptr` from two threads |
| 949 | ``` |
| 950 | // thread A |
| 951 | p3 = p2; // reads p2, writes p3 |
| 952 | |
| 953 | // thread B |
| 954 | // p2 goes out of scope: undefined, the destructor is considered a "write access" |
| 955 | ``` |
| 956 | |
| 957 | .Writing a `shared_ptr` from two threads |
| 958 | ``` |
| 959 | // thread A |
| 960 | p3.reset(new int(1)); |
| 961 | |
| 962 | // thread B |
| 963 | p3.reset(new int(2)); // undefined, multiple writes |
| 964 | ``` |
| 965 | |
| 966 | Starting with Boost release 1.33.0, `shared_ptr` uses a lock-free implementation on most common platforms. |
| 967 | |
| 968 | If your program is single-threaded and does not link to any libraries that might have used `shared_ptr` in its default configuration, |
| 969 | you can `#define` the macro `BOOST_SP_DISABLE_THREADS` on a project-wide basis to switch to ordinary non-atomic reference count updates. |
| 970 | |
| 971 | (Defining `BOOST_SP_DISABLE_THREADS` in some, but not all, translation units is technically a violation of the One Definition Rule and |
| 972 | undefined behavior. Nevertheless, the implementation attempts to do its best to accommodate the request to use non-atomic updates in those |
| 973 | translation units. No guarantees, though.) |
| 974 | |
| 975 | You can define the macro `BOOST_SP_USE_PTHREADS` to turn off the lock-free platform-specific implementation and fall back to the generic |
| 976 | `pthread_mutex_t`-based code. |
| 977 | |
| 978 | ## Frequently Asked Questions |
| 979 | |
| 980 | [qanda] |
| 981 | There are several variations of shared pointers, with different tradeoffs; why does the smart pointer library supply only a single implementation? It would be useful to be able to experiment with each type so as to find the most suitable for the job at hand?:: |
| 982 | |
| 983 | An important goal of `shared_ptr` is to provide a standard shared-ownership pointer. Having a single pointer type is important for stable |
| 984 | library interfaces, since different shared pointers typically cannot interoperate, i.e. a reference counted pointer (used by library A) |
| 985 | cannot share ownership with a linked pointer (used by library B.) |
| 986 | |
| 987 | Why doesn't shared_ptr have template parameters supplying traits or policies to allow extensive user customization?:: |
| 988 | |
| 989 | Parameterization discourages users. The `shared_ptr` template is carefully crafted to meet common needs without extensive parameterization. |
| 990 | |
| 991 | I am not convinced. Default parameters can be used where appropriate to hide the complexity. Again, why not policies?:: |
| 992 | |
| 993 | Template parameters affect the type. See the answer to the first question above. |
| 994 | |
| 995 | Why doesn't `shared_ptr` use a linked list implementation?:: |
| 996 | |
| 997 | A linked list implementation does not offer enough advantages to offset the added cost of an extra pointer. In addition, it is expensive to |
| 998 | make a linked list implementation thread safe. |
| 999 | |
| 1000 | Why doesn't `shared_ptr` (or any of the other Boost smart pointers) supply an automatic conversion to T*?:: |
| 1001 | |
| 1002 | Automatic conversion is believed to be too error prone. |
| 1003 | |
| 1004 | Why does `shared_ptr` supply `use_count()`?:: |
| 1005 | |
| 1006 | As an aid to writing test cases and debugging displays. One of the progenitors had `use_count()`, and it was useful in tracking down bugs in |
| 1007 | a complex project that turned out to have cyclic-dependencies. |
| 1008 | |
| 1009 | Why doesn't `shared_ptr` specify complexity requirements?:: |
| 1010 | |
| 1011 | Because complexity requirements limit implementors and complicate the specification without apparent benefit to `shared_ptr` users. For example, |
| 1012 | error-checking implementations might become non-conforming if they had to meet stringent complexity requirements. |
| 1013 | |
| 1014 | Why doesn't `shared_ptr` provide a `release()` function?:: |
| 1015 | |
| 1016 | `shared_ptr` cannot give away ownership unless it's `unique()` because the other copy will still destroy the object. |
| 1017 | + |
| 1018 | Consider: |
| 1019 | + |
| 1020 | ``` |
| 1021 | shared_ptr<int> a(new int); |
| 1022 | shared_ptr<int> b(a); // a.use_count() == b.use_count() == 2 |
| 1023 | |
| 1024 | int * p = a.release(); |
| 1025 | |
| 1026 | // Who owns p now? b will still call delete on it in its destructor. |
| 1027 | ``` |
| 1028 | + |
| 1029 | Furthermore, the pointer returned by `release()` would be difficult to deallocate reliably, as the source `shared_ptr` could have been created with a |
| 1030 | custom deleter, or may have pointed to an object of a different type. |
| 1031 | |
| 1032 | Why is `operator\->()` const, but its return value is a non-const pointer to the element type?:: |
| 1033 | |
| 1034 | Shallow copy pointers, including raw pointers, typically don't propagate constness. It makes little sense for them to do so, as you can always obtain a |
| 1035 | non-const pointer from a const one and then proceed to modify the object through it. `shared_ptr` is "as close to raw pointers as possible but no closer". |