3
votes

I am currently trying to replace some portions of my code with std::shared_ptr and std::weak_ptr. In many parts I have std::vectors of std::weak_ptrs (which were raw ptrs before) and use std::find to find a certain pointer in the array. This does not work with weak pointers since they don't implement == operators for various reasons. I have to stick to std::weak_ptr to avoid circular dependencies. What are my options, should I go back to using raw pointers instead of weak pointers?

example

//_window is a shared_ptr that I want to find in a vector of weak_ptrs
WindowWeakPtrArray::iterator it = std::find(m_windows.begin(), m_windows.end(), _window); 

Thanks

1

1 Answers

10
votes

Without considering the design, you can use std::find_if with a custom comparator that verifies whether the weak_ptr is valid and then compares the internal pointers if they are.