我遇到了一个奇怪的行为与新的宇宙飞船操作员 <=>
在 C + + 20。我使用的是带有 /std:c++latest
的 VisualStudio2019编译器。
正如所料,这段代码编译得很好:
#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;
}
但是,如果我将 X改为:
struct X
{
int Dummy = 0;
auto operator<=>(const X& other) const
{
return Dummy <=> other.Dummy;
}
};
我得到以下编译器错误:
error C2676: binary '==': 'X' does not define this operator or a conversion to a type acceptable to the predefined operator
我也在叮当声中尝试了这种方法,我得到了类似的行为。
我希望能够解释一下为什么默认实现正确地生成了 operator==
,但是自定义实现却不能。