ProtoObject and comparison for equality and ordering

Andrei Alexandrescu SeeWebsiteForEmail at erdani.com
Wed May 15 15:11:00 UTC 2019


On 5/15/19 1:30 AM, 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.)

Thanks.

> So, not exactly the
> kind of code we should recommend, let alone bend over backwards to
> support.

All integral arithmetic is subject to overflow, at every step of the 
way. A D coder implementing opCmp would need to figure when the range of 
the operators does not put comparisons at risk, and where it does, use 
e.g. CheckedInt or a more elaborate approach.


More information about the Digitalmars-d mailing list