std::weak_ptr<T>::expired
来自cppreference.com
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 有效\n"; else std::cout << "gw 已过期\n"; } int main() { { auto sp = std::make_shared<int>(42); gw = sp; f(); } f(); }
输出:
gw 有效 gw 已过期
参阅
创建管理被引用的对象的 shared_ptr (公开成员函数) | |
返回管理该对象的 shared_ptr 对象数量 (公开成员函数) |