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 as typename 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 _Yp with a _Constructible<const shared_ptr<_Yp>&> constraint, and it's noexcept. 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 _Yp with _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 owning shared_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 many shared_ptrs are currently owning the object (0 if expired). Lowkey useful πŸ“

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 for weak_ptr. Swap overload for weak_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