C++ Reference ยท Memory and Utilities
C++ std::weak_ptr class template
Reference for C++ std::weak_ptr: observe a shared_ptr-managed object without owning it, lock(), expired(), breaking ownership cycles and building caches.
std::weak_ptr<T>, declared in <memory>, refers to an object managed by a shared_ptr without keeping it alive. It does not add to the strong reference count. You cannot dereference it directly; instead you call lock(), which returns a shared_ptr that is either valid (the object still exists) or empty (it is gone).
Typical uses are back-pointers from child to parent, observer lists and caches that should not prevent cleanup.
std::weak_ptr<Node> w = strong; // observe if (auto s = w.lock()) { /* use *s safely */ }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
template<class T> class weak_ptr; weak_ptr() noexcept; weak_ptr(const shared_ptr<T>& r) noexcept; weak_ptr(const weak_ptr& r) noexcept; weak_ptr(weak_ptr&& r) noexcept;
| Member | Description |
|---|---|
lock() |
Returns a shared_ptr owning the object, or an empty one if it expired. Atomic. |
expired() |
true if no shared_ptr owns the object any more. |
use_count() |
Number of shared_ptr owners (0 if expired). |
reset() |
Makes this weak_ptr empty. |
swap(other) |
Exchanges with another weak_ptr. |
owner_before(other) |
Ordering used for containers keyed by weak_ptr. |
Returns: see the table. Errors: none of the members throws. Constructing a shared_ptr directly from an expired weak_ptr (std::shared_ptr<T> s(w)) throws std::bad_weak_ptr; that is why lock() is the normal route.
Usage notes
- Always go through
lock()and test the result. Checkingexpired()and then locking in two steps is a race in multithreaded code, because the object can die in between. Onelock()call is atomic. - Keep the
shared_ptrfromlock()alive only as long as you need it. Holding it extends the object's lifetime. - The control block lives until the last
weak_ptris gone too. Withmake_sharedthe object's memory is therefore not returned until the weak pointers vanish, though the destructor already ran. - A
weak_ptrcannot be dereferenced and has noget(). That is intentional: it forces you to prove the object is alive. - Use
weak_ptrfor the back-reference in parent/child or observer/subject links. The owning direction usesshared_ptr. - A weak cache (
std::map<Key, std::weak_ptr<Value>>) lets objects disappear when nobody uses them. Clean out expired entries periodically. - A
weak_ptrdoes not supportoperator<,==, orboolconversion. Useowner_beforeor compare locked pointers. enable_shared_from_thisstores aweak_ptrinside the object for you; see enable_shared_from_this.