Moi*_*oia 6 c++ spaceship-operator c++20
spaceship operator 的定义旨在对排序进行强定义,但这是否会影响您的客户端代码的编写方式,或者只是如何定义您的类比较运算符?
由于在其他帖子中缺少真实世界的例子,我没有完全理解这部分。
关于宇宙飞船运营商的其他 SO 帖子:
你只需比较你一直做的方式:
a < b
Run Code Online (Sandbox Code Playgroud)
只是在幕后,该表达式的候选函数之一也会找到(a <=> b) < 0,如果该候选函数存在并且恰好是最佳可行候选函数,则调用它。
您通常不会<=>直接在“客户端代码”中使用,而是直接使用您想要的比较。
例如,给定:
struct X {
int i;
// this can be = default, just writing it out for clarity
strong_ordering operator<=>(X const& rhs) const { return i <=> rhs.i; }
};
Run Code Online (Sandbox Code Playgroud)
表达方式
X{42} < X{57};
Run Code Online (Sandbox Code Playgroud)
将评估为X{42} <=> X{57} < 0(没有<候选人,所以<=>非反转是最好的候选人)。X{42} <=> X{57}评估为42 <=> 57which is strong_ordering::less。然后< 0返回true。因此,初始表达式是true......正如预期的那样。
同一个操作符也直接给了我们 X{57} > X{42}, thatX{3} >= X{2}等。
的优势 <=>是您只需要编写一个运算符而不是四个运算符,该运算符通常比 编写容易得多<,您可以正确表达偏序和全序之间的区别,并且堆叠它通常性能更高(例如在以下情况下)喜欢string)。
此外,我们不必生活在这个怪异的世界里,每个人都假装这operator<是唯一存在的关系运算符。
<=>允许惰性方式也成为高效方式。您无需更改您的客户端代码。
当存在using std::rel_ops(或boost::ordered等)时,客户可能会看到性能优势。
一个例子
// old and busted
struct Person : boost::totally_ordered<Person>
{
std::string firstname;
std::string lastname
bool operator<(const Person & other)
{
return std::tie(firstname, lastname)
< std::tie(other.firstname, other.lastname);
}
}
// new hotness
struct Person
{
std::string firstname;
std::string lastname;
auto operator<=>(const Person &) = default;
}
int main()
{
Person person1 { "John", "Smith" };
Person person2 { "John", "Smith" };
std::cout << person2 <= person1 << std::endl;
}
Run Code Online (Sandbox Code Playgroud)