Can opCmp return a 'long' instead of 'int'?

Timon Gehr timon.gehr at gmx.ch
Sun Feb 16 07:19:08 PST 2014


On 02/16/2014 04:13 PM, Timon Gehr wrote:
> On 02/16/2014 02:59 PM, Saurabh Das wrote:
>>
>> This does compile and run correctly, but are there any hidden
>> assumptions or requirements on the return value of opCmp that I should
>> be aware of? Is there any reason that doing this may be not be wise?
>
> No, this is fine.

To be more precise: Returning long is fine.

The subtraction trick does not work in general regardless of return type:

import std.stdio;
struct S{
     int d;
     int opCmp(S r){ return d - r.d; }
}

void main(){
     assert(S(1)<S(2)); // passes. ok.
     assert(S(int.min)>S(int.max)); // passes. oops.
}



More information about the Digitalmars-d-learn mailing list