In my opinion, this use of std::find_if bit misleading. When I read this piece of code, I do not expect any side effects, I just expect the server name to be found. The fact that the result of find_if will be discarded will also make me wonder if the code is really correct. Perhaps a different name for the predicate will make the goal clearer, but I think the problem is more fundamental.
For most people, find_if is a query , not a modifying algorithm. Despite the fact that you do not actually change the values that are repeated, you change the global state of your application (in this case, you can even change the state of remote servers).
In that case, I would probably stick with a manual loop, especially now that C ++ 11 introduced range-based loops:
for (std::string const & name : serverNames) { if (ContactServer(name)) break; }
Another solution would be to encapsulate this in a function with a name that more clearly displays the intent, like apply_until or something like this:
template <typename InputIterator, typename Function> void apply_until(InputIterator first, InputIterator last, Function f) { std::find_if(first, last, f);
But maybe I'm too purist :)!
source share