A non-owning observer of a
shared_ptr-managed object: it can tell whether the object is still alive and temporarily take ownership, but it never keeps it alive. Header:<memory>· Since: C++11
A weak_ptr points at the same control block as a shared_ptr but increments
only the weak count, not the strong count. So it doesn't prevent destruction.
To use the object you must lock() it, which atomically returns a shared_ptr
(promoting to owner) if the object still exists, or an empty one if it's gone.
shared_ptr sp ──┐ ┌── weak_ptr wp
▼ ▼
┌──────────────────────────────┐
│ control block │ object dies at strong == 0
│ strong = 1 weak = 1 │──▶ control block dies at
└──────────────────────────────┘ strong == 0 && weak == 0
template<class T> class weak_ptr;| Member | Meaning |
|---|---|
weak_ptr(const shared_ptr<Y>&), = sp |
Observe what sp owns |
lock() |
shared_ptr<T> that owns the object, or empty if it's gone (atomic) |
expired() |
use_count() == 0 — a hint, not a guarantee in threaded code |
use_count() |
Number of shared_ptr owners right now |
reset(), swap |
Stop observing; swap |
owner_before (and C++26 owner_hash, owner_equal — GCC 16 has them, GCC 15 and Clang 22 don't) |
Order/compare by control block, for keys in associative containers |
shared_ptr<T>(wp) is the throwing alternative to lock(): it throws
std::bad_weak_ptr if the object is gone.
- Fits: back/parent pointers in a
shared_ptrgraph, caches and registries that shouldn't keep entries alive, callbacks that must not extend the lifetime of the object they call back into. - Anti-fits: objects with a single, clear owner — use
unique_ptrand a raw pointer or reference for observers whose lifetime is known to be shorter.
auto sp = std::make_shared<Widget>();
std::weak_ptr<Widget> wp = sp; // does NOT bump strong count
if (auto locked = wp.lock()) { // returns shared_ptr<Widget> or empty
locked->use(); // safe: object is alive for this scope
} else {
// object already destroyed
}
wp.expired(); // true once the last shared_ptr is gonestruct Node {
std::shared_ptr<Node> next; // forward edge: owns
std::weak_ptr<Node> prev; // back edge: observes → no cycle, no leak
};This is the canonical cure for the shared_ptr cycle leak: make the "back" or
"parent" pointer a weak_ptr.
struct Value {};
std::unordered_map<std::string, std::weak_ptr<Value>> cache;
// entries don't keep values alive; lock() to fetch, re-create on miss/expired#include <functional>
#include <memory>
struct Session : std::enable_shared_from_this<Session> {
void on_data() {}
std::function<void()> callback() {
return [self = weak_from_this()] { // weak_from_this: C++17
if (auto s = self.lock()) s->on_data();
// else: the session is gone — do nothing
};
}
};- You can't dereference a
weak_ptrdirectly. There's nooperator*/->; you mustlock()(or construct ashared_ptrfrom it, which throwsstd::bad_weak_ptrif expired).lock()is the safe path. expired()then use is a race. In multithreaded code, check-then-use can let the object die in between.lock()is atomic — use its result directly.- It still pins the control block (not the object). With
make_shared, the object's storage lives until all weak_ptrs are gone too; for very large objects that can delay memory release. weak_from_this()is empty unless ashared_ptrowns the object. Called on a stack ornew-allocated object, it returns an expiredweak_ptr.