Can opCmp return a 'long' instead of 'int'?
Saurabh Das
saurabh.das at gmail.com
Sun Feb 16 23:12:39 PST 2014
On Sunday, 16 February 2014 at 15:19:08 UTC, Timon Gehr wrote:
> 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.
> }
Right. I didn't expect that! So I shouldn't use it anyway. Thanks!
Saurabh
More information about the Digitalmars-d-learn
mailing list