Comparison operators
Compares the arguments.
# Declarations
bool operator<(L, R);
bool operator>(L, R);
bool operator<=(L, R);
bool operator>=(L, R);
bool operator==(L, R);
bool operator!=(L, R);
# Notes
Because comparison operators group left-to-right, the expression a < b < c is parsed (a < b) < c, and not a < (b < c) or (a < b) && (b < c).
A common requirement for user-defined operator< is strict weak ordering. In particular, this is required by the standard algorithms and containers that work with Compare types: std::sort, std::max_element, std::map, etc.
The comparison result of pointers to different non-static data members of the same class implies that non-static data membersin each of the three member access modes(until C++23) are positioned in memory in order of declaration.
Although the results of comparing pointers of random origin (e.g. not all pointing to members of the same array) is unspecified, many implementations provide strict total ordering of pointers, e.g. if they are implemented as addresses within continuous virtual address space. Those implementations that do not (e.g. where not all bits of the pointer are part of a memory address and have to be ignored for comparison, or an additional calculation is required or otherwise pointer and integer is not a 1 to 1 relationship), provide a specialization of std::less for pointers that has that guarantee. This makes it possible to use all pointers of random origin as keys in standard associative containers such as std::set or std::map.
For the types that are both EqualityComparable and LessThanComparable, the C++ standard library makes a distinction between equality, which is the value of the expression a == b and equivalence, which is the value of the expression !(a < b) && !(b < a).
Comparison between pointers and null pointer constants was removed by the resolution of CWG issue 583 included in N3624:
Three-way comparison can be automatically generated for class types, see default comparisons.
If both of the operands are arrays, three-way comparison is ill-formed.
# Defect reports
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| CWG 583(N3624) | C++98 | all six comparison operators could be used tocompare a pointer with a null pointer constant | only equality operatorsare allowed |
| CWG 661 | C++98 | the actual semantics of arithmetic comparisons (e.g.whether 1 < 2 yields true or false) were unspecified | specification added |
| CWG 879 | C++98 | pointers to function types and pointersto void did not have built-in comparisons | added comparisonspecification for these pointers |
| CWG 1596 | C++98 | non-array objects were considered to belong to arrays withone element only for the purpose of pointer arithmetic | the rule is alsoapplied to comparison |
| CWG 1598 | C++98 | two pointers to members of classes that are different andneither is the base class of the other did not compare equaleven if the offsets of the pointed members can be the same | the result isunspecifiedin this case |
| CWG 1858 | C++98 | it was not clear whether two pointers to membersthat refer to different members of the same unioncompare equal as if they refer to the same member | they compareequal in this case |
| CWG 2419 | C++98 | a pointer to non-array object was only treated as apointer to the first element of an array with size 1in pointer comparison if the pointer is obtained by & | applies to all pointersto non-array objects |
| CWG 2526 | C++98 | the definition of relational comparison (>, >=, < and <=) ofpointers to void and function pointers were removed by N3624 | restored |
| CWG 2796 | C++17 | function pointer conversions were not performed on the convertedpointer operands during built-in pointer relational comparisons | performs theseconversions in this case |