How to using opCmp

Heromyth bitworld at
Sat Dec 17 08:32:07 PST 2011

== Quote from bearophile (bearophileHUGS at's article
> Heromyth:
> > I have defined a opCmp function to overload comparison operators, then how can
> > I use it correctly?
> I don't have an answer yet, but I suggest you to look at the D docs that
describe how to use opCmp. Probably you need to accept a const Object, and to use
override. I suggest to strip your code from all the not essential things, so you
will see the problems better.
> Bye,
> bearophile

I tested
public override  int opCmp(Object o)
and got the same result.

The D document suggests rewriting
a < b
a.opCmp(b) < 0

While doing code porting from C# to D, I wander if I can directly use the
comparing operation between two class object.

Maybe the compiler should give some messages for not using "a < b".
Is it a compiler bug? Can anyone give some suggertion. It will be well grateful.

More information about the Digitalmars-d-learn mailing list