On Tue, Sep 16, 2014 at 04:19:10PM +0000, via Digitalmars-d-learn wrote: > The following code does not compile, because the custom predicate of > std.algorithm.sort is a template parameter, and therefore can only be > a function, but not a delegate.
What makes you think so? Template parameters certainly can be delegates. I use that all the time. > In C++, there is a variant of sort taking a function-object as a > second (run-time) parameter, but phobos does not seems to have such a > thing. It seems like a pretty common problem to me, so does anyone > have a solution? > > > > class Foo > { > int[] order; > > bool cmp(int a, int b) const > { > return order[a] < order[b]; > } > } > > void main() > { > auto f = new Foo; > int[] data; > sort!(f.cmp)(data); > } Try this: sort!(&f.cmp)(data); In D, writing `f.cmp` (without the &) can in some contexts be misinterpreted as calling the function and passing the return value to the template parameter, which is probably why you're getting a compile error. T -- Why ask rhetorical questions? -- JC