Problem with Sorting

Steven Schveighoffer schveiguy at
Fri Jan 28 08:00:32 PST 2011

On Fri, 28 Jan 2011 00:05:34 -0500, Jonathan M Davis <jmdavisProg at>  

> Also, the signature for opCmp on classes is supposed to be
> int opCmp(Object o)
> It's supposed to take Object. I'm surprised that it works at all the way  
> that it
> is.

std.algorithm.sort is a template, so it has access to the fully derived  
class type.  The compiler simply rewrites a < b as a.opCmp(b), which calls  
his function if you are using the derived type.

Without defining opCmp as taking object, however, the builtin array sort  
would fail with a hidden function error.


More information about the Digitalmars-d-learn mailing list