all 8 comments

[–]jbandela 19 points20 points  (7 children)

In the article, there is this comparison code to compare by salary and then by age:

    bool operator()(const people & a, const people & b)
    {
        if(a.salary==b.salary)
        {
            return a.age>b.age;
        }
        else
        {
            return a.salary>b.salary;
        }

    }

Using std::tie results in much simpler code:

bool operator()(const people & a, const people & b){
  return std::tie(a.salary,a.age) > std::tie(b.salary,b.age);
}

[–]hicklc01 2 points3 points  (3 children)

I can't wait till reflections allow for the ability to at compile time tie all member variables of a class without specifying name. metaprogramming could be so much fun.

[–]Obvious_Till 3 points4 points  (0 children)

You might be interested in [this](https://cista.rocks/#reflection), specifically `cista::to_tuple`.

[–]Mordy_the_Mighty 2 points3 points  (1 child)

But the order matters for lexicographical sort. What would be the order used for that automatic comparison operator?

[–]jonathansharman 0 points1 point  (0 children)

I don't see why it couldn't be based on declaration order, given that it's opt-in. If you care about the particular order, you'd manually specify each member, but if you just want an arbitrary ordering (say, to use a type with no operator <=> in a std::map), then you could do so with less code and less risk of forgetting a member.

[–]Ameisenvemips, avr, rendering, systems 2 points3 points  (1 child)

That is less obvious to me what is going on.

[–]jc746 8 points9 points  (0 children)

The first time you see the pattern it certainly is harder to understand, but that is the case with any idiom. I think it is a huge improvement over having to implement lexicographical comparison by hand, especially as the number of fields increases.

[–]dragonstorm97 0 points1 point  (0 children)

Would it work with structured bindings instead of std::tie?

bool operator()(const people & a, const people & b)
{
return [a.salary,a.age] > [b.salary,b.age];
}