The Compiler-generated Spaceship Operator
Learn how a three-way comparison operator works.
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.
Press + to interact
#include <compare>#include <iostream>#include <string>#include <vector>int main() {std::cout << '\n';int a(2011);int b(2014);auto res = a <=> b;if(res < 0) std::cout << "a < b" << '\n';else if (res == 0) std::cout << "a == b" << '\n';else if (res > 0) std::cout << "a > b" << '\n';std::string str1("2014");std::string str2("2011");auto res2 = str1 <=> str2;if (res2 < 0) std::cout << "str1 < str2" << '\n';else if (res2 == 0) std::cout << "str1 == str2" << '\n';else if (res2 > 0) std::cout << "str1 > str2" << '\n';std::vector<int> vec1{1, 2, 3};std::vector<int> vec2{1, 2, 3};auto res3 = vec1 <=> vec2;if (res3 < 0) std::cout << "vec1 < vec2" << '\n';else if (res3 == 0) std::cout << "vec1 == vec2" << '\n';else if (res3 > 0) std::cout << "vec1 > vec2" << '\n';std::cout << '\n';}
The program uses the spaceship operator for int
(line 11), for string
(line 18), and vector
...