Namespaces
Variants
Views
Actions

Default comparisons(since C++20)

From cppreference.com
< cpp‎ | language
 
 
 
 

Provides a way to request the compiler to generate consistent relational operators for a class.

In brief, a class that defines operator<=> automatically gets compiler-generated operators ==, !=, <, <=, >, and >=. A class can define operator<=> as defaulted, in which case the compiler will also generate the code for that operator.

class Point {
 int x;
 int y;
public:
 auto operator<=>(const Point&) const = default;
 // ... non-comparison functions ...
};
 
Point pt1, pt2;
if (pt1 == pt2) { /*...*/ } // ok
set<Point> s; // ok
s.insert(pt1); // ok
if (pt1 <= pt2) { /*...*/ } // ok, makes only a single call to <=>