Comparison operator overloading
Mike Parker via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Dec 7 05:31:52 PST 2015
On Monday, 7 December 2015 at 11:49:51 UTC, Dominikus Dittes
> On Sunday, 6 December 2015 at 15:01:08 UTC, cym13 wrote:
>> Don't use opCmp, all binary operators should be overriden
>> using opBinary. For more information I recommend this page
> Why should we don't use opCmp() ?
> I can't see any recommendation about this in the cited book. In
That chapter actually shows quite clearly in the table in the
Binary Operators section that opCmp is used for <, <=, >, and >=,
which is also exactly what the language reference says. There's
also another section further down the page that covers opCmp
explicitly. I have no idea what cym13 is looking at.
> On the other hand the chapter also states that opCmp() should
> always return "int" - which is a bad idea if you e.g. want to
> provide a "NaN" value in your type. For that "float" is a much
> better return type for opCmp().
> So you should not weight in gold for the words in the cited
> book, I think.
This is not something the author is just making up. opCmp is
expected to return int because of the way the compiler rewrites
comparison expressions. See the table at . It wouldn't do to
return NaN from opCmp.
More information about the Digitalmars-d-learn