STD::WEAK_PTR(3)

πŸ’– std::weak_ptr< _Tp > β€” Library Functions Manual πŸ’–

okay bestie, sit down, hydrate, because we're talking about std::weak_ptr< _Tp > from libstdc++ and it's genuinely one of the most underrated glow-ups in the whole standard library. this is the std::weak_ptr manual page, section 3, and she's serving non-owning observer for a pointer owned by a shared_ptr. periodt.


✨ NAME ✨

std::weak_ptr< _Tp > β€” a non-owning observer for a pointer owned by a shared_ptr.

like, she's literally just looking. not touching. not owning. just vibing in the group chat watching the shared_ptrs do their thing. πŸ¦‹


🌸 SYNOPSIS 🌸

#include <memory>

πŸŽ€ Public Types πŸŽ€

  • using element_type β€” the element type, bestie. she has one. iconic.

πŸ’… Public Member Functions πŸ’…

okay so here's the full roster, no cap, every single one:

  • template<typename _Yp, typename = _Constructible<const shared_ptr<_Yp>&>> weak_ptr (const shared_ptr< _Yp > &__r) noexcept β€” build a weak_ptr from a shared_ptr, and it's noexcept so she will not be doing any drama.
  • weak_ptr (const weak_ptr &) noexcept=default β€” copy constructor, defaulted, noexcept.
  • template<typename _Yp, typename = _Constructible<const weak_ptr<_Yp>&>> weak_ptr (const weak_ptr< _Yp > &__r) noexcept β€” converting copy constructor, noexcept.
  • weak_ptr (weak_ptr &&) noexcept=default β€” move constructor, defaulted, noexcept.
  • template<typename _Yp, typename = _Constructible<weak_ptr<_Yp>>> weak_ptr (weak_ptr< _Yp > &&__r) noexcept β€” converting move constructor, noexcept.
  • bool expired () const noexcept β€” is she expired? yes/no, noexcept, no notes.
  • shared_ptr< _Tp > lock () const noexcept β€” the main character move: upgrade to a shared_ptr, noexcept.
  • template<typename _Yp> _Assignable< const shared_ptr< _Yp > & > operator= (const shared_ptr< _Yp > &__r) noexcept β€” assign from a shared_ptr, noexcept.
  • weak_ptr & operator= (const weak_ptr &__r) noexcept=default β€” copy assignment, defaulted, noexcept.
  • template<typename _Yp> _Assignable< const weak_ptr< _Yp > & > operator= (const weak_ptr< _Yp > &__r) noexcept β€” converting copy assignment, noexcept.
  • weak_ptr & operator= (weak_ptr &&__r) noexcept=default β€” move assignment, defaulted, noexcept.
  • template<typename _Yp> _Assignable< weak_ptr< _Yp > > operator= (weak_ptr< _Yp > &&__r) noexcept β€” converting move assignment, noexcept.
  • template<typename _Tp1> bool owner_before (const __shared_ptr< _Tp1, _Lp > &__rhs) const noexcept β€” owner-based ordering against a __shared_ptr, noexcept.
  • template<typename _Tp1> bool owner_before (const __weak_ptr< _Tp1, _Lp > &__rhs) const noexcept β€” owner-based ordering against a __weak_ptr, noexcept.
  • void reset () noexcept β€” drop the reference, noexcept.
  • void swap (__weak_ptr &__s) noexcept β€” swap with another __weak_ptr, noexcept.
  • long use_count () const noexcept β€” how many owners are there? returns a long, noexcept.

(nooo bestie, heads up: these are not member symbols, they're just friends of the family 🫢)

  • template<typename _Tp> void swap (weak_ptr< _Tp > &__a, weak_ptr< _Tp > &__b) noexcept β€” swap overload for weak_ptr.

πŸ‘‘ 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 ⭐

so here's the tea. 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. the way that she just... does that. no help needed. πŸ’…

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. she literally cannot let you touch the ghost. that's the whole point. πŸ™ˆ

ngl this is the safety feature, bestie β€” expired means expired, and that's a good thing, it's what stops you from reading freed memory. do not fight her on this.

πŸ“ Author πŸ“

Generated automatically by Doxygen for libstdc++ from the source code. (she didn't write herself, the robots did, and honestly? they ate.)


and that's the whole page, bestie!! πŸ’– this was std::weak_ptr< _Tp >, page 3 of the libstdc++ Library Functions Manual β€” go forth and observe non-owningly. mwah. πŸŽ€βœ¨πŸ¦‹

πŸ“¦ where this page comes from
  • arch rolling: gcc 16.2.1+r23+gd564253eb6c8-1 Β· GPL-3.0-or-later WITH GCC-exception-3.1 AND GFDL-1.3-or-later
  • fedora 44: libstdc++-docs 16.0.1-0.10.fc44 Β· GPL-3.0-or-later AND LGPL-3.0-or-later AND (GPL-3.0-or-later WITH GCC-exception-3.1) AND (GPL-3.0-or-later WITH Texinfo-exception) AND (LGPL-2.1-or-later WITH GCC-exception-2.0) AND (GPL-2.0-or-later WITH GCC-exception-2.0) AND (GPL-2.0-or-later WITH GNU-compiler-exception) AND BSL-1.0 AND GFDL-1.3-or-later AND Linux-man-pages-copyleft-2-para AND SunPro AND BSD-1-Clause AND BSD-2-Clause AND BSD-2-Clause-Views AND BSD-3-Clause AND BSD-4-Clause AND BSD-Source-Code AND Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 WITH LLVM-Exception) AND ZPL-2.1 AND ISC AND LicenseRef-Fedora-Public-Domain AND HP-1986 AND curl AND Martin-Birgmeier AND HPND-Markus-Kuhn AND dtoa AND SMLNJ AND AMD-newlib AND OAR AND HPND-merchantability-variant AND HPND-Intel
  • fedora 44: libstdc++-docs 16.2.1-2.fc44
  • fedora 44: libstdc++-docs 16.2.1-3.fc44 Β· GPL-3.0-or-later AND LGPL-3.0-or-later AND (GPL-3.0-or-later WITH GCC-exception-3.1) AND (GPL-3.0-or-later WITH Texinfo-exception) AND (LGPL-2.1-or-later WITH GCC-exception-2.0) AND (GPL-2.0-or-later WITH GCC-exception-2.0) AND (GPL-2.0-or-later WITH GNU-compiler-exception) AND BSL-1.0 AND GFDL-1.3-or-later AND Linux-man-pages-copyleft-2-para AND SunPro AND BSD-1-Clause AND BSD-2-Clause AND BSD-2-Clause-Views AND BSD-3-Clause AND BSD-4-Clause AND BSD-Source-Code AND Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 WITH LLVM-Exception) AND ZPL-2.1 AND ISC AND LicenseRef-Fedora-Public-Domain AND HP-1986 AND curl AND Martin-Birgmeier AND HPND-Markus-Kuhn AND dtoa AND SMLNJ AND AMD-newlib AND OAR AND HPND-merchantability-variant AND HPND-Intel
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms