C++ Reference ยท Operators
C++ comparison operators
Reference for C++ comparison operators ==, !=, <, <=, >, >= and the C++20 three-way operator <=>: ordering categories, defaulting, floating point and pointers.
The comparison operators ==, !=, <, <=, > and >= compare two values and produce a bool. C++20 added the three-way comparison operator <=> (nicknamed "spaceship"), which returns an object that says whether the left side is less, equal or greater.
For your own types you rarely need to write all six operators any more: declare operator<=> as = default and the compiler generates a lexicographic comparison of the members, plus the relational operators. Add a defaulted operator== (or let <=> default imply it) and equality works too.
struct Version { int major, minor; auto operator<=>(const Version&) const = default; };
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
a == b a != b a < b a <= b a > b a >= b a <=> b auto operator<=>(const T&) const = default; bool operator==(const T&) const = default;
| Name | Description |
|---|---|
==, != |
Equality and inequality. a != b is rewritten as !(a == b) in C++20. |
<, <=, >, >= |
Relational operators. With <=> available they are rewritten as (a <=> b) < 0, and so on. |
<=> |
Three-way comparison. Returns std::strong_ordering, std::weak_ordering or std::partial_ordering. Needs <compare> when you name the result types. |
Returns: bool for the six operators; an ordering object for <=>. Integers and pointers give strong_ordering; floating-point types give partial_ordering because NaN is unordered.
Errors / undefined behavior: relational comparison of pointers that do not point into the same array has an unspecified result (use std::less for a total order). Comparing a signed with an unsigned integer converts the signed value and can give surprising results.
Usage notes
- Defaulting:
= defaultcompares base classes and members in declaration order. It works only if every member is comparable. A defaulted<=>also declares a defaulted==. - Custom
<=>does not give==: if you write<=>yourself, also declareoperator==. Equality is separate because it can be faster than a full ordering (comparing two strings of different lengths). - Containers:
std::set,std::mapandstd::sortneed<(found via<=>).std::unordered_mapneeds==and a hash. - Floating point: never rely on
==for computed values; compare with a tolerance.NaN == NaNis false. - C strings:
const char* == const char*compares pointers. Usestd::string,std::string_vieworstd::strcmp. - Safe integer comparison:
std::cmp_less,std::cmp_equaland friends (C++20,<utility>) compare mixed signed/unsigned values correctly. - Reversed operands: since C++20,
a == balso triesb == a, so you rarely need to write the mixed-type pair twice. - Added in:
<=>and the ordering types in C++20.