std::weak_ptr

From Cppreference

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

template< class T > class weak_ptr;
(since C++11)

std::weak_ptr is a smart pointer that holds a non-owning ("weak") reference to an object that is managed by std::shared_ptr. It must be converted to std::shared_ptr in order to access the referenced object.

std::weak_ptr models temporary ownership: when an object needs to be accessed only if it exists, and it may be deleted at any time by someone else, std::weak_ptr is used to track the object, and it is converted to std::shared_ptr to assume temporary ownership. If the original std::shared_ptr is destroyed at this time, the object's lifetime is extended until the temporary std::shared_ptr is destroyed as well.

In addition, std::weak_ptr is used to break circular references of std::shared_ptr.

Contents

[edit] Member types

Member type Definition
element_type T

[edit] Member functions

creates a new weak_ptr
(public member function)
destroys a weak_ptr
(public member function)
replaces the reference held in the weak_ptr
(public member function)
Modifiers
clears the reference held in the weak pointer
(public member function)
exchanges contents of two weak pointers
(public member function)
Observers
returns the number of shared_ptrs that manage the object
(public member function)
checks if the referenced object doesn't exist anymore
(public member function)
creates a shared_ptr that manages the referenced object
(public member function)
provides owner-based ordering of weak pointers
(public member function)

[edit] Non-member functions

specializes the std::swap algorithm
(function template)