Skip to content
Tutorials Center
My learning

Topics

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.

C++
struct Version {
    int major, minor;
    auto operator<=>(const Version&) const = default;
};

Try it

C++ Comparing built-in and library types

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.

C++ Defaulted and custom three-way comparison

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

C++
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: = default compares 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 declare operator==. Equality is separate because it can be faster than a full ordering (comparing two strings of different lengths).
  • Containers: std::set, std::map and std::sort need < (found via <=>). std::unordered_map needs == and a hash.
  • Floating point: never rely on == for computed values; compare with a tolerance. NaN == NaN is false.
  • C strings: const char* == const char* compares pointers. Use std::string, std::string_view or std::strcmp.
  • Safe integer comparison: std::cmp_less, std::cmp_equal and friends (C++20, <utility>) compare mixed signed/unsigned values correctly.
  • Reversed operands: since C++20, a == b also tries b == a, so you rarely need to write the mixed-type pair twice.
  • Added in: <=> and the ordering types in C++20.