C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Let spaceship return an int

From: Thiago Macieira <thiago_at_[hidden]>
Date: Wed, 27 Sep 2023 19:52:04 -0700
On Wednesday, 27 September 2023 18:13:29 PDT Jason McKesson via Std-Proposals
wrote:
> C++ explicitly didn't want to engage with this convention for its
> 3-way comparison operator. So they created a *new* convention: the
> ordering types. It's a convention that gives you a *compile error* if
> you try to directly return the result of `memcmp`, but if you return
> `memcmp(...) <=> 0`, it works fine.
>
> That's what good design looks like: noisy errors when you do it wrong,
> and easy to do it right.

And efficient code, as shown in my godbolt link earlier.

The compiler will translate an "a < b" to a sign check if you do exactly what
Jason suggested above, if the type returned is strong or weak ordering.
There's no need to check for a fourth value for unordered because it can't
happen.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel DCAI Cloud Engineering

Received on 2023-09-28 02:52:06