You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
C++20 introduces the spaceship operator (`<=>`) as a new way to write comparison functions that reduce boilerplate and help developers define clearer comparison semantics. Defining a three-way comparison operator will autogenerate the other comparison operator functions (i.e. `==`, `!=`, `<`, etc.).
248
+
249
+
Three orderings are introduced:
250
+
*`std::strong_ordering`: The strong ordering distinguishes between items being equal (identical and interchangeable). Provides `less`, `greater`, `equivalent`, and `equal` ordering. Examples of comparisons: searching for a specific value in a list, values of integers, case-sensitive strings.
251
+
*`std::weak_ordering`: The weak ordering distinguishes between items being equivalent (not identical, but can be interchangeable for the purposes of comparison). Provides `less`, `greater`, and `equivalent` ordering. Examples of comparisons: case-insensitive strings, sorting, comparing some but not all visible members of a class.
252
+
*`std::partial_ordering`: The partial ordering follows the same principle of weak ordering but includes the case when an ordering isn't possible. Provides `less`, `greater`, `equivalent`, and `unordered` ordering. Examples of comparisons: floating-point values (e.g. `NaN`).
253
+
254
+
A defaulted three-way comparison operator does a member-wise comparison:
std::erase_if(v, [](int n) { return n == 0; }); // v == {1, 2, 3}
633
678
```
634
679
680
+
### Three-way comparison helpers
681
+
Helper functions for giving names to comparison results:
682
+
```c++
683
+
std::is_eq(0 <=> 0); // == true
684
+
std::is_lteq(0 <=> 1); // == true
685
+
std::is_gt(0 <=> 1); // == false
686
+
```
687
+
688
+
See also: [three-way comparison](#three-way-comparison).
689
+
690
+
### std::lexicographical_compare_three_way
691
+
Lexicographically compares two ranges using three-way comparison and produces a result of the strongest applicable comparison category type.
692
+
```c++
693
+
std::vector a{0, 0, 0}, b{0, 0, 0}, c{1, 1, 1};
694
+
695
+
auto cmp_ab = std::lexicographical_compare_three_way(
696
+
a.begin(), a.end(), b.begin(), b.end());
697
+
std::is_eq(cmp_ab); // == true
698
+
699
+
auto cmp_ac = std::lexicographical_compare_three_way(
700
+
a.begin(), a.end(), c.begin(), c.end());
701
+
std::is_lt(cmp_ac); // == true
702
+
```
703
+
704
+
See also: [three-way comparison](#three-way-comparison), [three-way comparison helpers](#three-way-comparison-helpers).
705
+
635
706
## Acknowledgements
636
707
*[cppreference](http://en.cppreference.com/w/cpp) - especially useful for finding examples and documentation of new library features.
637
708
*[C++ Rvalue References Explained](http://web.archive.org/web/20240324121501/http://thbecker.net/articles/rvalue_references/section_01.html) - a great introduction I used to understand rvalue references, perfect forwarding, and move semantics.
C++17 includes the following new language features:
40
43
- [template argument deduction for class templates](#template-argument-deduction-for-class-templates)
@@ -344,6 +347,48 @@ concept C = requires(T x) {
344
347
```
345
348
See also: [concepts library](#concepts-library).
346
349
350
+
### Three-way comparison
351
+
C++20 introduces the spaceship operator (`<=>`) as a new way to write comparison functions that reduce boilerplate and help developers define clearer comparison semantics. Defining a three-way comparison operator will autogenerate the other comparison operator functions (i.e. `==`, `!=`, `<`, etc.).
352
+
353
+
Three orderings are introduced:
354
+
* `std::strong_ordering`: The strong ordering distinguishes between items being equal (identical and interchangeable). Provides `less`, `greater`, `equivalent`, and `equal` ordering. Examples of comparisons: searching for a specific value in a list, values of integers, case-sensitive strings.
355
+
* `std::weak_ordering`: The weak ordering distinguishes between items being equivalent (not identical, but can be interchangeable for the purposes of comparison). Provides `less`, `greater`, and `equivalent` ordering. Examples of comparisons: case-insensitive strings, sorting, comparing some but not all visible members of a class.
356
+
* `std::partial_ordering`: The partial ordering follows the same principle of weak ordering but includes the case when an ordering isn't possible. Provides `less`, `greater`, `equivalent`, and `unordered` ordering. Examples of comparisons: floating-point values (e.g. `NaN`).
357
+
358
+
A defaulted three-way comparison operator does a member-wise comparison:
0 commit comments