Namespaces
Variants
Views
Actions

std::weak_ptr::expired

From cppreference.com
< cpp‎ | memory‎ | weak ptr
Revision as of 14:49, 2 November 2012 by P12bot (Talk | contribs)

 
 
 
 
 
bool expired() const;
(since C++11)

Checks whether the managed object has already been deleted. Equivalent to use_count() == 0.

Contents

Parameters

(none)

Return value

true if the managed object has already been deleted, false otherwise.

Exceptions

noexcept specification:  
noexcept
  

Notes

expired() may be faster than Template:rlpf.

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


See also

Template:cpp/memory/weak ptr/dcl list lockTemplate:cpp/memory/weak ptr/dcl list use count