Skip to content
Tutorials Center
My learning

Topics

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.

C++
std::weak_ptr<Node> w = strong;     // observe
if (auto s = w.lock()) { /* use *s safely */ }

Try it

C++ lock() and expired()

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.

C++ Breaking an ownership cycle

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

C++
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. Checking expired() and then locking in two steps is a race in multithreaded code, because the object can die in between. One lock() call is atomic.
  • Keep the shared_ptr from lock() alive only as long as you need it. Holding it extends the object's lifetime.
  • The control block lives until the last weak_ptr is gone too. With make_shared the object's memory is therefore not returned until the weak pointers vanish, though the destructor already ran.
  • A weak_ptr cannot be dereferenced and has no get(). That is intentional: it forces you to prove the object is alive.
  • Use weak_ptr for the back-reference in parent/child or observer/subject links. The owning direction uses shared_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_ptr does not support operator<, ==, or bool conversion. Use owner_before or compare locked pointers.
  • enable_shared_from_this stores a weak_ptr inside the object for you; see enable_shared_from_this.