opCmp: strange definition in specs

Steven Schveighoffer schveiguy at yahoo.com
Mon Mar 8 03:53:33 PST 2010


On Mon, 08 Mar 2010 06:15:16 -0500, Norbert Nemec  
<Norbert at nemec-online.de> wrote:

> I just read through the specs about operators and found a strangeness in  
> the definition of opCmp:
>
> http://www.digitalmars.com/d/2.0/operatoroverloading.html#compare
>
> Mathematically the following are equivalent
> 	a < b       <=>       b > a
>
> But the definition seems to swap
> 	a < b	 into	  b >= a
>
> I have not tested the compiler about this yet...

 From testing (dmd 2.040) it appears that the spec is wrong (the correct  
thing is done).  I'm amazed this was never caught, I think it's been this  
way for a long time.

Good catch, you should file a bug.

test code:


import std.stdio;

struct S
{
}

struct T
{
     int opCmp(S s)
     {
         return 0;
     }
}

void main()
{
     S s;
     T t;
     writeln(s < t);
     writeln(t < s);
     writeln(s <= t);
     writeln(t <= s);
     writeln(s > t);
     writeln(t > s);
     writeln(s >= t);
     writeln(t >= s);
}

outputs:

false
false
true
true
false
false
true
true

-Steve



More information about the Digitalmars-d mailing list