|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | ||||
| Modifiers | ||||
| Observers | ||||
weak_ptr::expired | ||||
(C++26) | ||||
(C++26) | ||||
| Non-member functions | ||||
| Helper classes | ||||
(C++20) | ||||
| Deduction guides(C++17) |
bool expired()constnoexcept; | (since C++11) | |
Equivalent touse_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 |
(none)
true if the managed object has already been deleted,false otherwise.
If the managed object is shared among threads, it is only meaningful whenexpired() returns true.
Demonstrates howexpired is used to check validity of the pointer.
#include <iostream>#include <memory> std::weak_ptr<int> gw; void f(){if(!gw.expired())std::cout<<"gw is valid\n";elsestd::cout<<"gw is expired\n";} int main(){{auto sp=std::make_shared<int>(42);gw= sp; f();} f();}
Output:
gw is validgw is expired
creates ashared_ptr that manages the referenced object(public member function)[edit] | |
returns the number ofshared_ptr objects that manage the object(public member function)[edit] |