How to implement opCmp?
Patrick Schluter via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Jun 13 11:29:56 PDT 2017
On Tuesday, 13 June 2017 at 16:49:14 UTC, H. S. Teoh wrote:
> On Tue, Jun 13, 2017 at 10:51:40AM -0400, Steven Schveighoffer
> via Digitalmars-d-learn wrote: [...]
>> I think Andrei has a nice way to do opCmp for integers that's
>> a simple subtraction and negation or something like that.
> [...]
>
> In theory, cmp(int x, int y) can be implemented simply as (x -
> y).
> However, this fails when integer overflow occurs. Does Andrei
> have a
> nice way of doing this that isn't vulnerable to integer
> overflow?
>
return (x > y) - (x < y);
According to this stackoverflow question it looks like a good
candidate
https://stackoverflow.com/questions/10996418/efficient-integer-compare-function
More information about the Digitalmars-d-learn
mailing list