以下代码两次调用了operator <=>,参数顺序相反。为什么?
GCC 10.2和clang 12似乎都使用libstdc++-10,它的提供了operator <=>,因此似乎不是缺少标准库支持的情况,我的代码必须是错误的。如何修复它?
GCC 10.2和clang 12似乎都使用libstdc++-10,它的提供了operator <=>,因此似乎不是缺少标准库支持的情况,我的代码必须是错误的。如何修复它?
#include <tuple>
#include <compare>
#include <iostream>
struct X {
int i;
auto operator <=>(X const& other) const {
std::cout << this << " <=> " << &other << std::endl;
return i <=> other.i;
}
};
int main() {
std::tuple{X{42}} <=> std::tuple{X{42}};
}
a < b
,b < a
或相等?例如,return a < b ? -1 : b < a ? 1 : 0
,在三分之二的情况下也需要进行两次比较。 - Ted Lyngmo<=>
。运算符 <、> 等并没有被移除。这些从元组实现中移除了,只剩下<=>
。 - armagedescu