Search code examples
c++assertsmart-pointersweak-ptr

How to use assert to check whether a weak_ptr is nullptr


The use of assert for checking whether a shared_ptr is not nullPtr is explained in c++ how to assert that all std::shared_ptr in a vector are referring to something but I don't find a decent way to check the same for a weak_ptr. I try to avoid converting it to shared_ptr so pls let me know your other solutions.


Solution

  • If you want to check whether the referenced model has already been deleted or whether the weak reference is empty -> use std::weak_ptr::expired().

    reference documentation: https://en.cppreference.com/w/cpp/memory/weak_ptr/expired