std::shared_ptr

From Cppreference

Jump to: navigation, search
Defined in header <memory>

template< class T > class shared_ptr;
(C++11 feature)

std::shared_ptr is a smart pointer that manages lifetime of an object, typically allocated with new. Several shared_ptr objects may manage the same object; the object is destroyed when the last remaining shared_ptr pointing to it is destroyed or reset. The object is destroyed using delete-expression or a custom deleter that is supplied to shared_ptr during construction.

Contents

[edit] Member types

Member type Definition
element_type T

[edit] Member functions

(constructor)
constructs new shared_ptr
(public member function)
(destructor)
destructs the owned object if no more shared_ptrs link to it
(public member function)
operator=
assigns the shared_ptr
(public member function)
Modifiers
reset
replaces the managed object
(public member function)
swap
swaps the managed objects
(public member function)
Observers
get
returns a pointer to the managed object
(public member function)
operator*
operator->
dereferences pointer to the managed object
(public member function)
use_count
returns the number of shared_ptr objects referring to the same managed object
(public member function)
unique
checks whether the managed object is managed only by the current shared_ptr instance
(public member function)
operator bool
checks if there is associated managed object
(public member function)
owner_before
provides owner-based ordering of shared pointers
(public member function)

[edit] Non-member functions

make_shared
creates a shared pointer that manages a new object
(function template)
allocate_shared
creates a shared pointer that manages a new object allocated using an allocator
(function template)
static_pointer_cast
dynamic_pointer_cost
const_pointer_cast
applies static_cast, dynamic_cast or const_cast to the type of the managed object
(function template)
get_deleter
returns the deleter of specified type, if owned
(function template)
operator==
operator!=
operator<
operator<=
operator>
operator>=
compares with another shared_ptr or with nullptr
(function template)
operator<<
outputs the value of the managed pointer to an output stream
(function template)
std::swap(std::shared_ptr) (C++11)
specializes the std::swap() algorithm
(function template)
std::atomic_is_lock_free(std::shared_ptr)
std::atomic_load(std::shared_ptr)
std::atomic_load_explicit(std::shared_ptr)
std::atomic_store(std::shared_ptr)
std::atomic_store_explicit(std::shared_ptr)
std::atomic_exchange(std::shared_ptr)
std::atomic_exchange_explicit(std::shared_ptr)
std::atomic_compare_exchange_weak(std::shared_ptr)
std::atomic_compare_exchange_strong(std::shared_ptr)
std::atomic_compare_exchange_weak_explicit(std::shared_ptr)
std::atomic_compare_exchange_strong_explicit(std::shared_ptr)
specializes atomic operations
(function template)

[edit] Helper classes

std::hash<std::shared_ptr> (C++11)
hash support for std::shared_ptr
(class template specialization)