STD::WEAK_PTR(3cxx)
β¨ std::weak_ptr< _Tp > β¨
Okay bestie, so this is the manual page for std::weak_ptr< _Tp > from libstdc++ (that's the GNU C++ standard library, iykyk) and honestly it's giving non-owning observer realness and we're gonna get into all of it π
πΈ NAME πΈ
std::weak_ptr< _Tp > β a non-owning observer for a pointer owned by a shared_ptr. Literally just watches from the sidelines, no cap π
π¦ SYNOPSIS π¦
#include <memory>
Inherits std::__weak_ptr< _Tp, _Lp >. Periodt.
π Public Types π
element_typeβ defined astypename remove_extent< _Tp >::type. That's the type of the thing you're observing, bestie β¨
π Public Member Functions π
Okay so there are so many of these, let's go:
weak_ptr(const shared_ptr< _Yp > &__r)β templated on_Ypwith a_Constructible<const shared_ptr<_Yp>&>constraint, and it'snoexcept. Builds a weak observer from a shared owner πweak_ptr(const weak_ptr &)β copy constructor,noexcept,=default. The classic.weak_ptr(const weak_ptr< _Yp > &__r)β templated on_Ypwith_Constructible<const weak_ptr<_Yp>&>,noexcept. Copy from a different-but-compatible weak_ptr π«Άweak_ptr(weak_ptr &&)β move constructor,noexcept,=default. Zoom zoom.weak_ptr(weak_ptr< _Yp > &&__r)β templated move constructor with_Constructible<weak_ptr<_Yp>>constraint,noexcept.bool expired() const noexceptβ tells you if the observed object has been destroyed. The vibe check of this whole class πshared_ptr< _Tp > lock() const noexceptβ the main character move: upgrades you to an owningshared_ptr(or empty if expired). Slay._Assignable< const shared_ptr< _Yp > & > operator=(const shared_ptr< _Yp > &__r) noexceptβ assign from a shared ptr, templated on_Yp.weak_ptr & operator=(const weak_ptr &__r) noexcept=defaultβ copy assign,=default._Assignable< const weak_ptr< _Yp > & > operator=(const weak_ptr< _Yp > &__r) noexceptβ assign from a compatible weak_ptr, templated.weak_ptr & operator=(weak_ptr &&__r) noexcept=defaultβ move assign,=default._Assignable< weak_ptr< _Yp > > operator=(weak_ptr< _Yp > &&__r) noexceptβ move assign from a compatible weak_ptr, templated.bool owner_before(const __shared_ptr< _Tp1, _Lp > &__rhs) const noexceptβ templated on_Tp1, orders by owner, not by pointed-to value. For when you need strict weak ordering over owners π₯bool owner_before(const __weak_ptr< _Tp1, _Lp > &__rhs) const noexceptβ same energy but against another__weak_ptr, templated on_Tp1.void reset() noexceptβ drop the reference, go back to observing nothing. Iconic.void swap(__weak_ptr &__s) noexceptβ swap with another weak ptr.long use_count() const noexceptβ how manyshared_ptrs are currently owning the object (0 if expired). Lowkey useful π
π Related Symbols π
Note that these are not member symbols, bestie:
template<typename _Tp> void swap(weak_ptr< _Tp > &__a, weak_ptr< _Tp > &__b) noexceptβ the free-function swap overload forweak_ptr. Swap overload forweak_ptr, fr fr.
π Detailed Description π
template<typename _Tp>
class std::weak_ptr< _Tp >
"A non-owning observer for a pointer owned by a shared_ptr."
Since: C++11 β¨
A weak_ptr provides a safe alternative to a raw pointer when you want a non-owning reference to an object that is managed by a shared_ptr.
Unlike a raw pointer, a weak_ptr can be converted to a new shared_ptr that shares ownership with every other shared_ptr that already owns the pointer. In other words, you can upgrade from a non-owning 'weak' reference to an owning shared_ptr, without having access to any of the existing shared_ptr objects. That's the iconic part, honestly π
Also unlike a raw pointer, a weak_ptr does not become 'dangling' after the object it points to has been destroyed. Instead, a weak_ptr becomes expired and can no longer be converted to a shared_ptr that owns the freed pointer β so you cannot accidentally access the pointed-to object after it has been destroyed. Safety slay π‘οΈπ
Bestie please note: an expired weak_ptr is your friend. It's literally how you avoid undefined behavior and use-after-free. Do not fight the expiry check, embrace it π
π Author π
Generated automatically by Doxygen for libstdc++ from the source code. Not a human, just vibes and parser output, ngl π
And that's the whole page! Cute little sign-off: this was libstdc++'s std::weak_ptr< _Tp >(3cxx) manual page, straight from the source, no cap π«Ά Go forth and lock() responsibly, bestie πππ¦
π¦ where this page comes from
- ubuntu noble: libstdc++-14-doc 14-20240412-0ubuntu1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms