Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you reverse a strong_ordering?

Is there an easier way to achieve the effect of this function?

strong_ordering reverse(strong_ordering v) {
    if (v > 0)
        return strong_ordering::less;
    else if (v < 0)
        return strong_ordering::greater;
    else
        return v;
}
like image 509
nullptr Avatar asked Feb 03 '23 16:02

nullptr


1 Answers

Yep:

strong_ordering reverse(strong_ordering v)
{
    return 0 <=> v;
}

Which is literally specified as what you want:

Returns: v < 0 ? strong_­ordering​::​greater : v > 0 ? strong_­ordering​::​less : v.

This follows the general principle that x <=> y and y <=> x are opposites, and v <=> 0 is just an identity operation for v.

like image 87
Barry Avatar answered Feb 17 '23 21:02

Barry