bsl::shared_ptr

Thread-safe reference-counted smart pointer with shared ownership.

Synopsis

Declared in <bslstl_sharedptr.h>

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

NameDescription
element_type Alias to the element type managed by this shared_ptr.
weak_type weak_type is an alias to a weak pointer with the same element type as this shared_ptr.

Member Functions

NameDescription
shared_ptr [constructor]Constructors
~shared_ptr [destructor]Destroy this shared_ptr and release its managed object if last owner.
operator= Assignment operators
clear Reset this shared_ptr to the empty state.
createInplace createInplace overloads
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.
load load overloads
loadAlias Reset this shared_ptr as an alias sharing ownership with source.
managedPtr Return a ManagedPtr referring to the same object as this shared_ptr.
numReferences Return a "snapshot" of the number of shared pointers (including this one) that share ownership of the object managed by this shared pointer.
operator* Return a reference providing modifiable access to the object referred to by this shared pointer.
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.
operator[] Return a reference to the element at the specified index.
owner_before Return whether this shared_ptr precedes other in owner order.
owner_equal Return whether this pointer shares ownership with other.
owner_hash Return a hash value based on this pointer's ownership identity.
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.
release Release ownership and return the pointer and representation addresses.
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.
reset reset overloads
swap Exchange the state of this shared_ptr with other.
unique Return whether this shared_ptr is the unique owner of its object.
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.
operator BloombergLP::bslmf::NestedTraitDeclaration<shared_ptr<ELEMENT_TYPE>, is_nothrow_move_constructible> Nested trait declaration for is_nothrow_move_constructible.
operator BoolType Return whether this shared_ptr refers to an object.

Deduction Guides

NameDescription
shared_ptr<ELEMENT_TYPE> Deduce the specified type ELEMENT_TYPE corresponding template parameter of the bslma::ManagedPtr supplied to the constructor of shared_ptr.
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.
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.
shared_ptr<ELEMENT_TYPE> Deduction guide for constructing shared_ptr from ManagedPtr.
shared_ptr<ELEMENT_TYPE> Deduction guide for constructing shared_ptr from std::unique_ptr.

Friends

NameDescription
bsl::shared_ptrThread-safe reference-counted smart pointer with shared ownership.

Non-Member Functions

NameDescription
allocate_sharedReturn a shared_ptr managing a newly created object.
allocate_sharedReturn a shared_ptr managing a newly created object.
allocate_sharedReturn a shared_ptr managing a newly created object.
allocate_sharedReturn a shared_ptr managing a newly created object.
allocate_sharedReturn a shared_ptr managing a newly created object.
allocate_sharedReturn a shared_ptr managing a newly created object.
allocate_sharedReturn a shared_ptr managing a newly created object.
allocate_sharedReturn a shared_ptr managing a newly created object.
allocate_sharedReturn a shared_ptr managing a newly created object.
allocate_sharedReturn a shared_ptr managing a newly created object.
allocate_shared_for_overwriteReturn a shared_ptr managing a newly created object.
allocate_shared_for_overwriteReturn a shared_ptr managing a newly created object.
allocate_shared_for_overwriteReturn a shared_ptr managing a newly created object.
allocate_shared_for_overwriteReturn a shared_ptr managing a newly created object.
allocate_shared_for_overwriteReturn a shared_ptr managing a newly created object.
allocate_shared_for_overwriteReturn a shared_ptr managing a newly created object.
const_pointer_castReturn a shared_ptr obtained by const_cast from source.
dynamic_pointer_castReturn a shared_ptr obtained by dynamic_cast from source.
get_deleterReturn a pointer to the deleter of type DELETER used by p, or null.
make_sharedReturn a shared_ptr managing a newly created object.
make_sharedReturn a shared_ptr managing a newly created object.
make_sharedReturn a shared_ptr managing a newly created object.
make_sharedReturn a shared_ptr managing a newly created object.
make_sharedReturn a shared_ptr managing a newly created object.
make_shared_for_overwriteReturn a shared_ptr managing a newly created object.
make_shared_for_overwriteReturn a shared_ptr managing a newly created object.
make_shared_for_overwriteReturn a shared_ptr managing a newly created object.
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.
operator<=>Compare lhs and rhs three-way by their stored pointers.
operator==Return whether lhs and rhs refer to the same object.
operator==Return true if the specified lhs shared pointer does not refer to an object, and false otherwise.
reinterpret_pointer_castReturn a shared_ptr obtained by reinterpret_cast from source.
static_pointer_castReturn a shared_ptr obtained by static_cast from source.
swapExchange the states of the specified shared_ptr objects a and b.