Namespaces
Variants
Views
Actions

std::weak_ptr<T>::expired

From cppreference.com
< cpp‎ | memory‎ | weak ptr
 
 
Utilities library
Language support
Type support (basic types, RTTI)
Library feature-test macros (C++20)
Dynamic memory management
Program utilities
Coroutine support (C++20)
Variadic functions
Debugging support
(C++26)
Three-way comparison
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)   
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)
Elementary string conversions
(C++17)
(C++17)

 
Dynamic memory management
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Allocators
Garbage collection support
(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)



 
 
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.

#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

[edit] See also

creates a shared_ptr that manages the referenced object
(public member function) [edit]
returns the number of shared_ptr objects that manage the object
(public member function) [edit]