Why did c++11 add a separate find_if()
instead of simply overloading the existing find()
?
Wouldn't overloading the function be sufficient?

- 8,591
- 5
- 31
- 50

- 53
- 3
-
Why questions are tricky because they tend to be opinionated and few people know first hand how a decision was made. – Martin K Feb 29 '20 at 20:55
1 Answers
Let's take a look at two of the relevant functions:
template <class InputIt, class T>
InputIt find(InputIt first, InputIt last, const T& value);
template <class InputIt, class UnaryPredicate>
InputIt find_if(InputIt first, InputIt last, UnaryPredicate p);
Observe that both have an iterator-range first, and return an iterator into it (not that we can overload on that), which does not help distinguish them.
Third and last, find()
has a value to compare against, while find_if()
has a unary predicate (object which can be called with a single element of the range, and returns something convertible to bool
).
Now, if we don't know which of the two we have, how can we distinguish?
Unfortunately, we cannot do so in the general case; while most arguments will fill at most one of the roles, it is trivial to create those fulfilling both, optionally with conflicting semantics.
An additional argument to disambiguate, while not impossible, would be cumbersome, inefficient, and degrade compile-time checking.
Thus, in the end we are left with choosing different function-names.

- 8,591
- 5
- 31
- 50