Skip to main content
added 462 characters in body
Source Link
Tony Delroy
  • 106.7k
  • 16
  • 188
  • 265

As other people have saidC++20 introduced default comparisons, aka the "spaceship" operator<=>, which allows you need to implement a comparison function yourselfrequest compiler-generated </<=/==/!=/>=/ and/or > operators with the obvious/naive(?) implementation...

auto operator<=>(const MyClass&) const = default;

There is a proposed way to ask the compiler to generate the obvious/naive...but you can customise that for more complicated situations (?discussed below) implementation: see. See here for the language proposal, which contains justifications and discussion. This answer remains relevant for C++17 and earlier, and for insight in to when you should customise the implementation of operator<=>....

It may seem a bit unhelpful of C++ not to have already Standardised this earlier, but often structs/classes have some data members to exclude from comparison (e.g. counters, cached results, container capacity, last operation success/error code, cursors), as well as decisions to make about myriad things including but not limited to:

As other people have said, you need to implement a comparison function yourself.

There is a proposed way to ask the compiler to generate the obvious/naive(?) implementation: see here.

It may seem a bit unhelpful of C++ not to have already Standardised this, but often structs/classes have some data members to exclude from comparison (e.g. counters, cached results, container capacity, last operation success/error code, cursors), as well as decisions to make about myriad things including but not limited to:

C++20 introduced default comparisons, aka the "spaceship" operator<=>, which allows you to request compiler-generated </<=/==/!=/>=/ and/or > operators with the obvious/naive(?) implementation...

auto operator<=>(const MyClass&) const = default;

...but you can customise that for more complicated situations (discussed below). See here for the language proposal, which contains justifications and discussion. This answer remains relevant for C++17 and earlier, and for insight in to when you should customise the implementation of operator<=>....

It may seem a bit unhelpful of C++ not to have already Standardised this earlier, but often structs/classes have some data members to exclude from comparison (e.g. counters, cached results, container capacity, last operation success/error code, cursors), as well as decisions to make about myriad things including but not limited to:

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

It's easy to leverage std::tuple's comparison operators to compare your own class instances - just use std::tie to create tuples of references to fields in the desired order of comparison. Generalising my example from herehere:

It's easy to leverage std::tuple's comparison operators to compare your own class instances - just use std::tie to create tuples of references to fields in the desired order of comparison. Generalising my example from here:

It's easy to leverage std::tuple's comparison operators to compare your own class instances - just use std::tie to create tuples of references to fields in the desired order of comparison. Generalising my example from here:

Encoded less-than, greater-than symbols for HTML
Source Link
Tony Delroy
  • 106.7k
  • 16
  • 188
  • 265

A consistent approach to ==, etc<, > <= etc

A consistent approach to ==, etc

A consistent approach to ==, <, > <= etc

had introduced a duplicate re pointer comparisons
Source Link
Tony Delroy
  • 106.7k
  • 16
  • 188
  • 265
Loading
Moved generally-wanted solution higher in answer, generalised discussion to implementing all comparison operators with the help of std::tie.
Source Link
Tony Delroy
  • 106.7k
  • 16
  • 188
  • 265
Loading
Mention language proposal for defaulted comparison operators.
Source Link
Tony Delroy
  • 106.7k
  • 16
  • 188
  • 265
Loading
added locks to list of issues
Source Link
Tony Delroy
  • 106.7k
  • 16
  • 188
  • 265
Loading
added 1277 characters in body
Source Link
Tony Delroy
  • 106.7k
  • 16
  • 188
  • 265
Loading
added 68 characters in body
Source Link
Tony Delroy
  • 106.7k
  • 16
  • 188
  • 265
Loading
added 376 characters in body
Source Link
Tony Delroy
  • 106.7k
  • 16
  • 188
  • 265
Loading
added 443 characters in body
Source Link
Tony Delroy
  • 106.7k
  • 16
  • 188
  • 265
Loading
added 569 characters in body
Source Link
Tony Delroy
  • 106.7k
  • 16
  • 188
  • 265
Loading
added 170 characters in body
Source Link
Tony Delroy
  • 106.7k
  • 16
  • 188
  • 265
Loading
added 545 characters in body
Source Link
Tony Delroy
  • 106.7k
  • 16
  • 188
  • 265
Loading
Source Link
Tony Delroy
  • 106.7k
  • 16
  • 188
  • 265
Loading