If I had a STL container, say a list of pointers I could remove them like in the exmple below. With a container of weak_ptrs this does not work, because they cannot be compared, as they need to be locked first. What can I do?
void MyClass::RemoveItem(std::tr1::weak_ptr<Item> const & pItem)
{
mylist.remove(pItem);
}
For one thing, you could just define operator == for any weak_ptr. I’m sure there’s a reason this is not implemented, it can probably bite you at a later point.
… and you’ll be able to just call remove() as usual. This is a little bit extreme I guess.
If you stick to the remove_if() approach, you could get rid of the bind magic* by using function object:
and then use it like this:
It looks like more code, but you can compress the EqPredicate class, it’s mostly hollow. Also, it could be made template to use it with lists containing types other than Item.
Oh, and do pass you weak_ptrs by reference everywhere including your comparison function.
*bind is not free performance-wise. If you expect a lot of Remove() calls and care much about performance it might be good to avoid it.