Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ equal(==) overload, Shortcut or best way comparing all attributes

I have to overload a == operator in C++ for a class with many attributes.
The operator should return true, if and only if all attributes are equal. A shortcut might be useful, if these attributes change over time, to avoid bugs.

Is there a shortcut to compare every attribute in a class?

like image 656
Mehno Avatar asked Feb 29 '16 16:02

Mehno


People also ask

Can comparison operator be overloaded?

Relational Operators Overloading in C++You can overload any of these operators, which can be used to compare the objects of a class. Following example explains how a < operator can be overloaded and similar way you can overload other relational operators.

What is comparison operator overloading?

Operator overloading is a crucial concept in C++ that lets you achieve the functionality of the built-in operators while working with user-defined data types. Comparison operators in C++ are the ones that are there to compare two values with each other such as “==”, “!= ”, “>”, “<”, “>=”, and “<=”.

Which is used to overload the Greater Than operator?

C++ Program to overload the Greater than > operator In this program we try to overload the > operator with C++. Greater number C++ Program with operator overloading. n1 is greater than n2.

How do you overload an assignment operator?

Assignment Operators Overloading in C++You can overload the assignment operator (=) just as you can other operators and it can be used to create an object just like the copy constructor. Following example explains how an assignment operator can be overloaded.


2 Answers

There is no shortcut. You will have to list everything.

Some sources of error can be reduced by introducing a member function named tied() like:

struct Foo {     A a;     B b;     C c;     ...  private:     auto tied() const { return std::tie(a, b, c, ...); } }; 

So that your operator== can just use that:

bool operator==(Foo const& rhs) const { return tied() == rhs.tied(); } 

This lets you only list all your members once. But that's about it. You still have to actually list them (so you can still forget one).


There is a proposal (P0221R0) to create a default operator==, but I don't know if it will get accepted.


The above proposal was rejected in favor of a different direction regarding comparisons. C++20 will allow you to write:

struct Foo {     A a;     B b;     C c;      // this just does memberwise == on each of the members     // in declaration order (including base classes)     bool operator==(Foo const&) const = default; }; 
like image 148
Barry Avatar answered Sep 16 '22 22:09

Barry


Starting in C++11 with the introduction of tuples we also got std::tie(). This will let use make a tuple out of a bunch of variables and call a comparison function against all of them. You can use it like

struct Foo {     int a,b,c,d,e,f;     bool operator==(const Foo& rhs) { return std::tie(a,b,c,d,e,f) == std::tie(rhs.a,rhs.b,rhs.c,rhs.d,rhs.e,rhs.f); } }; 

You still have to list all the members you want to check but it makes it easier. You can also use this to make less than and greater than comparisons much easier.

It should also be noted that the variables are checked in the order you provide them to tie. This is important for less than and greater than comparisons.

std::tie(a,b) < std::tie(rhs.a, rhs.b); 

Does not have to be the same as

std::tie(b,a) < std::tie(rhs.b, rhs.a); 
like image 37
NathanOliver Avatar answered Sep 18 '22 22:09

NathanOliver