The Compiler-generated Spaceship Operator
Explore the compiler-generated three-way comparison operator in C++20, known as the spaceship operator. Learn how it performs lexicographical comparisons implicitly as constexpr and noexcept. Understand its use with built-in and user-defined types, and discover performance optimizations for equality operators on string-like and vector-like types.
We'll cover the following...
We'll cover the following...
The compiler-generated three-way comparison operator needs the header <compare>, is implicitly constexpr and noexcept, and it performs a lexicographical comparison.
You can even directly use the three-way comparison operator.
Direct use of the three-way comparison operator
The following program directly uses the spaceship operator.
The program uses the spaceship operator for int (line 11), for string (line 18 ...