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)