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 ashared_ptr, and it'snoexceptso 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 ashared_ptr,noexcept.template<typename _Yp> _Assignable< const shared_ptr< _Yp > & > operator= (const shared_ptr< _Yp > &__r) noexceptβ assign from ashared_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 along,noexcept.
π§ Related Symbols π§
(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 forweak_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