nullptr
nullptr

Reputation: 353

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

Upvotes: 15

Views: 671

Answers (1)

Barry
Barry

Reputation: 303357

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.

Upvotes: 17

Related Questions