[#bsl-shared_ptr-0a] = xref:bsl.adoc[bsl]::shared_ptr :relfileprefix: ../ :mrdocs: Thread‐safe reference‐counted smart pointer with shared ownership. == Synopsis Declared in `<bslstl_sharedptr.h>` [source,cpp,subs="verbatim,replacements,macros,-callouts"] ---- template<class ELEMENT_TYPE> class shared_ptr; ---- == Description This class provides a thread‐safe reference‐counted "smart pointer" to support "shared ownership" of objects: a shared pointer ensures that the shared object is destroyed, using the appropriate deletion method, only when there are no shared references to it. The object (of template parameter type `ELEMENT_TYPE`) referred to by a shared pointer may be accessed directly using the `‐>` operator, or the dereference operator (operator `*`) can be used to obtain a reference to that object. Note that the object referred to by a shared pointer representation is usually the same as the object referred to by that shared pointer (of the same `ELEMENT_TYPE`), but this need not always be true in the presence of conversions or "aliasing": the object referred to (of template parameter type `ELEMENT_TYPE`) by the shared pointer may differ from the object of type `COMPATIBLE_TYPE` (see the "Aliasing" section in the component‐level documentation) referred to by the shared pointer representation. More generally, this class supports a complete set of _in_‐_core_ pointer semantic operations. == Type Aliases [cols="1,4"] |=== | Name| Description | xref:bsl/shared_ptr-0a/element_type.adoc[`element_type`] | Alias to the element type managed by this `shared_ptr`. | xref:bsl/shared_ptr-0a/weak_type.adoc[`weak_type`] | `weak_type` is an alias to a weak pointer with the same element type as this `shared_ptr`. |=== == Member Functions [cols="1,4"] |=== | Name| Description | xref:bsl/shared_ptr-0a/2constructor-03f.adoc[`shared_ptr`] [.small]#[constructor]# | Constructors | xref:bsl/shared_ptr-0a/2destructor.adoc[`~shared_ptr`] [.small]#[destructor]# | Destroy this `shared_ptr` and release its managed object if last owner. | xref:bsl/shared_ptr-0a/operator_assign-05.adoc[`operator=`] | Assignment operators | xref:bsl/shared_ptr-0a/clear.adoc[`clear`] | Reset this `shared_ptr` to the empty state. | xref:bsl/shared_ptr-0a/createInplace-00.adoc[`createInplace`] | `createInplace` overloads | xref:bsl/shared_ptr-0a/get.adoc[`get`] | Return the address providing modifiable access to the object referred to by this shared pointer, or 0 if this shared pointer does not refer to an object. | xref:bsl/shared_ptr-0a/load-06.adoc[`load`] | `load` overloads | xref:bsl/shared_ptr-0a/loadAlias.adoc[`loadAlias`] | Reset this `shared_ptr` as an alias sharing ownership with `source`. | xref:bsl/shared_ptr-0a/managedPtr.adoc[`managedPtr`] | Return a `ManagedPtr` referring to the same object as this `shared_ptr`. | xref:bsl/shared_ptr-0a/numReferences.adoc[`numReferences`] | Return a "snapshot" of the number of shared pointers (including this one) that share ownership of the object managed by this shared pointer. | xref:bsl/shared_ptr-0a/operator_star.adoc[`operator*`] | Return a reference providing modifiable access to the object referred to by this shared pointer. | xref:bsl/shared_ptr-0a/operator_ptr.adoc[`operator‐>`] | Return the address providing modifiable access to the object referred to by this shared pointer, or 0 if this shared pointer does not refer to an object. | xref:bsl/shared_ptr-0a/operator_subs.adoc[`operator[]`] | Return a reference to the element at the specified `index`. | xref:bsl/shared_ptr-0a/owner_before-0f.adoc[`owner_before`] | Return whether this `shared_ptr` precedes `other` in owner order. | xref:bsl/shared_ptr-0a/owner_equal-08.adoc[`owner_equal`] | Return whether this pointer shares ownership with `other`. | xref:bsl/shared_ptr-0a/owner_hash.adoc[`owner_hash`] | Return a hash value based on this pointer's ownership identity. | xref:bsl/shared_ptr-0a/ptr.adoc[`ptr`] | Return the address providing modifiable access to the object referred to by this shared pointer, or 0 if this shared pointer does not refer to an object. | xref:bsl/shared_ptr-0a/release.adoc[`release`] | Release ownership and return the pointer and representation addresses. | xref:bsl/shared_ptr-0a/rep.adoc[`rep`] | Return the address providing modifiable access to the `BloombergLP::bslma::SharedPtrRep` object used by this shared pointer, or 0 if this shared pointer is empty. | xref:bsl/shared_ptr-0a/reset-0c8.adoc[`reset`] | `reset` overloads | xref:bsl/shared_ptr-0a/swap.adoc[`swap`] | Exchange the state of this `shared_ptr` with `other`. | xref:bsl/shared_ptr-0a/unique.adoc[`unique`] | Return whether this `shared_ptr` is the unique owner of its object. | xref:bsl/shared_ptr-0a/use_count.adoc[`use_count`] | Return a "snapshot" of the number of shared pointers (including this one) that share ownership of the object managed by this shared pointer. | xref:bsl/shared_ptr-0a/2conversion-01.adoc[`operator BloombergLP::bslmf::NestedTraitDeclaration<shared_ptr<ELEMENT_TYPE>, is_nothrow_move_constructible>`] | Nested trait declaration for `is_nothrow_move_constructible`. | xref:bsl/shared_ptr-0a/2conversion-05.adoc[`operator BoolType`] | Return whether this `shared_ptr` refers to an object. |=== == Deduction Guides [cols="1,4"] |=== | Name| Description | xref:bsl/shared_ptr-0b0.adoc[`shared_ptr<ELEMENT_TYPE>`] | Deduce the specified type `ELEMENT_TYPE` corresponding template parameter of the `bslma::ManagedPtr` supplied to the constructor of `shared_ptr`. | xref:bsl/shared_ptr-0b7.adoc[`shared_ptr<ELEMENT_TYPE>`] | Deduce the specified type `ELEMENT_TYPE` corresponding template parameter of the `bsl::weak_ptr` supplied to the constructor of `shared_ptr`. | xref:bsl/shared_ptr-0e.adoc[`shared_ptr<ELEMENT_TYPE>`] | Deduce the specified type `ELEMENT_TYPE` corresponding template parameter of the `std::unique_ptr` supplied to the constructor of `shared_ptr`. | xref:bsl/shared_ptr-0c.adoc[`shared_ptr<ELEMENT_TYPE>`] | Deduction guide for constructing `shared_ptr` from `ManagedPtr`. | xref:bsl/shared_ptr-02.adoc[`shared_ptr<ELEMENT_TYPE>`] | Deduction guide for constructing `shared_ptr` from `std::unique_ptr`. |=== == Friends [cols="1,4"] |=== | Name| Description | `bsl::shared_ptr` | Thread‐safe reference‐counted smart pointer with shared ownership. |=== == Non-Member Functions [cols="1,4"] |=== | Name| Description | xref:bsl/allocate_shared-01e.adoc[`allocate_shared`] | Return a `shared_ptr` managing a newly created object. | xref:bsl/allocate_shared-01f.adoc[`allocate_shared`] | Return a `shared_ptr` managing a newly created object. | xref:bsl/allocate_shared-04.adoc[`allocate_shared`] | Return a `shared_ptr` managing a newly created object. | xref:bsl/allocate_shared-05b.adoc[`allocate_shared`] | Return a `shared_ptr` managing a newly created object. | xref:bsl/allocate_shared-05d.adoc[`allocate_shared`] | Return a `shared_ptr` managing a newly created object. | xref:bsl/allocate_shared-07.adoc[`allocate_shared`] | Return a `shared_ptr` managing a newly created object. | xref:bsl/allocate_shared-08.adoc[`allocate_shared`] | Return a `shared_ptr` managing a newly created object. | xref:bsl/allocate_shared-0bb.adoc[`allocate_shared`] | Return a `shared_ptr` managing a newly created object. | xref:bsl/allocate_shared-0c.adoc[`allocate_shared`] | Return a `shared_ptr` managing a newly created object. | xref:bsl/allocate_shared-0f.adoc[`allocate_shared`] | Return a `shared_ptr` managing a newly created object. | xref:bsl/allocate_shared_for_overwrite-017.adoc[`allocate_shared_for_overwrite`] | Return a `shared_ptr` managing a newly created object. | xref:bsl/allocate_shared_for_overwrite-01c.adoc[`allocate_shared_for_overwrite`] | Return a `shared_ptr` managing a newly created object. | xref:bsl/allocate_shared_for_overwrite-07.adoc[`allocate_shared_for_overwrite`] | Return a `shared_ptr` managing a newly created object. | xref:bsl/allocate_shared_for_overwrite-0d.adoc[`allocate_shared_for_overwrite`] | Return a `shared_ptr` managing a newly created object. | xref:bsl/allocate_shared_for_overwrite-0ee2.adoc[`allocate_shared_for_overwrite`] | Return a `shared_ptr` managing a newly created object. | xref:bsl/allocate_shared_for_overwrite-0eed.adoc[`allocate_shared_for_overwrite`] | Return a `shared_ptr` managing a newly created object. | xref:bsl/const_pointer_cast.adoc[`const_pointer_cast`] | Return a `shared_ptr` obtained by `const_cast` from `source`. | xref:bsl/dynamic_pointer_cast.adoc[`dynamic_pointer_cast`] | Return a `shared_ptr` obtained by `dynamic_cast` from `source`. | xref:bsl/get_deleter.adoc[`get_deleter`] | Return a pointer to the deleter of type `DELETER` used by `p`, or null. | xref:bsl/make_shared-02.adoc[`make_shared`] | Return a `shared_ptr` managing a newly created object. | xref:bsl/make_shared-06.adoc[`make_shared`] | Return a `shared_ptr` managing a newly created object. | xref:bsl/make_shared-0a.adoc[`make_shared`] | Return a `shared_ptr` managing a newly created object. | xref:bsl/make_shared-0d.adoc[`make_shared`] | Return a `shared_ptr` managing a newly created object. | xref:bsl/make_shared-0e.adoc[`make_shared`] | Return a `shared_ptr` managing a newly created object. | xref:bsl/make_shared_for_overwrite-02.adoc[`make_shared_for_overwrite`] | Return a `shared_ptr` managing a newly created object. | xref:bsl/make_shared_for_overwrite-054.adoc[`make_shared_for_overwrite`] | Return a `shared_ptr` managing a newly created object. | xref:bsl/make_shared_for_overwrite-05b.adoc[`make_shared_for_overwrite`] | Return a `shared_ptr` managing a newly created object. | xref:bsl/operator_3way-0412.adoc[`operator<=>`] | Perform a three‐way comparison of the specified `ptr` and null pointer by using the comparison operators of `TYPE *`; return the result of that comparison. | xref:bsl/operator_3way-086.adoc[`operator<=>`] | Compare `lhs` and `rhs` three‐way by their stored pointers. | xref:bsl/operator_eq-0cd9.adoc[`operator==`] | Return whether `lhs` and `rhs` refer to the same object. | xref:bsl/operator_eq-0ec.adoc[`operator==`] | Return `true` if the specified `lhs` shared pointer does not refer to an object, and `false` otherwise. | xref:bsl/reinterpret_pointer_cast.adoc[`reinterpret_pointer_cast`] | Return a `shared_ptr` obtained by `reinterpret_cast` from `source`. | xref:bsl/static_pointer_cast.adoc[`static_pointer_cast`] | Return a `shared_ptr` obtained by `static_cast` from `source`. | xref:bsl/swap-092.adoc[`swap`] | Exchange the states of the specified `shared_ptr` objects `a` and `b`. |=== [.small]#Created with https://www.mrdocs.com[MrDocs]#