当前位置: 首页 > news >正文

c++20引入的三路比较操作符<=>

目录

一、简介

二、三向比较的返回类型

2.1 std::strong_ordering

2.2 std::weak_ordering

2.3 std::partial_ordering

三、对基础类型的支持

四、自动生成的比较运算符函数

4.1 std::rel_ops的作用

4.2 使用<=>

五、兼容他旧代码


一、简介

c++20引入了三路比较操作符(或者三向比较)<=>(three-way comparison operator),也叫太空船(spaceship)操作符。

<=>也是一个二元关系运算符,但它不像其他二元比较操作符那样返回类型是布尔类型,而是根据用户指明的三种类型之一:partial_ordering、weak_ordering和strong_ordering,定义于标准库头文件<compare>中,默认为strong_ordering类型。

  • 偏序partial_ordering表达了比较关系中的偏序关系,即给定类的任意两个对象不一定可比较。例如给定一棵对象树,假设父节点比子节点大,<=>得到的结果将为greater,但不是任意两个节点都可比较,此时它们的关系为unordered。对于偏序关系的排序,使用拓扑排序算法将获得正确的结果。
  • 弱序weak_ordering表达了比较关系中的全序关系,即给定类的任意两个对象都能比较,将既不大于也不小于的关系定义为等价(equivalent)关系。假设长方形类按照面积比较就是弱序关系,长宽分别为2和6的矩形与长宽分别为3和4的比较,面积都为12(既不大于也不小于)​,那么它们是等价的,但不相等是因为可以通过长宽区分出来它们不一样。标准库中的std::sort要求关系至少为弱序的才能正确工作。
  • 强序strong_ordering与弱序一样,当对等价关系进行了约束即为相等(equal)关系。考虑正方形类按照面积比较就是强序关系,因为面积一样的正方形无法像长方形那样通过外表能区分出来,即它们是相等的。一些查找算法要求关系为强序才能正确工作。

此外<=>的结果也与字符串比较函数strcmp类似,能够通过正负判断关系:当结果大于0表示大于关系,等于0表示等价、等于关系,小于0表示小于关系。

顾名思义,三向比较就是在形如lhs <=> rhs的表达式中,两个比较的操作数lhs和rhs通过<=>比较可能产生3种结果,该结果可以和0比较,小于0、等于0或者大于0分别对应lhs < rhs、lhs == rhs和lhs > rhs。举例来说:

#include<iostream>int main(int argc,char* argv[]){bool b = 7<=>11 <0;std::cout<<std::boolalpha<<b<<std::endl;b = 7<=>11 ==0;std::cout<<std::boolalpha<<b<<std::endl;b = 7<=>11 >0;std::cout<<std::boolalpha<<b<<std::endl;b = 7<=>7 ==0;std::cout<<std::boolalpha<<b<<std::endl;return 0;
}

输出:

true
false
false
true

请注意,运算符<=>的返回值只能与0和自身类型来比较,如果同其他数值比较,编译器会报错:

#include<iostream>int main(int argc,char* argv[]){bool b = 7<=>11 <100;//编译失败,<=>的结果不能与除0以外的数值比较std::cout<<std::boolalpha<<b<<std::endl;return 0;
}


二、三向比较的返回类型

<=>的返回结果并不是一个普通类型,根据标准三向比较会返回3种类型,分别为std::strong_ordering、std::weak_ordering以及std::partial_ordering,而这3种类型又会分为有3~4种最终结果。


2.1 std::strong_ordering

std::strong_ordering类型有3种比较结果,分别为std::strong_ ordering::less、std::strong_ordering::equal以及std::strong_ ordering::greater。表达式lhs <=> rhs分别表示lhs < rhs、lhs == rhs以及lhs > rhs。std::strong_ordering类型的结果强调的是strong的含义,表达的是一种可替换性,简单来说,若lhs == rhs,那么在任何情况下rhs和lhs都可以相互替换,也就是fx(lhs) == fx(rhs)。

对于基本类型中的int类型,三向比较返回的是std::strong_ordering,例如:

用MSVC编译运行以上代码,会在输出窗口显示class std::strong_ ordering,刻意使用MSVC是因为它的typeid(x).name()可以输出友好可读的类型名称。

对于有复杂结构的类型,std::strong_ordering要求其数据成员和基类的三向比较结果都为std::strong_ordering。例如:

#include<iostream>struct B{int a;long b;auto operator <=> (const B&) const = default;
};struct D : B{short c;auto operator <=> (const D&) const = default;
};int main(int argc,char* argv[]){D x1,x2;std::cout<<typeid(decltype(x1 <=> x2)).name()<<std::endl;
}

上面这段代码用MSVC编译运行会输出class std::strong_ordering。

请注意,默认情况下自定义类型是不存在三向比较运算符函数的,需要用户显式默认声明,比如在结构体B和D中声明auto operator <=> (const B&) const = default;和auto operator <=> (const D&)const = default;。

 如果删除基类的<=>运算符,派生类显式定义的<=>将被删除。

如果删除派生类的<=>,保留基类的<=>,还可以运行。

 对结构体B而言,由于int和long的比较结果都是std::strong_ordering,因此结构体B的三向比较结果也是std::strong_ordering。同理,对于结构体D,其基类和成员的比较结果是std::strong_ordering,D的三向比较结果同样是std::strong_ordering。

另外,明确运算符的返回类型,使用std::strong_ ordering替换auto也是没问题的。


2.2 std::weak_ordering

std::weak_ordering类型也有3种比较结果,分别为std::weak_ ordering::less、std::weak_ordering::equivalent以及std::weak_ ordering::greater。std::weak_ordering的含义正好与std::strong_ ordering相对,表达的是不可替换性。即若有lhs == rhs,则rhs和lhs不可以相互替换,也就是fx(lhs) != fx(rhs)。这种情况在基础类型中并没有,但是它常常发生在用户自定义类中,比如一个大小写不敏感的字符串类:

#include <compare>
#include <string>
#include <iostream>int ci_compare(const char* s1, const char* s2)
{while (tolower(*s1) == tolower(*s2++)) {if (*s1++ == '\0') {return 0;}}return tolower(*s1) - tolower(*--s2);
}class CIString {
public:CIString(const char *s) : str_(s) {}std::weak_ordering operator<=>(const CIString& b) const {return ci_compare(str_.c_str(), b.str_.c_str()) <=> 0;//strong_ordering返回为weak_ordering类型,实际上发生了类型转换}
private:std::string str_;
};int main(int argc,char* argv[])
{auto res = 'a'<=>'a';std::cout << typeid(res).name()<<std::endl;      //strong_orderingstd::cout << typeid(res<=>0).name()<<std::endl;  //strong_orderingstd::cout << typeid( ((std::weak_ordering)res) ).name()<<std::endl; //strong_ordering可以转为weak_orderingCIString s1{ "HELLO" }, s2{"hello"};std::cout << std::boolalpha << (s1 <=> s2 == 0)<<std::endl; // 输出为truestd::cout << typeid(s1<=>s2).name()<<std::endl;  //weak_orderingreturn 0;
}

 

以上代码实现了一个简单的大小写不敏感的字符串类,它对于s1和s2的比较结果是std::weak_ordering::equivalent,表示两个操作数是等价的,但是它们不是相等的也不能相互替换。当std::weak_ordering和std::strong_ ordering同时出现在基类和数据成员的类型中时,该类型的三向比较结果是std::weak_ordering,例如:

#include <compare>
#include <string>
#include <iostream>int ci_compare(const char* s1, const char* s2)
{while (tolower(*s1) == tolower(*s2++)) {if (*s1++ == '\0') {return 0;}}return tolower(*s1) - tolower(*--s2);
}class CIString {
public:CIString(const char *s) : str_(s) {}std::weak_ordering operator<=>(const CIString& b) const {return ci_compare(str_.c_str(), b.str_.c_str()) <=> 0;//strong_ordering返回为weak_ordering类型,实际上发生了类型转换}
private:std::string str_;
};struct B{int a=0;long b=0;std::strong_ordering operator <=> (const B&) const = default;
};struct D : B{CIString c{""};auto operator <=> (const D&) const = default;
};int main(int argc,char* argv[])
{D w1,w2;std::cout << std::boolalpha << (w1 <=> w2 == 0)<<std::endl; // 输出为truestd::cout << std::boolalpha << (w1 <=> w2 == std::weak_ordering::equivalent)<<std::endl; // 输出为truestd::cout << typeid(w1<=>w2).name()<<std::endl;  //weak_orderingreturn 0;
}

用MSVC编译运行上面这段代码会输出class std::weak_ordering,因为D中的数据成员CIString的三向比较结果为std::weak_ordering。请注意,如果显式声明默认三向比较运算符函数为std::strong_ordering operator <=> (const D&) const = default;,那么一定会遭遇到一个编译错误。


2.3 std::partial_ordering

std::partial_ordering类型有4种比较结果,分别为std::partial_ ordering::less、std::partial_ordering::equivalent、std::partial_ ordering::greater以及std::partial_ordering::unordered。std:: partial_ordering约束力比std::weak_ordering更弱,它可以接受当lhs == rhs时rhs和lhs不能相互替换,同时它还能给出第四个结果std::partial_ordering::unordered,表示进行比较的两个操作数没有关系。比如基础类型中的浮点数:

#include <iostream>int main(int argc,char* argv[])
{std::cout << typeid(decltype(7.7 <=> 11.1)).name();//输出partial_orderingreturn 0;
}

用MSVC编译运行以上代码会输出class std::partial_ordering。之所以会输出class std::partial_ordering而不是std::strong_ordering,是因为浮点的集合中存在一个特殊的NaN,它和其他浮点数值是没关系的:

#include <iostream>int main(int argc,char* argv[])
{std::cout<<std::boolalpha<< ((0.0/0.0 <=> 1.0) == std::partial_ordering::unordered);//输出truereturn 0;
}

这段代码编译输出的结果为true。

当std::weak_ordering和std:: partial_ordering同时出现在基类和数据成员的类型中时,该类型的三向比较结果是std::partial_ordering,例如:

#include <compare>
#include <string>
#include <iostream>int ci_compare(const char* s1, const char* s2)
{while (tolower(*s1) == tolower(*s2++)) {if (*s1++ == '\0') {return 0;}}return tolower(*s1) - tolower(*--s2);
}class CIString {
public:CIString(const char *s) : str_(s) {}std::weak_ordering operator<=>(const CIString& b) const {return ci_compare(str_.c_str(), b.str_.c_str()) <=> 0;//strong_ordering返回为weak_ordering类型,实际上发生了类型转换}
private:std::string str_;
};struct B{int a=0;long b=0;std::strong_ordering operator <=> (const B&) const = default;
};struct D : B{CIString c{""};float u=0.0;auto operator <=> (const D&) const = default;
};int main(int argc,char* argv[])
{D w1,w2;std::cout << std::boolalpha << (w1 <=> w2 == 0)<<std::endl; // 输出为truestd::cout << std::boolalpha << (w1 <=> w2 == std::partial_ordering::equivalent)<<std::endl; // 输出为truestd::cout << typeid(w1<=>w2).name()<<std::endl;  //partial_orderingreturn 0;
}


用MSVC编译运行以上代码会输出class std::partial_ordering,因为D中的数据成员u的三向比较结果为std::partial_ordering,同样,显式声明为其他返回类型也会让编译器报错。在C++20的标准库中有一个模板元函数std::common_comparison_category,它可以帮助我们在一个类型合集中判断出最终三向比较的结果类型,当类型合集中存在不支持三向比较的类型时,该模板元函数返回void。

再次强调一下,std::strong_ordering、std::weak_ordering和`std::partial_ordering`只能与`0`和类型自身比较。深究其原因,是这3个类只实现了参数类型为自身类型和`nullptr_t的比较运算符函数。


三、对基础类型的支持

  • 3.1.对两个算术类型的操作数进行一般算术转换,然后进行比较。其中整型的比较结果为std::strong_ordering,浮点型的比较结果为std::partial_ordering。例如7 <=> 11.1中,整型7会转换为浮点类型,然后再进行比较,最终结果为std::partial_ordering类型。
  • 3.2.对于无作用域枚举类型和整型操作数,枚举类型会转换为整型再进行比较,无作用域枚举类型无法与浮点类型比较:
enum color {red
};auto r = red <=> 11;   //编译成功
auto r = red <=> 11.1; //编译失败
  • 3.3.对两个相同枚举类型的操作数比较结果,如果枚举类型不同,则无法编译。
  • 3.4.对于其中一个操作数为bool类型的情况,另一个操作数必须也是bool类型,否则无法编译。比较结果为std::strong_ordering。
  • 3.5.不支持作比较的两个操作数为数组的情况,会导致编译出错,例如:
int arr1[5];
int arr2[5];
auto r = arr1 <=> arr2; // 编译失败
  • 3.6.对于其中一个操作数为指针类型的情况,需要另一个操作数是同样类型的指针,或者是可以转换为相同类型的指针,比如数组到指针的转换、派生类指针到基类指针的转换等,最终比较结果为std::strong_ordering:
char arr1[5];
char arr2[5];
char* ptr = arr2;
auto r = ptr <=> arr1;

上面的代码可以编译成功,若将代码中的arr1改写为int arr1[5],则无法编译,因为int [5]无法转换为char *。如果将char * ptr = arr2;修改为void * ptr = arr2;,代码就可以编译成功了。


四、自动生成的比较运算符函数

4.1 std::rel_ops的作用

标准库中提供了一个名为std::rel_ops的命名空间,在用户自定义类型已经提供了==运算符函数和<运算符函数的情况下,帮助用户实现其他4种运算符函数,包括!=、>、<=和>=。

 代码:

#include <compare>
#include <string>
#include <iostream>
#include <utility>int ci_compare(const char* s1, const char* s2)
{while (tolower(*s1) == tolower(*s2++)) {if (*s1++ == '\0') {return 0;}}return tolower(*s1) - tolower(*--s2);
}class CIString2 {
public:CIString2(const char* s) : str_(s) {}bool operator < (const CIString2& b) const {return ci_compare(str_.c_str(), b.str_.c_str()) < 0;}bool operator== (const CIString2& b) const {return ci_compare(str_.c_str(), b.str_.c_str()) == 0;}
private:std::string str_;
};int main(int argc,char* argv[])
{using namespace std::rel_ops;CIString2 s1( "hello" ), s2( "world" );bool r = true;r = s1 == s2;std::cout<<std::boolalpha<<r<<std::endl;r = s1 != s2;std::cout<<std::boolalpha<<r<<std::endl;r = s1 > s2;std::cout<<std::boolalpha<<r<<std::endl;r = s1 >= s2;std::cout<<std::boolalpha<<r<<std::endl;r = s1 < s2;std::cout<<std::boolalpha<<r<<std::endl;r = s1 <= s2;std::cout<<std::boolalpha<<r<<std::endl;return 0;
}

输出:

false
true
false
false
true
true

4.2 使用<=>

不过因为C++20标准有了三向比较运算符的关系,所以不推荐上面这种做法了。C++20标准规定,如果用户为自定义类型声明了三向比较运算符,那么编译器会为其自动生成<、>、<=和>=这4种运算符函数。对于CIString我们可以直接使用这4种运算符函数:

#include <compare>
#include <string>
#include <iostream>
#include <utility>int ci_compare(const char* s1, const char* s2)
{while (tolower(*s1) == tolower(*s2++)) {if (*s1++ == '\0') {return 0;}}return tolower(*s1) - tolower(*--s2);
}class CIString {
public:CIString(const char *s) : str_(s) {}//   bool operator== (const CIString& b) const {
//       return ci_compare(str_.c_str(), b.str_.c_str()) == 0;
//   }std::weak_ordering operator<=>(const CIString& b) const {return ci_compare(str_.c_str(), b.str_.c_str()) <=> 0;//strong_ordering返回为weak_ordering类型,实际上发生了类型转换}
private:std::string str_;
};int main(int argc,char* argv[])
{CIString s1( "hello" ), s2( "world" );bool r = true;// r = s1 == s2;// std::cout<<std::boolalpha<<r<<std::endl;// r = s1 != s2;// std::cout<<std::boolalpha<<r<<std::endl;r = s1 > s2;std::cout<<std::boolalpha<<r<<std::endl;r = s1 >= s2;std::cout<<std::boolalpha<<r<<std::endl;r = s1 < s2;std::cout<<std::boolalpha<<r<<std::endl;r = s1 <= s2;std::cout<<std::boolalpha<<r<<std::endl;return 0;
}

输出

false
false
true
true

那么这里就会产生一个疑问,很明显三向比较运算符能表达两个操作数是相等或者等价的含义,为什么标准只允许自动生成4种运算符函数,却不能自动生成==和=!这两个运算符函数呢?实际上这里存在一个严重的性能问题。在C++20标准拟定三向比较的早期,是允许通过三向比较自动生成6个比较运算符函数的,而三向比较的结果类型也不是3种而是5种,多出来的两种分别是std::strong_ equality和std::weak_equality。但是在提案文档p1190中提出了一个严重的性能问题。简单来说,假设有一个结构体:

struct S {std::vector<std::string> names;auto operator<=>(const S &) const = default;
};

它的三向比较运算符的默认实现这样的:

template<typename T>
std::strong_ordering operator<=>(const std::vector<T>& lhs, const std::vector<T> & rhs) 
{size_t min_size = min(lhs.size(), rhs.size());for (size_t i = 0; i != min_size; ++i) {if (auto const cmp = std::compare_3way(lhs[i], rhs[i]); cmp != 0) {return cmp;}}return lhs.size() <=> rhs.size();
}


这个实现对于<和>这样的运算符函数没有问题,因为需要比较容器中的每个元素。但是==运算符就显得十分低效,对于==运算符高效的做法是先比较容器中的元素数量是否相等,如果元素数量不同,则直接返回false:

template<typename T>
bool operator==(const std::vector<T>& lhs, const std::vector<T>& rhs)
{const size_t size = lhs.size();if (size != rhs.size()) {return false;}for (size_t i = 0; i != size; ++i) {if (lhs[i] != rhs[i]) {return false;}}return true;
}

想象一下,如果标准允许用三向比较的算法自动生成==运算符函数会发生什么事情,很多旧代码升级编译环境后会发现运行效率下降了,尤其是在容器中元素数量众多且每个元素数据量庞大的情况下。很少有程序员会注意到三向比较算法的细节,导致这个性能问题难以排查。基于这种考虑,C++委员会修改了原来的三向比较提案,规定声明三向比较运算符函数只能够自动生成4种比较运算符函数。由于不需要负责判断是否相等,因此std::strong_equality和std::weak_ equality也退出了历史舞台。对于==和!=两种比较运算符函数,只需要多声明一个==运算符函数,!=运算符函数会根据前者自动生成:

#include <compare>
#include <string>
#include <iostream>
#include <utility>int ci_compare(const char* s1, const char* s2)
{while (tolower(*s1) == tolower(*s2++)) {if (*s1++ == '\0') {return 0;}}return tolower(*s1) - tolower(*--s2);
}class CIString {
public:CIString(const char *s) : str_(s) {}bool operator== (const CIString& b) const {return ci_compare(str_.c_str(), b.str_.c_str()) == 0;}std::weak_ordering operator<=>(const CIString& b) const {return ci_compare(str_.c_str(), b.str_.c_str()) <=> 0;//strong_ordering返回为weak_ordering类型,实际上发生了类型转换}
private:std::string str_;
};int main(int argc,char* argv[])
{CIString s1( "hello" ), s2( "world" );bool r = true;r = s1 == s2;std::cout<<std::boolalpha<<r<<std::endl;r = s1 != s2;std::cout<<std::boolalpha<<r<<std::endl;r = s1 > s2;std::cout<<std::boolalpha<<r<<std::endl;r = s1 >= s2;std::cout<<std::boolalpha<<r<<std::endl;r = s1 < s2;std::cout<<std::boolalpha<<r<<std::endl;r = s1 <= s2;std::cout<<std::boolalpha<<r<<std::endl;return 0;
}
false
true
false
false
true
true

五、兼容他旧代码

现在C++20标准已经推荐使用<=>和==运算符自动生成其他比较运算符函数,而使用<、==以及std::rel_ops生成其他比较运算符函数则会因为std::rel_ops已经不被推荐使用而被编译器警告。那么对于老代码,我们是否需要去实现一套<=>和==运算符函数呢?其实大可不必,C++委员会在裁决这项修改的时候已经考虑到老代码的维护成本,所以做了兼容性处理,即在用户自定义类型中,实现了<、==运算符函数的数据成员类型,在该类型的三向比较中将自动生成合适的比较代码。比如:

#include <iostream>struct Legacy {int n=0;bool operator==(const Legacy& rhs) const{return n == rhs.n;}bool operator<(const Legacy& rhs) const{return n < rhs.n;}
};struct TreeWay {Legacy m;std::strong_ordering operator<=>(const TreeWay &) const = default;
};int main(int argc,char* argv[])
{TreeWay t1, t2;bool r = t1 < t2;std::cout<<std::boolalpha<<r<<std::endl;return 0;
}

在上面的代码中,结构体TreeWay的三向比较操作会调用结构体Legacy中的<和==运算符来完成,其代码类似于:

struct TreeWay {Legacy m;std::strong_ordering operator<=>(const TreeWay& rhs) const {if (m < rhs.m) return std::strong_ordering::less;if (m == rhs.m) return std::strong_ordering::equal;return std::strong_ordering::greater;}
};

需要注意的是,这里operator<=>必须显式声明返回类型为std::strong_ ordering,使用auto是无法通过编译的。

相关文章:

  • 保姆教程-----安装MySQL全过程
  • DiT中的 Adaptive Layer Normalization (adaLN) 讲解
  • 【Android构建系统】如何在Camera Hal的Android.bp中选择性引用某个模块
  • 使用哈希表封装myunordered_set和myunordered_map
  • leetcode:58. 最后一个单词的长度(python3解法)
  • Centos7 中 Docker运行配置Apache
  • 【Shell的基本操作】
  • 第九天——贪心算法——非递减数组
  • Promise.all 详解
  • ch10 题目参考思路
  • 突围“百机大战”,云轴科技ZStack智塔获IDC中国AI大模型一体机推荐品牌
  • 文章记单词 | 第87篇(六级)
  • Android App CAN通信测试
  • elementUI源码学习
  • OpenCV CUDA模块中矩阵操作------归一化与变换操作
  • 反射机制详细说明
  • 2025年渗透测试面试题总结-安恒[实习]安全服务工程师(题目+回答)
  • 遥感图像露天矿区检测数据集VOC+YOLO格式1542张1类别
  • 智能裂变引擎 商业增长利器 —— 专业推客系统耀世而来
  • Sprnig MVC 如何统一异常处理 (Exception Handling)?
  • 摄影师|伊莎贝尔·穆尼奥斯:沿着身体进行文化溯源
  • 央视起底“字画竞拍”网络传销案:涉案44亿元,受害者众多
  • 金砖国家召开经贸联络组司局级特别会议,呼吁共同抵制单边主义和贸易保护主义
  • 脑血管支架:救命神器还是定时炸弹?听听医生的大实话
  • 食用城市|食饭识人
  • 马上评丨75万采购300元设备,仅仅终止采购还不够