如何使用 assert 检查 weak_ptr 是否为 nullptr

How to use assert to check whether a weak_ptr is nullptr

中解释了使用断言来检查 shared_ptr 是否不是 nullPtr,但我没有找到一个合适的方法来检查 weak_ptr 的相同方法。我尽量避免将其转换为 shared_ptr 所以请让我知道您的其他解决方案。

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

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