Comparison operator overloading

Dominikus Dittes Scherkl via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Dec 7 03:49:51 PST 2015


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 
> http://ddili.org/ders/d.en/operator_overloading.html

Why should we don't use opCmp() ?
I can't see any recommendation about this in the cited book. In 
the chapter is only mentioned that opSliceAssign() and the like 
are discouraged - but without stating a reason or a replacement.
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.




More information about the Digitalmars-d-learn mailing list