Reputation: 353
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
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