Partially instantiating templates?

Magnus Lie Hetland magnus at
Mon Jan 31 03:00:19 PST 2011

I'm building a function (or template or whatever, really) that is 
related to map and minPos in std.algorithm. Basically, it's the 
standard mathematical argmin, except that it also returns min. It looks 
something like this:

  auto minArg(alias fun, Range, T)(Range range, out T minVal) {

Already there may be issues -- the return type should be 
ElementType(range) and T should be the return type of fun ... but it 
works. (Suggestions on these issues are welcome, but that's not really 
the main point here.)

The thing is, because I'm also returning the actual value, I'd rather 
not use the strategy of std.algorithm.minPos, which asks you to use an 
inverted function to get maxPos; instead, I'd like an explicit maxArg 
function. My idea was to have a common, more general optArg, which took 
an operator ("<" or ">") as a compile-time argument. Then I could do 
something like

  alias optArg!"<" minArg;
  alias optArg!">" maxArg;

Then, at some *later* time, I might want to do something like:

  alias maxArg!((v) {return dist(u,v);}) farthest;

(By the way: For some reason, I'm not allowed to use curry(dist,u) 
instead of the lambda here, it seems. Any insights on that? Would have 
been nice to use "d(u,a)" as well -- as I do use unaryFunc on the fun 
argument. That doesn't work either, though...)

I've been able to make either one of these two pieces of functionality 
work with some twiddling and nesting (i.e., *either* instantiating 
optArg into minArg/maxArg, *or* instantiating explicitly defined 
minArg/maxArg into specialized functions) but combining them has so far 
eluded me (unless I start fiddling with strinc constants and mixin(), 
which seems excessively hacky for such a simple thing).

Any ideas/suggestions? I'm sure I'm missing something obvious ... 
(Perhaps even existing functionality for minArg/maxArg -- although the 
general question still stands.)

Magnus Lie Hetland

More information about the Digitalmars-d-learn mailing list