如何使用断言来检查weak_ptr是否为 nullptr

How to use assert to check whether a weak_ptr is nullptr

本文关键字:ptr 是否 nullptr weak 何使用 断言 检查      更新时间:2023-10-16

使用 assert 检查 shared_ptr 是否不是 nullPtr 在 c++ 中解释如何断言向量中的所有 std::shared_ptr 都引用了某些东西,但我没有找到一种体面的方法来检查weak_ptr相同的内容。我尽量避免将其转换为shared_ptr所以请让我知道您的其他解决方案。

如果要检查引用的模型是否已被删除或弱引用是否为空 ->请使用 std::weak_ptr::expire((。

参考文档:https://en.cppreference.com/w/cpp/memory/weak_ptr/expired