Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++20 default equality / inequality

Tags:

c++

c++20

C++20 gives us the spaceship operator and even allows us to default it, generating all comparisons with default semantics, which will remove a lot of boilerplate from our code bases, great!

But how about equality and inequality? Does C++20 also give us a way to default equality and inequality? According to cppreference.com, defaulting spaceship will only give us < <= >, and >=, but not == and !=. Given that I usually need equality/inequality way more often than greater/less, this seems to be quite unfortunate. So, is there a way to also (or only) default generate == and != in C++20?

Update: The page now contains a description for defaulting operator==. Seems that I accessed the page like two hours before the documentation for this was added. :D

like image 206
gexicide Avatar asked Oct 29 '20 15:10

gexicide


People also ask

How does C check equality?

Checking for equality in C and C++identifier == identifier; The == sign is used to compare primitive types such as char, int, float, etc. The comparison operator returns true if the two identifiers are equal.

What type of C operator Do you need to compare two values?

The equality operator (==) is used to compare two values or expressions. It is used to compare numbers, strings, Boolean values, variables, objects, arrays, or functions.

How do you write an equality operator in C++?

The equality operators in C++ are is equal to(==) and is not equal to(!=). They do the task as they are named. The binary equality operators compare their operands for strict equality or inequality. The equality operators, equal to (==) and not equal to (!

What is the inequality operator in C++?

Inequality operator != The inequality operator != returns true if its operands are not equal, false otherwise. For the operands of the built-in types, the expression x != y produces the same result as the expression ! (x == y). For more information about type equality, see the Equality operator section.

How to compare two objects using equality operator in c++20?

In C++20, if we use the default <=>, then all the other comparison operators are also added. Here in the code the class has two integers so to compare there would be user-defined comparison required. But since the equality operator will be generated automatically, I need to know how it will compare the objects.

What are the relational and equality operators in C?

The relational and equality operators test the following relationships: The first four operators in the list above have a higher precedence than the equality operators ( == and != ). See the precedence information in the table Precedence and Associativity of C Operators. The operands can have integral, floating, or pointer type.

What is the equality comparison function in C++?

The equality comparison function (whether defaulted or not) is called whenever values are compared using == or != and overload resolution selects this overload. Like defaulted special member functions, a defaulted comparison function is defined if odr-used or needed for constant evaluation .


1 Answers

Does C++20 also give us a way to default equality and inequality?

Yes (but see the last paragraph). The syntax is same:

friend bool operator==(const T&, const T&) = default;
friend bool operator!=(const T&, const T&) = default; // not needed if == exists

The ordered inequality can be defaulted also:

friend bool operator<=(const T&, const T&) = default;
// ...

defaulting spaceship will only give us < <= >, and >=, but not == and !=

This is inaccurate. Defaulting three way comparison gives us all comparisons So, the above are not needed in that case.

like image 59
eerorika Avatar answered Oct 12 '22 19:10

eerorika