C++20草案中的宇宙飞船运算符(<=>,spaceship operator)
2021-07-02 20:04
标签:宇宙 设计 doc rhs nan lang 类型 equal https 宇宙飞船运算符(hh)形式如 如果a b是浮点值,就返回的是 标准还有更多规定,比如a与b是指针,a与b是枚举等,详细内容请参见 最后, 也就是说,返回的是 http://open-std.org/JTC1/SC22/WG21/docs/papers/2017/p0515r0.pdf C++20草案中的宇宙飞船运算符(,spaceship operator) 标签:宇宙 设计 doc rhs nan lang 类型 equal https 原文地址:https://www.cnblogs.com/racaljk/p/9629599.htmlC++20草案中的宇宙飞船运算符(,spaceship operator)
Herb Sutter提议的新三路运算符已经被合入C++20草案中。lhsrhs。
比如a与b是整型,那么ab返回std::strong_ordering类型的纯右值(prvalue,不能取地址那种):
a,
(ab)返回std::strong_ordering::lessa>b,(ab)返回std::strong_ordering::greater
a与b全等/相等,(ab)返回std::strong_ordering::equal
std::partial_ordering::less/greater/equivalent或者std::partial_ordering::unordered(如果ab中有一个NaN)更多std::strong_ordering和std::partial_ordering的值定义如下:+--------------------------------------------------------------------+
| | Numeric values | Non-numeric |
| Category +-----------------------------------+ |
| | -1 | 0 | +1 | values |
+------------------+------+------------+---------------+-------------+
| strong_ordering | less | equal | greater | |
| weak_ordering | less | equivalent | greater | |
| partial_ordering | less | equivalent | greater | unordered |
| strong_equality | | equal | nonequal | |
| weak_equality | | equivalent | nonequivalent | |
+------------------+------+------------+---------------+-------------+0/1/-1,还记得strcmp(stra,strb)吧,设计理念之一正是基于这种函数,如果ab且a==3 && b==3,那么ab就等于0更多
https://en.cppreference.com/w/cpp/language/operator_comparison
文章标题:C++20草案中的宇宙飞船运算符(<=>,spaceship operator)
文章链接:http://soscw.com/index.php/essay/100934.html