ProtoObject and comparison for equality and ordering
Timon Gehr
timon.gehr at gmx.ch
Wed May 15 12:38:08 UTC 2019
On 15.05.19 07:30, H. S. Teoh wrote:
> On Tue, May 14, 2019 at 09:02:49PM -0400, Andrei Alexandrescu via Digitalmars-d wrote:
> [...]
>> Well there would be in some instances. People often implement
>> comparisons as a - b currently, where a and b are int expressions.
> [...]
>
> FYI, the result of that is actually incorrect in some cases. (Consider
> what happens when there is overflow involved, such as int.max
> - int.min, and remember the type of the result.) So, not exactly the
> kind of code we should recommend, let alone bend over backwards to
> support.
>
> Int comparisons should be left as built-in operators <, =, >, etc.,
> which then get translated to the correct hardware instructions that
> don't suffer from the overflow bug.
>
>
> T
>
That's not an option with opCmp, because the operators cannot be
overloaded independently. Built-in types should just support overloaded
operator syntax, so one can write a.opCmp(b) instead of trying to get
overly clever with a manual implementation.
More information about the Digitalmars-d
mailing list