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.
1 Answer
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
assertmacro, note that it's "disabled" (expands to nothing) on typical release builds. It will also "crash" your program if it fails. Therefore it shouldn't really be used for run-time check in production builds.shared_ptrcannot benullPtr(if I'm reading the intent of that name correctly). It can hold a null pointer. With aweak_ptrthere are two possible ways that it might not refer to actual memory: theshared_ptrs that it's associated with might hold a null pointer or it might not be associated with anyshared_ptrs. So there isn't anything that's "the same", but two things that are similar. You'll have to say which one you mean, or you'll get guesses instead of actual answers.