How to implement opCmp?

Honey via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jun 9 07:53:13 PDT 2017


Hi everyone!

Given

struct Pair(T, U = T)
{
   T f;
   U s;
}

what is the intended way to genrically implement opCmp for this 
struct?

The naive approach

struct Pair(T, U = T)
{
   // [...]

   int opCmp(const Pair r) const
   {
     immutable c = f.opCmp(r.f);
     return c != 0 ? c : s.opCmp(r.s);
   }
}

yields

Error: no property 'opCmp' for type 'const(int)'

if one tries to compare pairs of int.


More information about the Digitalmars-d-learn mailing list