๐๐ฎ๐ฐ๐ต๐ถ๐ป๐ด ๐๐ถ๐๐ต ๐๐๐ฑ::๐๐ฒ๐ฎ๐ธ_๐ฝ๐๐ฟ ๐ถ๐ป ๐++
๐ฏ๐๐ฎ๐ฐ๐ต๐ถ๐ป๐ด ๐๐ถ๐๐ต ๐๐๐ฑ::๐๐ฒ๐ฎ๐ธ_๐ฝ๐๐ฟ ๐ถ๐ป ๐++
There are a few potential use cases for std::weak_ptr:
โ
Caching
โ
Observer lists
โ
Breaking std::shared_ptr ownership cycles
This post focuses on the first one: caching
๐๐๐ก๐๐ญ ๐ข๐ฌ ๐ฌ๐ญ๐::๐ฐ๐๐๐ค_๐ฉ๐ญ๐ซ?
1๏ธโฃIt is a smart pointer that observes an object managed by std::shared_ptr without participating in its shared ownership. In other words, it neither affects the strong reference count nor ๐ธ๐ฒ๐ฒ๐ฝ๐ ๐๐ต๐ฒ ๐บ๐ฎ๐ป๐ฎ๐ด๐ฒ๐ฑ ๐ผ๐ฏ๐ท๐ฒ๐ฐ๐ ๐ฎ๐น๐ถ๐๐ฒ.
2๏ธโฃThis means a std::weak_ptr has to deal with the possibility that what it points to ๐ต๐ฎ๐ ๐ฎ๐น๐ฟ๐ฒ๐ฎ๐ฑ๐ ๐ฏ๐ฒ๐ฒ๐ป ๐ฑ๐ฒ๐๐๐ฟ๐ผ๐๐ฒ๐ฑ.
3๏ธโฃThe relationship between std::weak_ptr and std::shared_ptr can begin at birth. std::weak_ptrs are typically initialized from std::shared_ptrs and point to the same object as those shared_ptrs. They can also start empty.
4๏ธโฃA std::weak_ptr ๐ฒ๐ ๐ฝ๐ถ๐ฟ๐ฒ๐ when the last std::shared_ptr owning the managed object releases ownership. Since a std::weak_ptr can ๐ผ๐๐๐น๐ถ๐๐ฒ the object it observes, it provides no API to dereference that object directly. Instead, call lock() to get a shared_ptr that keeps the managed object alive while you use it. If the weak_ptr has expired, lock() returns an empty shared_ptr. Checking expiration and getting shared ownership happen in ๐ผ๐ป๐ฒ ๐ฎ๐๐ผ๐บ๐ถ๐ฐ ๐๐๐ฒ๐ฝ.
๐ค๐ฌ๐ผ๐ ๐บ๐ฎ๐ ๐ฏ๐ฒ ๐๐ผ๐ป๐ฑ๐ฒ๐ฟ๐ถ๐ป๐ด ๐ต๐ผ๐ ๐ฎ ๐๐๐ฑ::๐๐ฒ๐ฎ๐ธ_๐ฝ๐๐ฟ ๐ฐ๐ผ๐๐น๐ฑ ๐ฏ๐ฒ ๐๐๐ฒ๐ณ๐๐น.
Imagine a factory function, ๐ญ๐ฐ๐ข๐ฅ๐๐ช๐ฅ๐จ๐ฆ๐ต, that accepts an ID and returns a std::unique_ptr to a read-only Widget holding that ID and its text. Loading that Widget involves ๐ฒ๐
๐ฝ๐ฒ๐ป๐๐ถ๐๐ฒ ๐ผ๐ฝ๐ฒ๐ฟ๐ฎ๐๐ถ๐ผ๐ป๐ such as reading the data for that ID from a file or database.
E.g., loadWidget(3) could use 3 to find a database record, read its text and pass both the ID and text to the Widget constructor.
Get Samiraโs stories inย yourย inbox
Join Medium for free to get updates fromย thisย writer.
๐กIf callers keep requesting the same ID and its data doesnโt change, why repeat the expensive work while the Widget is alive? We can ๐ฐ๐ฎ๐ฐ๐ต๐ฒ results without keeping every Widget alive. ๐ง๐ข๐ด๐ต๐๐ฐ๐ข๐ฅ๐๐ช๐ฅ๐จ๐ฆ๐ต caches std::weak_ptrs and returns std::shared_ptrs:
๐นID found? Return lock()โs shared_ptr if non-empty.
๐นMissing or expired? Call ๐ญ๐ฐ๐ข๐ฅ๐๐ช๐ฅ๐จ๐ฆ๐ต, move its unique_ptr into a shared_ptr, cache a weak_ptr and return the shared_ptr.
โ ๏ธThis only helps while a shared owner still holds the Widget between requests. Otherwise, each call reloads it.
๐กloadWidget uses make_unique. fastLoadWidget transfers ownership through the shared_ptr constructor. make_shared canโt be used here because the Widget already exists. With separate allocations, releasing the last shared owner destroys the Widget and frees its storage. Only the control block stays until the last weak reference is released.
๐Demo is simplified. Real caches may need thread safety, clearing expired entries and data refresh.
๐๐๐ฒ๐บ๐ผ: godbolt.org/z/zWc4W95b8
โก๏ธSource: Effective Modern C++ by Scott Meyers, Item 20





