std::shared_ptr (3) - Linux Manuals
std::shared_ptr: std::shared_ptr
NAME
std::shared_ptr - std::shared_ptr
Synopsis
Defined in header <memory>
template< class T > class shared_ptr; (since C++11)
std::shared_ptr is a smart pointer that retains shared ownership of an object through a pointer. Several shared_ptr objects may own the same object. The object is destroyed and its memory deallocated when either of the following happens:
* the last remaining shared_ptr owning the object is destroyed;
* the last remaining shared_ptr owning the object is assigned another pointer via operator= or reset().
The object is destroyed using delete-expression or a custom deleter that is supplied to shared_ptr during construction.
A shared_ptr can share ownership of an object while storing a pointer to another object. This feature can be used to point to member objects while owning the object they belong to. The stored pointer is the one accessed by get(), the dereference and the comparison operators. The managed pointer is the one passed to the deleter when use count reaches zero.
A shared_ptr may also own no objects, in which case it is called empty (an empty shared_ptr may have a non-null stored pointer if the aliasing constructor was used to create it).
All specializations of shared_ptr meet the requirements of CopyConstructible, CopyAssignable, and LessThanComparable and are contextually_convertible to bool.
All member functions (including copy constructor and copy assignment) can be called by multiple threads on different instances of shared_ptr without additional synchronization even if these instances are copies and share ownership of the same object. If multiple threads of execution access the same shared_ptr without synchronization and any of those accesses uses a non-const member function of shared_ptr then a data race will occur; the shared_ptr_overloads_of_atomic_functions can be used to prevent the data race.
Member types
Member type Definition
element_type T (until C++17)
weak_type (since C++17) std::weak_ptr<T>
Member functions
constructor (public member function)
destructor (public member function)
operator= (public member function)
Modifiers
reset (public member function)
swap (public member function)
Observers
get (public member function)
operator* (public member function)
operator->
operator[] provides indexed access to the stored array
(C++17)
use_count (public member function)
unique checks whether the managed object is managed only by the current shared_ptr instance
(until C++20)
operator_bool (public member function)
owner_before (public member function)
Non-member functions
make_shared
make_shared_default_init creates a shared pointer that manages a new object
(C++20)
allocate_shared
allocate_shared_default_init creates a shared pointer that manages a new object allocated using an allocator
(C++20)
static_pointer_cast
dynamic_pointer_cast
const_pointer_cast
reinterpret_pointer_cast applies static_cast, dynamic_cast, const_cast, or reinterpret_cast to the stored pointer