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

DRApplied toBehavior as publishedCorrect behavior
CWG 583(N3624)C++98all six comparison operators could be used tocompare a pointer with a null pointer constantonly equality operatorsare allowed
CWG 661C++98the actual semantics of arithmetic comparisons (e.g.whether 1 < 2 yields true or false) were unspecifiedspecification added
CWG 879C++98pointers to function types and pointersto void did not have built-in comparisonsadded comparisonspecification for these pointers
CWG 1596C++98non-array objects were considered to belong to arrays withone element only for the purpose of pointer arithmeticthe rule is alsoapplied to comparison
CWG 1598C++98two 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 samethe result isunspecifiedin this case
CWG 1858C++98it was not clear whether two pointers to membersthat refer to different members of the same unioncompare equal as if they refer to the same memberthey compareequal in this case
CWG 2419C++98a 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 2526C++98the definition of relational comparison (>, >=, < and <=) ofpointers to void and function pointers were removed by N3624restored
CWG 2796C++17function pointer conversions were not performed on the convertedpointer operands during built-in pointer relational comparisonsperforms theseconversions in this case

# See also