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

Saurabh Das saurabh.das at gmail.com
Sun Feb 16 05:59:02 PST 2014


Hello,

The call signature for opCmp in a struct is:

struct S {
   int opCmp(ref const S s) const { ... }
}

int opCmp(ref const S s) const { return _val - s._val; }

This works fine if _val is 'int'. However, if _val is 'long' then 
subtracting 2 longs may not result in an int - and therefore I 
would have to replace the subtraction above with 2 comparison 
operators.

My question is: Can I rewrite opCmp to return a 'long', like:

struct S {
   long opCmp(ref const S s) const { return _val - s.val; }
}

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?

Thanks,
Saurabh


More information about the Digitalmars-d-learn mailing list