How to implement opCmp?

drug via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jun 9 08:34:36 PDT 2017


09.06.2017 18:12, Steven Schveighoffer пишет:
> int doCmp(T)(auto ref T t1, auto ref T t2)
> {
>     static if(is(typeof(t1.opCmp(t2))))
>      return t1.opCmp(t2);
>     else
>     {
>        if(t1 < t2) return -1;
>        else if(t1 > t2) return 1;
>        return 0;
>     }
> }
Isn't it enough to use just '<' et al?
int opCmp(const Pair r) const
{
     if (f < r.f)
	return -1;
     if (f > r.f)
	return 1;

     if (s < r.s)
	return -1;
     if (s > r.s)
	return 1;

     return 0;
}
for floating types it's not completely right of course but nevertheless


More information about the Digitalmars-d-learn mailing list