在 g++ 10 中,我尝试使用三向比较,仅用于实验。
我读到不再需要其他运算符(== 除外)。
但即使我可以使用运算符(它在编译器上实现),它也不会取代(或暗示)!=。
因此,以下代码不起作用。
#include<iostream>
using namespace std;
struct A
{
struct Iterator
{
size_t index;
size_t operator*() { return index + 1000; }
//bool operator!=(const Iterator &a) const { return index != a.index; }
auto operator<=>(const Iterator &a) const { return index <=> a.index; }
Iterator &operator++() { ++index; return *this; }
};
Iterator begin() { return Iterator{0}; }
Iterator end() { return Iterator{5}; }
};
int main()
{
A a;
auto result = a.begin() <=> a.end();
for (auto b : a)
cout << b << "\n";
cout << (a.begin() != a.end()) << "\n";
return 0;
}
我在这里想念什么?