struct Triple {
uint64_t first;
uint64_t second;
uint64_t third;
// first option: hand-written member-wise comparison (for e.g. sorting, only operator< is needed)
bool operator<(const Triple& rhs) const {
if (first != rhs.first)
return first < rhs.first;
if (second != rhs.second)
return second < rhs.second;
return third < rhs.third;
}
// second option: compiler-generated spaceship operator
auto operator<=>(const WithSpaceship&) const = default;
};
Apparently, the generated code for the hand-written less-than comparison is better: the setb is only performed before the actual return, and the benchmarks show that it is indeed faster. I would expect the compiler-generated comparison to be at least as fast so that it doesn't discourage adopting operator<=>.
Consider the following simple example ([compiler explorer](https://godbolt.org/z/Tx6j7rcMo)):
```cpp
struct Triple {
uint64_t first;
uint64_t second;
uint64_t third;
// first option: hand-written member-wise comparison (for e.g. sorting, only operator< is needed)
bool operator<(const Triple& rhs) const {
if (first != rhs.first)
return first < rhs.first;
if (second != rhs.second)
return second < rhs.second;
return third < rhs.third;
}
// second option: compiler-generated spaceship operator
auto operator<=>(const WithSpaceship&) const = default;
};
```
Apparently, the generated code for the hand-written less-than comparison is better: the `setb` is only performed before the actual return, and the benchmarks show that it is indeed faster. I would expect the compiler-generated comparison to be at least as fast so that it doesn't discourage adopting `operator<=>`.
Consider the following simple example (compiler explorer):
Apparently, the generated code for the hand-written less-than comparison is better: the
setb
is only performed before the actual return, and the benchmarks show that it is indeed faster. I would expect the compiler-generated comparison to be at least as fast so that it doesn't discourage adoptingoperator<=>
.