17

C++ primer, 5th, 14.8.2, Using a Library Function Object with the Algorithms:

vector<string *> nameTable;  // vector of pointers
// error: the pointers in nameTable are unrelated, so < is undefined
sort(nameTable.begin(), nameTable.end(),
     [](string *a, string *b) { return a < b; });
// ok: library guarantees that less on pointer types is well defined
sort(nameTable.begin(), nameTable.end(), less<string*>());

Then I checked the std::less implementation:

template<typename _Tp>
struct less : public binary_function<_Tp, _Tp, bool>
{
  bool
  operator()(const _Tp& __x, const _Tp& __y) const
  { return __x < __y; }
};

I found out that std::less also use operator < to do the work, so why < is undefined and library guarantees that less on pointer types is well defined, why is std:less recommended, and why is std::less better than <.

cong
  • 1,105
  • 1
  • 12
  • 29
  • 2
    Pointer-types are the important part here; you're looking at the primary definition of `less`, but I'd bet it also has a specialization for pointers that is far more relevant to your question. Importantly: "[*A specialization of `std::less` for any pointer type yields a strict total order, even if the built-in `operator<` does not.*](http://en.cppreference.com/w/cpp/utility/functional/less)" – ildjarn Mar 13 '17 at 11:34
  • 3
    On your system the `<` operator might work the same as `std::less`, but it is not required to. – Bo Persson Mar 13 '17 at 11:39

1 Answers1

17

Because < isn't always operator<(). Only classes have operator functions, so your suggestion would not work for the built-in types.

Furthermore, < on pointers doesn't necessarily implement a strict-weak ordering, whereas std::less (via specialisation — what you posted isn't "all" of std::less!) is required to:

A specialization of std::less for any pointer type yields a strict total order, even if the built-in operator< does not.

In short: std::less works for anything that supports a less-than comparison.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055