Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If std::greater<>, then why std::less (and not std::lesser<>)?

Tags:

c++

This apparently looks like a grammatical (and funny?) question but I hope it is not.

I seriously wonder why we have std::greater<> if we don't have std::lesser<> (instead we have std::less<>)? Doesn't it make sense to have either greater and lesser or great and less? I ask this question because I pretty much mess it up every single time and need to Google it.

Is there some naming convention that the standard follows?

like image 963
user6490375 Avatar asked Sep 12 '17 02:09

user6490375


People also ask

What is greater in C++ with example?

std::greater in C++ with Examples. The std::greater is a functional object which is used for performing comparisons. It is defined as a Function object class for the greater-than inequality comparison. This can be used for changing the functionality of the given function. This can also be used with various standard algorithms such as sort ...

What is the use of less function in C++?

The std::less is a is a member of the functional class (<functional.h>) used for performing comparisons. It is defined as a function object class for less than inequality comparison which returns a boolean value depending upon the condition.

Why does C++ have a new typeless<void>?

Even if the technology used by std::less<void> existed since C++98 (template type deduction in class methods), this new addition is consistent with the direction of the language: offloading type deduction to the compiler. This is what other features of Modern C++ also allow, such as auto and template type deduction for constructors.


1 Answers

I would say, and this is really just informed speculation, that the authors of the standard made an explicit choice to go one way rather than the other. English, in its near-infinite capacity for confusing people, has many ways for expressing the same idea

a is GREATER than b   =>   a is the GREATER value
a is LESS    than b   =>   a is the LESSER  value

However, since the intent of std::greater and std::less is to return a Boolean value indicating whether the related condition is true, the left-hand column above is the right way to go. If instead it returned the greater or lesser value, it would be more apt to use the right hand column.

In fact, if you look at the ISO standard (C++11 20.8.5 Comparisons /4 and /5), you'll see how they're defined (slightly reformatted):

template <class T> struct greater {
    bool operator()(const T& x, const T& y) const;
    typedef T first_argument_type;
    typedef T second_argument_type;
    typedef bool result_type;
};                                                   // operator() returns x > y.
template <class T> struct less {
    bool operator()(const T& x, const T& y) const;
    typedef T first_argument_type;
    typedef T second_argument_type;
    typedef bool result_type;
};                                                   // operator() returns x < y.

Those comment sections would clearly be read as "returns x is greater than y" and "returns x is less than y".

like image 130
paxdiablo Avatar answered Oct 23 '22 04:10

paxdiablo