There are two reasons why this is probably not a good idea.
First, most standard library algorithms should not use predicates what modify the elements they act on.
Second, std::remove
and std::remove_if
don't give you a good set of "removed" elements*. You can only rely on the elements that are selected to keep. The "removed" elements might in fact be copies of the "good" ones. And since you are storing shared pointers, they could be pointing to the same objects as the "good" elements.
An alternative would be to use std::partition
, then iterate over the relevant section of the partition, then use erase
in a way similar to the erase-remove idiom.
auto p = std::partition(v.begin, v.end(), pred);
std::for_each(p, v.end(), call_method_functor);
v.erase(p, v.end());
* These algorithms should probably have been named keep
and keep_if
algorithm
library, right? So, either way, you're violating some notion of "perfect" programming standards. (Edit: apparently there is but I would still be tempted to do it your way!) – Thole