C++20 three-way comparison operator: Part 4

Gajendra Gulgulia
10 min readJul 5, 2021
image ©: gajendra gulgulia

In the third part of the tutorial series, I uncovered the mechanics of operator <=> and explained in detail, how the compiler re-writes the comparison expression on a custom object with only operator<=> declared as default and how additionally it can make use of synthesized expression to reverse the operands during expression re-writing process. If you haven’t read the third part, I strongly encourage you to read it before reading this tutorial.

As promised at the end of the third part, in this part I’ll explain the theory behind the concrete return types of the C++20’s three way operator. Honestly speaking, I did not know what I was about to get into when I decided to write a tutorial about the return types of the spaceship operators. The logic behind each of the return types is mathematically quite involved and needs one to have knowledge of set theory and ordering. But to keep this tutorial easy to understand I’ll first lay out the theoretical ideas in very simple terms and go into details of the return types of operator<=> in the 5th part.

Lets again start by looking at a custom object say MyClass providing an operator<=> :

class MyClass{
private:
int num_;
char ch_;
public:
MyClass(int num, char ch): num_{num}, ch_{ch} {}
auto operator <=>(const MyClass& rhs) const =…

--

--