On Friday, 15 February 2013 at 23:41:50 UTC, Steven Schveighoffer wrote:
In any case, the example was bad, the point that which is better depends on the situation is still correct.

The compiler has to do something with "a<b". I would hope for a fundamental type it can avoid the lowering and just do whatever assembly is optimal. For a non-fundamental type, what is the case where (a.opCmp(b)<0) is more optimal than a comparable (a.opLess(b))?

It is easy to create an inefficient implementation of opCmp that calls "<" twice.

struct S {
   string s;
   int opCmp ( const ref S other ) {
     if ( s<other.s ) {
       return -1;
     } else if ( other.s<s ) {
       return 1;
     } else {
       return 0;
     }
   }
}

My point was, you can use compile time reflection to generate a suitable opCmp that uses s.opCmp if it exists or does the long version of two comparisons if not.

Thanks
Dan

Reply via email to