什么是Ruby <=>(宇宙飞船)运营商?运营商是否通过其他语言实施?
当我试图了解C++运算符时,我偶然发现cppreference.com上的一个奇怪的比较运算符,*在一个如下所示的表中:

"好吧,如果这些是C++中常见的操作符,我会更好地学习它们",我想.但我所有试图阐明这个谜团的尝试都没有成功.即使在这里,在Stack Overflow上我的搜索也没有运气.
如果有的话,这个运营商到底做了什么?
*与此同时,cppreference.com更新了该页面,现在包含有关<=>运营商的信息.
PHP 7将于今年11月推出,它将引入Spaceship(<=>)运营商.它是什么以及它是如何工作的?
关于PHP运算符的一般参考问题,这个问题已经有了答案.
我正在研究由以前的开发人员编写的代码,并在查询中说,
WHERE p.name <=> NULL
Run Code Online (Sandbox Code Playgroud)
<=>这个查询意味着什么?它等于=什么?或者是语法错误?
但它没有显示任何错误或例外.我已经知道了<>= !=在MySQL中.
我一直将其精简为仅使用Boost Operators:
#include <boost/operators.hpp>
struct F : boost::totally_ordered1<F, boost::totally_ordered2<F, int>> {
/*implicit*/ F(int t_) : t(t_) {}
bool operator==(F const& o) const { return t == o.t; }
bool operator< (F const& o) const { return t < o.t; }
private: int t;
};
int main() {
#pragma GCC diagnostic ignored "-Wunused"
F { 42 } == F{ 42 }; // OKAY
42 == F{42}; // C++17 OK, C++20 infinite …Run Code Online (Sandbox Code Playgroud)#include <compare>
struct A
{
int n;
auto operator<=>(A const& other) const
{
if (n < other.n)
{
return std::strong_ordering::less;
}
else if (n > other.n)
{
return std::strong_ordering::greater;
}
else
{
return std::strong_ordering::equal;
}
}
// compile error if the following code is commented out.
// bool operator==(A const& other) const
// { return n == other.n; }
};
int main()
{
A{} == A{};
}
Run Code Online (Sandbox Code Playgroud)
看在线演示
为什么我必须 operator == 在 足够的时候提供operator <=> ?
c++ language-design language-lawyer spaceship-operator c++20
<=>在C++ 20中有一个新的比较运算符.但是我认为在大多数情况下,简单的减法效果很好:
int my_strcmp(const char *a, const char *b) {
while (*a == *b && *a != 0 && *b != 0) {
a++, b++;
}
// Version 1
return *a - *b;
// Version 2
return *a <=> *b;
// Version 3
return ((*a > *b) - (*a < *b));
}
Run Code Online (Sandbox Code Playgroud)
它们具有相同的效果.我无法理解其中的差异.
令我惊讶的是,我遇到了另一个障碍,例如C++20 行为用相等运算符破坏了现有代码?.
考虑一个简单的不区分大小写的键类型,用于例如std::setor std::map:
// Represents case insensitive keys
struct CiKey : std::string {
using std::string::string;
using std::string::operator=;
bool operator<(CiKey const& other) const {
return boost::ilexicographical_compare(*this, other);
}
};
Run Code Online (Sandbox Code Playgroud)
简单的测试:
using KeySet = std::set<CiKey>;
using Mapping = std::pair<CiKey, int>; // Same with std::tuple
using Mappings = std::set<Mapping>;
int main()
{
KeySet keys { "one", "two", "ONE", "three" };
Mappings mappings {
{ "one", 1 }, { "two", 2 }, { "ONE", 1 }, { "three", …Run Code Online (Sandbox Code Playgroud) 我<=>在C ++ 20中使用新的宇宙飞船运算符遇到一种奇怪的行为。我正在将Visual Studio 2019编译器与一起使用/std:c++latest。
这段代码可以正常编译:
#include <compare>
struct X
{
int Dummy = 0;
auto operator<=>(const X&) const = default; // Default implementation
};
int main()
{
X a, b;
a == b; // OK!
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我将X更改为:
struct X
{
int Dummy = 0;
auto operator<=>(const X& other) const
{
return Dummy <=> other.Dummy;
}
};
Run Code Online (Sandbox Code Playgroud)
我收到以下编译器错误:
error C2676: binary '==': 'X' does not define this operator or a conversion to …
Herb Sutter在他的"宇宙飞船"运营商提案(第12.2节,第12页底部)中说:
基于所有内容
<=>及其返回类型:此模型具有主要优势,与先前的C++提议和其他语言的功能相比,此提案有一些独特之处:[...]
(6)效率,包括最终实现用于比较的零开销抽象:绝大多数比较总是单通道.唯一的例外是生成,
<=并且>=在支持部分排序和相等的类型的情况下.对于<单通是基本实现了零开销的原则,以避免重复平等的比较,如struct Employee { string name; /*more members*/ };在使用的struct Outer { Employeee; /*more members*/ };-今天的对比违反零开销抽象,因为operator<在Outer执行冗余相等比较,因为它执行if (e != that.e) return e < that.e;横贯平等前缀e.name两次(如果名称相同,则遍历Employee两次其他成员的相等前缀),这通常无法优化.正如Kamiński所指出的,零开销抽象是C++的支柱,并且首次实现它的比较是这种设计的一个重要优势<=>.
但他给出了这个例子(第1.4.5节,第6页):
class PersonInFamilyTree { // ...
public:
std::partial_ordering operator<=>(const PersonInFamilyTree& that) const {
if (this->is_the_same_person_as ( that)) return partial_ordering::equivalent;
if (this->is_transitive_child_of( that)) return partial_ordering::less;
if (that. …Run Code Online (Sandbox Code Playgroud)