std::weak_ptr::expired(3)
π std::weak_ptr::expired(3) π
Okay bestie, welcome to the C++ Standard Libary manual page for std::weak_ptr::expired β yes, thatβs the manual name, typo and all, and weβre keeping it exactly as it is. This one is short, iconic, and lowkey important, so letβs get into it. π
β¨ NAME β¨
std::weak_ptr::expired - std::weak_ptr::expired
Itβs literally the std::weak_ptr::expired member function. Main character energy, no cap. π
πΈ Synopsis πΈ
bool expired() const noexcept; (since C++11)
Equivalent to use_count() == 0. The destructor for the managed object may not yet have been called, but this object's destruction is imminent (or may have already happened).
So yeah, itβs giving βis this thing already gone or about to be gone?β β and the noexcept is there, periodt. β¨
π Parameters π
Nope, nada, (none) β no parameters here, bestie. π«Ά
π Return value π
It returns true if the managed object has already been deleted, and false otherwise.
Simple. Clean. Iconic. π¦
π¦ Notes π¦
β οΈ This function is inherently racy if the managed object is shared among threads. In particular, afalseresult may become stale before it can be used. Atrueresult is reliable.
Bestie, read that again fr fr β the racy part is not a vibe. A false can go stale before you even use it, but a true result is reliable. Not me being dramatic, thatβs just what the page says. π
π Example π
This example demonstrates how expired is used to check validity of the pointer.
The original says // Run this code, so hereβs the code, exactly as it should be:
#include <iostream>
#include <memory>
std::weak_ptr<int> gw;
void f()
{
if (!gw.expired())
std::cout << "gw is valid\n";
else
std::cout << "gw is expired\n";
}
int main()
{
{
auto sp = std::make_shared<int>(42);
gw = sp;
f();
}
f();
}
β Output β
gw is valid
gw is expired
Ate. π π₯
π₯ See also π₯
lockβ creates ashared_ptrthat manages the referenced object (public member function)use_countβ returns the number ofshared_ptrobjects that manage the object (public member function)
xoxo, your Unix bestie π β sourced from http://cppreference.com, dated/versioned 2024.06.10, page std::weak_ptr::expired(3) π«Άβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms