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