在飞船操作员的最初设计中,
==
允许呼叫
<=>
,但由于效率问题,这一做法后来被禁止(
<=>
通常是一种低效的实现方式
).
operator<=>() = default
仍然定义为隐式定义
operator==
==
而不是
<
为方便起见,请各位成员注意。所以你想要的是:
struct A {
int n;
auto operator<=>(const A& rhs) const noexcept = default;
};
// ^^^ basically expands to vvv
struct B {
int n;
bool operator==(const B& rhs) const noexcept
{
return n == rhs.n;
}
auto operator<=>(const B& rhs) const noexcept
{
return n <=> rhs.n;
}
};
注意,您可以独立默认
操作员==
同时仍然提供用户定义的
operator<=>
struct B {
int n;
// note: return type for defaulted equality comparison operator
// must be 'bool', not 'auto'
bool operator==(const B& rhs) const noexcept = default;
auto operator<=>(const B& rhs) const noexcept
{
return n <=> rhs.n;
}
};