public member function
<memory>

std::weak_ptr::expired

bool expired() const noexcept;
Check if expired
Returns whether the weak_ptr object is either empty or there are no more shared_ptr in the owner group it belongs to.

Expired pointers act as empty weak_ptr objects when locked, and thus can no longer be used to restore an owning shared_ptr.

This function shall return the same as (use_count()==0), although it may do so in a more efficient way.

Parameters

none

Return value

true if this is an expired weak_ptr, false otherwise.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// weak_ptr::expired example
#include <iostream>
#include <memory>

int main () {
  std::shared_ptr<int> shared (new int(10));
  std::weak_ptr<int> weak(shared);

  std::cout << "1. weak " << (weak.expired()?"is":"is not") << " expired\n";

  shared.reset();

  std::cout << "2. weak " << (weak.expired()?"is":"is not") << " expired\n";

  return 0;
}

Output:
1. weak is not expired
2. weak is expired


See also