C++20 three way comparison operator: Part 6
In the fifth part of the tutorial series, I demonstrated the usage of std::strong_ordering
comparison category as a return type for the three way operator. In the process, I clarified the meanings of terminologies like value of an object, salient properties and substitutablity. In the current part I’ll explain the usage of second comparison category, i.e., std::weak_ordering
defined in the C++20’s new compare
header.
Weak ordering allows to objects to be allowed for equivalence instead of an exact equality. In the fourth tutorial, I only discussed about equivalence and did not define objectively.
std::strong_ordering
has four valid values: (1) less
(2) equivalent
(3) equal
and (4) greater
. Similarly std::weak_ordering
has three valid values viz (1) less
(2) equivalent
(3) greater
. The only difference in comparison to std::strong_ordering
is that it allows for only a weak equality instead of the canonical equality relationship we’re used to in everyday programming.
1. When is a type only weakly comparable?
The answer to this question is buried in the second of the three rules for std::weak_ordering
in the cpprefernence page:
does not imply substitutability: if a is equivalent to b, f(a) may not be equivalent to f(b), where f denotes a…