std::weak_ptr<T>::expired
来自 cppreference.cn
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 (公共成员函数) | |
返回管理该对象的 shared_ptr 对象的数量 (公共成员函数) |