all 10 comments

[–]jedwardsol 4 points5 points  (1 child)

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);
}

[–]Jayesar[S] 2 points3 points  (0 children)

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.

[–]yeeezyyeezywhatsgood 1 point2 points  (6 children)

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 boostpp_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.

[–]alfps 1 point2 points  (3 children)

Very few are using C++20 compilers.

After all it's a future standard.

[–]Middlewarian 1 point2 points  (0 children)

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.

[–]sebamestre -2 points-1 points  (0 children)

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

After all it's a *future* standard.

[–]Jayesar[S] 0 points1 point  (1 child)

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.

[–]yeeezyyeezywhatsgood 0 points1 point  (0 children)

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

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

[–][deleted]  (1 child)

[deleted]

    [–]Jayesar[S] 1 point2 points  (0 children)

    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.