Simpler way to compare objects

SOLVED

Say I have Person:

struct Person
{
    int age;
    double height;
    double weight;
    std::string name;
}

My comparator is quite ugly and looks like:

bool operator==(const Person & a, const Person & b)
{
    if(a.age != b.age) return false;
    if(a.height != b.height) return false;
    if(a.weight != b.weight) return false;
    if(a.name != b.name) return false;
    return true;
}

Is it possible to rewrite it in a format that looks more like:

bool operator==(const Person & a, const Person & b)
{
    for(auto field : {age, height, weight, name})
        if(a.field != b.field) return false;
    return true;
}

Seems more readable, modern and extendable. What is the closest I can get to something like the bottom set of code?

Posted Apr 1, 2019, 10:30 PM UTC

011
jedwardsol•

One neater way is

bool operator==(const Person & a, const Person & b)
{
    return    std::tie(a.age,a.height,a.weight,a.name) 
           == std::tie(b.age,b.height,b.weight,b.name);
}
7
JayesarOP•

Thanks for the response. I do quite like that but my real example has a lot of fields, so it looks a bit unruly and unmanageable having like 4 lines of things that should match. I have changed the code to be:

bool operator==(const Person & a, const Person & b) 
{
    auto AllPublicMembers = [](const Person & obj){  return std::tie(obj.age obj.height, obj.weight, obj.name); }
    return AllPublicMembers(a) == AllPublicMembers(b);
}

This way I only need to add new members on the one line and I get them for free.

3

Best option: let the compiler do it

https://en.cppreference.com/w/cpp/language/default_comparisons

Second best option:

You could use the preprocessor to get the syntax you want. something like:

#define compare(seq) boost_pp_seq_for_each ...

// ....

compare((age)(height)(weight)(name));

using boost_pp_seq_ family of functions

https://www.boost.org/doc/libs/1_69_0/libs/preprocessor/doc/ref/seq_for_each.html

third best option: do the same as the previous one, but use templates instead of boost pp seq. i think you'd need pointer to member variables which I personally find unsightly. but this is almost exactly what your example code is

https://en.cppreference.com/w/cpp/language/pointer#Pointers_to_data_members

other option:

if you didn't have any variables with == operators (like string does), the you could do a memcmp. this is pretty ugly tho.

2
alfps•

Very few are using C++20 compilers.

After all it's a future standard.

2
Middlewarian•

Those working on on-line services may have this opportunity. I'm working on a C++ middleware service and am able to use new compilers with that.

2

true

1
sebamestre•

Very few are using t̸̯͓̟̟̻͌͊̿͌͋̂̾͑̂̃̽̿̎̿̿̄͘͠ḩ̵̙̜͕̩̖͙̳̳̗͇̖̪͖̓̑̆̂̽̿̆̎͠e̷̢̻̝̭͇̗͔͚̰̻̙͚͖̫̺̰̭̒͑̾̈͐̈́̏̈́̊̽̋̔͋͗̿̕̕͜ ̵̩̲͎͉̈̍̾̓͑͋͆̅͛d̸̛̖̲͚̝̲͎̳̓̑̿̌͊̔͋̌͌̒̒̕e̶̢̲͉͔̹̠͉̭̩̹̱̪̞̫̩̙͛̎̾͋̿͐̈́̒͌̈́̊͛̍̀̐̾͘v̷̡̨̙̜̫̠͕͙̩͖͙̜̣̪͙̭̮̦̠̊̾̆̋̚̚i̶̧̢̤͕͓̰̠͖͕̘̺͔͙͓͎̣̘̟̼͂c̶̨̢̬̼̥̹̗̙̹̺̱̘̹̖̈́͋͒͗͆͐͛͌e̸̢̞̺̭̼̲̼̠̣̘̥̜̯̾.

​

After all it's a *future* standard.

-1
JayesarOP•

Thanks for the response. The code base I'm working on is from another team, so I'm not trying to be too intrusive, so things like bringing in boost is probably out of the question for them.

I would much prefer to just let the compiler do it but for each of these classes they have it all explicitly defined, so I feel bound to do the same for style more than value.

1

probably just writing it the older style is the way to go then

edit just saw your tie version and that's pretty nifty

1
[deleted]•

[deleted]

2
JayesarOP•

This is what I was trying to avoid simply because in my real example (not Person class), I have a large number of fields and the chained statement would be 20+ lines.

Thank you though.

2
NEET_Here•

Use a tuple called members, should make the comparison nice and clean. Keep the members tuple private.

#include <iostream> 
#include <tuple>
#include <string>

struct Widget {
    
    Widget(int age, const std::string& name): age(age), name(name) {}
    
    bool operator==(const Widget& rhs) {
        return this->members == rhs.members;
    }
    
    int age;
    std::string name;
    
    std::tuple<int&, std::string&> members = std::tie(age, name);
};

int main() {
    
    Widget a(5, "dog");
    Widget b(5, "dog");
    
    Widget c(10, "other dog");
    
    std::cout << "a == b: " << (a == b) << std::endl;
    a.age = 2;
    
    std::cout << "a == b: " << (a == b) << std::endl;
    std::cout << "a == c: " << (a == c) << std::endl;
    std::cout << "b == c: " << (b == c) << std::endl;
}

Make sure to overload the assigment operator, otherwise instead of making a copy of members when you assigned a widget to another, it will asisgn a reference.

Output:

a == b: 1                                                                                                                                                                                                                                               
a == b: 0                                                                                                                                                                                                                                               
a == c: 0                                                                                                                                                                                                                                               
b == c: 0 
1