Namespaces
Variants

std::weak_ptr<T>:: expired

From cppreference.net
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (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)
(C++11) (until C++23)
bool expired ( ) const noexcept ;
(C++11以降)

use_count ( ) == 0 と等価です。管理対象オブジェクトのデストラクタはまだ呼び出されていない可能性がありますが、このオブジェクトの破棄は差し迫っている(または既に発生している)状態です。

目次

パラメータ

(なし)

戻り値

true 管理対象オブジェクトが既に削除されている場合、 false それ以外の場合。

注記

管理対象オブジェクトが複数のスレッド間で共有されている場合、 expired() がtrueを返すときのみ意味を持ちます。

expired を使用してポインタの有効性をチェックする方法を示します。

#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();
}

出力:

gw is valid
gw is expired

関連項目

参照されているオブジェクトを管理する shared_ptr を作成する
(public member function)
オブジェクトを管理している shared_ptr オブジェクトの数を返す
(public member function)