##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::weak_ptr::expired

From cppreference.com
< cpp‎ | memory‎ | weak ptr

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

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

### [edit] Parameters

(none)

### [edit] Return value

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

### [edit] Exceptions

`noexcept` specification:
noexcept

### [edit] Notes

This function is inherently racy if the managed object is shared among threads. In particular, a false result may become stale before it can be used. A true result is reliable.

### [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

### [edit] See also

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

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/memory/weak_ptr/expired&oldid=91474"
Category:

- unconditionally noexcept

Hidden categories:

- Pages with unreviewed unconditional noexcept template
- Pages with unreviewed noexcept template