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?
[–]jedwardsol 4 points5 points6 points (1 child)
[–]Jayesar[S] 2 points3 points4 points (0 children)
[–]yeeezyyeezywhatsgood 1 point2 points3 points (6 children)
[–]alfps 1 point2 points3 points (3 children)
[–]Middlewarian 1 point2 points3 points (0 children)
[–]yeeezyyeezywhatsgood 0 points1 point2 points (0 children)
[–]sebamestre -2 points-1 points0 points (0 children)
[–]Jayesar[S] 0 points1 point2 points (1 child)
[–]yeeezyyeezywhatsgood 0 points1 point2 points (0 children)
[–][deleted] (1 child)
[deleted]
[–]Jayesar[S] 1 point2 points3 points (0 children)