Custom Comparison Logic

How can we implement custom comparison logic using the spaceship operator <=>?

Implementing custom comparison logic using the spaceship operator <=> in C++20 allows you to define how your custom types should be compared in a consistent and efficient manner. Here's a step-by-step guide to implementing this:

Step 1: Define the Custom Type

First, define your custom type. For example, let's create a Player class that compares players based on their scores and, if the scores are the same, by their names.

#include <compare>
#include <iostream>
#include <string>

class Player {
 public:
  Player(std::string name, int score)
    : Name{name}, Score{score} {}

  std::strong_ordering operator<=>(
    const Player& Other) const {
    if (auto cmp = Score <=> Other.Score; cmp != 0) {
      return cmp;
    }
    return Name <=> Other.Name;
  }

  bool operator==(const Player& Other) const {
    return Score == Other.Score && Name == Other.Name;
  }

  std::string Name;
  int Score;
};

int main() {
  Player Player1{"Alice", 10};
  Player Player2{"Bob", 10};
  Player Player3{"Alice", 20};

  if (Player1 < Player2) {
    std::cout << Player1.Name
      << " is ranked lower than "
      << Player2.Name << "\n";
  }
  if (Player1 > Player3) {
    std::cout << Player1.Name
      << " is ranked higher than "
      << Player3.Name << "\n";
  }
  if (Player1 == Player1) {
    std::cout << Player1.Name
      << " is equal to "
      << Player1.Name << "\n";
  }
}
Alice is ranked lower than Bob
Alice is equal to Alice

Step 2: Implement Custom Comparison Logic

In the operator<=>, we first compare the scores. If they are not equal, we return the result of that comparison.

If the scores are equal, we then compare the names. This secondary comparison ensures that players with the same score are ordered by their names.

Step 3: Use the Custom Comparison

You can now use all the comparison operators with your custom type. The <=> operator handles the ordering, and the == operator ensures that equality is checked correctly.

In this example, the comparison logic considers both the Score and Name fields of the Player class, demonstrating how you can implement and use custom comparison logic effectively with the spaceship operator in C++20.

The Spaceship Operator and Expression Rewriting

A guide to simplifying our comparison operators using C++20 features

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Defining the == Operator
Why do we need to explicitly define the == operator if we already have <=>?
Equality vs. Equivalence
What are the differences between std::strong_ordering::equal and std::strong_ordering::equivalent?
Benefits of the Spaceship Operator
What are the benefits of using the spaceship operator <=> over traditional comparison operators?
Using <=> for Sorting
Can <=> be used for sorting algorithms in C++20?
Niche Cases for Three-Way Comparison
How does the C++20 standard handle types that do not naturally fit into the three-way comparison model?
Real-World Examples of <=>
What are some practical examples of using <=> in real-world applications?
Ask Your Own Question
Temporarily unavailable while we roll out updates. Back in a few days!