std::weak_ptr<T>::expired
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Memory management library
(exposition only*)
(C++11)
(C++23)
(C++11)
(C++17)
(C++11)
(C++11)
(C++20)
(C++20)
(C++17)
(C++11)
(C++17)
(C++20)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
Uninitialized storage (until C++20)
(until C++20*)
(until C++20*)
(until C++20*)
Garbage collector support (until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)
(C++11)
(C++17)
(C++20)
(C++17)
(C++11)
(C++11)
(C++11)
(until C++17*)
(C++11)
(C++17)
(C++26)
(C++26)
(C++11)
(C++11)
(C++11)
(C++23)
(C++23)
(C++11)
(C++20)
(C++11)
(C++11)
(C++20)
(C++26)
std::weak_ptr
Member functions
Modifiers
Observers
weak_ptr::expired
(C++26)
(C++26)
Non-member functions
Helper classes
(C++20)
Deduction guides (C++17)
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).
Contents
[edit] Parameters
(none)
[edit] Return value
true if the managed object has already been deleted, false otherwise.
[edit] Notes
If the managed object is shared among threads, it is only meaningful when expired()
returns true.
[edit] Example
Demonstrates how expired
is used to check validity of the pointer.
Run this code
#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