Title
Algorithms do not use binary predicate objects for default comparisons
Status
nad
Section
[alg.sorting]
Submitter
Pablo Halpern

Created on 2000-03-06.00:00:00 last changed 171 months ago

Messages

Date: 2010-10-21.18:28:33

Rationale:

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<.

Date: 2000-03-06.00:00:00

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.

History
Date User Action Args
2010-10-21 18:28:33adminsetmessages: + msg1887
2000-03-06 00:00:00admincreate