Namespaces
Variants
Actions

std::shared_ptr

From cppreference.com
< cpp‎ | memory
 
 
Memory management library
(exposition only*)
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
(until C++20*)
(until C++20*)

Garbage collector support (until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
 
 
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 shared_ptr objects without additional synchronization even if these objects are copies and share ownership of the same object. If multiple threads of execution access the same shared_ptr object without synchronization and any of those accesses uses a non-const member function of shared_ptr then a data race will occur; the std::atomic<shared_ptr> can be used to prevent the data race.

Contents

[edit] Member types

Member type Definition
element_type
T (until C++17)
std::remove_extent_t<T> (since C++17)
weak_type (since C++17) std::weak_ptr<T>

[edit] Member functions

constructs new shared_ptr
(public member function) [edit]
destructs the owned object if no more shared_ptrs link to it
(public member function) [edit]
assigns the shared_ptr
(public member function) [edit]
Modifiers
replaces the managed object
(public member function) [edit]
swaps the managed objects
(public member function) [edit]
Observers
returns the stored pointer
(public member function) [edit]
dereferences the stored pointer
(public member function) [edit]
provides indexed access to the stored array
(public member function) [edit]
returns the number of shared_ptr objects referring to the same managed object
(public member function) [edit]
(until C++20)
checks whether the managed object is managed only by the current shared_ptr object
(public member function) [edit]
checks if the stored pointer is not null
(public member function) [edit]