Comparison operator overloading

Dominikus Dittes Scherkl via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Dec 7 09:18:20 PST 2015


On Monday, 7 December 2015 at 13:31:52 UTC, Mike Parker wrote:
> On Monday, 7 December 2015 at 11:49:51 UTC, Dominikus Dittes 
> Scherkl wrote:
>> 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 [1]. It wouldn't do to 
> return NaN from opCmp.
>
> [1] http://dlang.org/spec/operatoroverloading.html#compare

Hmm. But it works just fine! It overloads also the special 
floatingpoint operators <> !<> !<= and so on.
And how else could I handle a self-defined type that happens to 
have a NaN value (like my save-signed intergers do)?



More information about the Digitalmars-d-learn mailing list