如果 std::greater<>,那么为什么是 std::less(而不是 std::lesser<>)?

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

本文关键字:std gt lt lesser greater 如果 为什么 less      更新时间:2023-10-16

这显然看起来像是一个语法(有趣?)问题,但我希望不是。

我很想知道为什么我们没有std :: greem&lt;>如果我们没有 std::lesser<>(而是我们有std :: Less&lt;>)?拥有greaterlessergreatless是否有意义?我问这个问题,因为我几乎每次都弄乱了它,需要谷歌搜索。

是否有一些标准遵循的命名约定?

我想说的,这实际上只是被告知猜测, 标准的作者明确选择了一种选择一种方式而不是另一种方式。英语以其几乎无限的能力使人们感到困惑,有多种表达相同想法的方式

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

但是,由于 std::greaterstd::less意图是返回一个布尔值,指示相关条件是否为真,因此上面的左侧列是正确的方法。相反,如果返回或多或少的值,则更容易使用右手列。

实际上,如果您查看ISO标准(C++11 20.8.5 Comparisons /4 and /5),您将看到它们的定义(稍微重新格式化):

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.

这些注释部分显然会被理解为"返回x是更大的,而不是y"answers"返回x是 sill 比y"。