How to Define Comparison Operators by Default in C++--Jonathan Boccara

Do you know?

How to Define Comparison Operators by Default in C++

by Jonathan Boccara

From the article:

Implementing comparison operators in C++ is easier said than done.

Indeed, for most types, if we could talk to the compiler we would say something like: “to order them, use a lexicographical order on their members”.

But when it comes to writing the corresponding code, things get more complicated.

However, a classical technique using std::tuple makes the code much more concise for comparison operators, and it should be used by default. (At least before C++20, as C++20 made comparison operators even easier to write).

Let’s see the technique involving std::tuple and then how the comparison operators situation evolves with C++20...

Add a Comment

Comments are closed.

Comments (0)

There are currently no comments on this entry.