Namespaces
Variants
Views
Actions

std::weak_ptr

From cppreference.com
< cpp‎ | memory
 
 
 
Dynamic memory management
Uninitialized storage
(C++17)
Garbage collection support
Miscellaneous
(C++20)
(C++11)
(C++11)
C Library
Low level memory management
 
 
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 (until C++17)
std::remove_extent_t<T> (since C++17)

[edit] Member functions

creates a new weak_ptr
(public member function) [edit]
destroys a weak_ptr
(public member function) [edit]
assigns the weak_ptr
(public member function) [edit]
Modifiers
releases the ownership of the managed object
(public member function) [edit]
swaps the managed objects
(public member function) [edit]
Observers
returns the number of shared_ptr objects that manage the object
(public member function) [edit]
checks whether the referenced object was already deleted
(public member function) [edit]
creates a shared_ptr that manages the referenced object
(public member function) [edit]
provides owner-based ordering of weak pointers
(public member function) [edit]

[edit] Non-member functions

specializes the std::swap algorithm
(function template) [edit]

[edit] Helper classes

atomic weak pointer
(class template specialization) [edit]

[edit] Deduction guides(since C++17)

[edit] Notes

Like std::shared_ptr, a typical implementation of weak_ptr stores two pointers:

  • a pointer to the control block; and
  • the stored pointer of the shared_ptr it was constructed from.

A separate stored pointer is necessary to ensure that converting a shared_ptr to weak_ptr and then back works correctly, even for aliased shared_ptrs. It is not possible to access the stored pointer in a weak_ptr without locking it into a shared_ptr.

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 3001 C++17 element_type was not updated for array support updated

[edit] Example

Demonstrates how lock is used to ensure validity of the pointer.

#include <iostream>
#include <memory>
 
std::weak_ptr<int> gw;
 
void observe()
{
    std::cout << "use_count == " << gw.use_count() << ": ";
    if (auto spt = gw.lock()) { // Has to be copied into a shared_ptr before usage
	std::cout << *spt << "\n";
    }
    else {
        std::cout << "gw is expired\n";
    }
}
 
int main()
{
    {
        auto sp = std::make_shared<int>(42);
	gw = sp;
 
	observe();
    }
 
    observe();
}

Output:

use_count == 1: 42
use_count == 0: gw is expired