当我试图学习c++操作符时,我偶然发现了下面的表格,其中列出了一个奇怪的比较操作符。<=>操作符有什么作用?

自2017年以来,cppreference.com更新了该页面,现在包含关于<=>操作符的详细信息。


当前回答

c++ 20中引入了三元比较运算符(<=>)。

该表达式返回如下所示的对象;

auto cmp  = a <=> b;

cmp > 0 if a > b
cmp = 0 if a == b
cmp < 0 if a < b  

示例程序

#include <iostream>

using namespace std;

int main()
{
        int lhs = 10, rhs = 20;
        auto result = lhs <=> rhs;

        if (result < 0) {
                cout << "lhs is less than rhs" << endl;
        }
        else if (result > 0) {
                cout << "lhs is greater than rhs" << endl;
        }
        else {
                cout << "lhs and rhs are equal" << endl;
        }

}

如何编译和运行?

g++-10 threewaycmp.cpp -std=c++20
./a.out

结果

lhs is less than rhs

详情请参考以下连结 https://en.cppreference.com/w/cpp/language/operator_comparison

其他回答

这个答案已经变得无关紧要,因为引用的网页已经改变了

您正在引用的网页已损坏。那天它被编辑了很多次,不同的部分不同步。我当时看到的状态是:

在页面的顶部,它列出了当前存在的比较操作符(在c++ 14中)。这里没有<=>。

在页面的底部,他们应该列出相同的操作符,但他们做了个蠢事,添加了这个未来的建议。

gcc还不知道<=>(和-std=c++14,永远不会),所以它认为你的意思是<=> b。这解释了错误消息。

如果你在五年后尝试同样的事情,你可能会得到一个更好的错误消息,比如<=>不是c++ 14的一部分。

这叫做三向比较运算符。

根据P0515论文提案:

There’s a new three-way comparison operator, <=>. The expression a <=> b returns an object that compares <0 if a < b, compares >0 if a > b, and compares ==0 if a and b are equal/equivalent. To write all comparisons for your type, just write operator<=> that returns the appropriate category type: Return an _ordering if your type naturally supports <, and we’ll efficiently generate <, >, <=, >=, ==, and !=; otherwise return an _equality, and we’ll efficiently generate == and !=. Return strong if for your type a == b implies f(a) == f(b) (substitutability, where f reads only comparison-salient state accessible using the nonprivate const interface), otherwise return weak.

cppreference表示:

三向比较运算符表达式的形式为 LHS <=> RHS (1) 表达式返回一个对象 如果LHS < RHS,则比较<0 LHS > RHS比较>0 如果LHS和RHS相等/等效,则比较==0。

<=>的优点是用于复杂类型,其比较代价很高,例如树导航。你可以“int x = A <=> B;”一次,然后从x中确定>,<,>=,<=,==,!=,而不需要对A和B进行额外的比较。对于树,想象bool find(根,A){如果根为nullptr返回false;int x = A <=> root->B;If !x,你发现它返回真,否则如果x < 0,找到左,否则找到右}。(对尾递归进行调优。)

我以为有些语言有一个三向控制流,像开关,但很难通过谷歌回忆。这种情况可以追溯到C的strcmp(), memcmp(),以及JAVA的compareTo()。众包我!

c++ 20中引入了三元比较运算符(<=>)。

该表达式返回如下所示的对象;

auto cmp  = a <=> b;

cmp > 0 if a > b
cmp = 0 if a == b
cmp < 0 if a < b  

示例程序

#include <iostream>

using namespace std;

int main()
{
        int lhs = 10, rhs = 20;
        auto result = lhs <=> rhs;

        if (result < 0) {
                cout << "lhs is less than rhs" << endl;
        }
        else if (result > 0) {
                cout << "lhs is greater than rhs" << endl;
        }
        else {
                cout << "lhs and rhs are equal" << endl;
        }

}

如何编译和运行?

g++-10 threewaycmp.cpp -std=c++20
./a.out

结果

lhs is less than rhs

详情请参考以下连结 https://en.cppreference.com/w/cpp/language/operator_comparison

默认<=>自动提供==,!=,<,>,<=,>=

c++ 20有一个新的“默认比较”特性设置,因此默认<=>将免费提供所有其他的比较。我相信这是添加运算符<=>的主要动机。

改编自https://en.cppreference.com/w/cpp/language/default_comparisons:

main.cpp

#include <cassert>
#include <compare>
#include <set>

struct Point {
    int x;
    int y;
    auto operator<=>(const Point&) const = default;
};

int main() {
    Point pt1{1, 1}, pt2{1, 2};

    // Just to show it Is enough for `std::set`.
    std::set<Point> s;
    s.insert(pt1);

    // All of these are automatically defined for us!
    assert(!(pt1 == pt2));
    assert( (pt1 != pt2));
    assert( (pt1 <  pt2));
    assert( (pt1 <= pt2));
    assert(!(pt1 >  pt2));
    assert(!(pt1 >= pt2));
}

编译并运行:

sudo apt install g++-10
g++-10 -ggdb3 -O0 -std=c++20 -Wall -Wextra -pedantic -o main.out main.cpp
./main.out

上述内容的一个等效的更明确的版本是:

struct Point {
    int x;
    int y;
    auto operator<=>(const Point& other) const {
        if (x < other.x) return -1;
        if (x > other.x) return 1;
        if (y < other.y) return -1;
        if (y > other.y) return 1;
        return 0;
    }
    bool operator==(const Point& other) const = default;
};

在这种情况下,我们需要显式地设置bool operator==(const Point& other) const = default;因为如果operator<=>没有默认值(例如上面明确给出的),那么operator==不会自动默认值:

根据任何运算符<=>重载的规则,默认的<=>重载也将允许该类型与<、<=、>和>=进行比较。 如果operator<=>是默认值,而operator==完全没有声明,则operator==是隐式默认值。

上面的例子使用了与默认操作符<=>相同的算法,正如cppreference解释的那样:

默认操作符<=>执行字典比较,先后比较T的基本子对象(从左到右深度优先)和非静态成员子对象(按声明顺序)来计算<=>,递归地展开数组成员(按下标增加的顺序),并在发现不相等的结果时提前停止

在c++ 20之前,你不能做像operator== = default这样的事情,并且定义一个操作符不会导致其他操作符被定义,例如使用-std=c++17编译失败:

#include <cassert>

struct Point {
    int x;
    int y;
    auto operator==(const Point& other) const {
        return x == other.x && y == other.y;
    };
};

int main() {
    Point pt1{1, 1}, pt2{1, 2};

    // Do some checks.
    assert(!(pt1 == pt2));
    assert( (pt1 != pt2));
}

错误:

main.cpp:16:18: error: no match for ‘operator!=’ (operand types are ‘Point’ and ‘Point’)
   16 |     assert( (pt1 != pt2));
      |              ~~~ ^~ ~~~
      |              |      |
      |              Point  Point

然而,上面的代码在-std=c++20下编译。

相关:任何c++操作符重载是基于其他操作符自动提供的吗?

在Ubuntu 20.04、GCC 10.2.0上测试。