218. Algorithms do not use binary predicate objects for default comparisons

Section: 27.8 [alg.sorting] Status: NAD Submitter: Pablo Halpern Opened: 2000-03-06 Last modified: 2016-01-28 10:19:27 UTC

Priority: Not Prioritized

View all other issues in [alg.sorting].

View all issues with NAD status.


Many of the algorithms take an argument, pred, of template parameter type BinaryPredicate or an argument comp of template parameter type Compare. These algorithms usually have an overloaded version that does not take the predicate argument. In these cases pred is usually replaced by the use of operator== and comp is replaced by the use of operator<.

This use of hard-coded operators is inconsistent with other parts of the library, particularly the containers library, where equality is established using equal_to<> and ordering is established using less<>. Worse, the use of operator<, would cause the following innocent-looking code to have undefined behavior:

vector<string*> vec;
sort(vec.begin(), vec.end());

The use of operator< is not defined for pointers to unrelated objects. If std::sort used less<> to compare elements, then the above code would be well-defined, since less<> is explicitly specialized to produce a total ordering of pointers.


This use of operator== and operator< was a very deliberate, conscious, and explicitly made design decision; these operators are often more efficient. The predicate forms are available for users who don't want to rely on operator== and operator<.