Sitemap
Samira

Samira

Backend C++ engineer passionate about solving complex problems with C++ and multi-threading, optimizing apps for advanced research in biotechnology.

๐—–๐—ฎ๐—ฐ๐—ต๐—ถ๐—ป๐—ด ๐˜„๐—ถ๐˜๐—ต ๐˜€๐˜๐—ฑ::๐˜„๐—ฒ๐—ฎ๐—ธ_๐—ฝ๐˜๐—ฟ ๐—ถ๐—ป ๐‚++

2 min readOct 23, 2026

๐ŸŽฏ๐—–๐—ฎ๐—ฐ๐—ต๐—ถ๐—ป๐—ด ๐˜„๐—ถ๐˜๐—ต ๐˜€๐˜๐—ฑ::๐˜„๐—ฒ๐—ฎ๐—ธ_๐—ฝ๐˜๐—ฟ ๐—ถ๐—ป ๐‚++
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

Press enter or click to view image in full size
Press enter or click to view image in full size

--

--

Samira
Samira

Written by Samira

Backend C++ engineer passionate about solving complex problems with C++ and multi-threading, optimizing apps for advanced research in biotechnology.