Template instantiation and overloaded functions

Falk Henrich schreibmalwieder at hammerfort.de
Sun Mar 25 13:49:08 PDT 2007


Hi,

I'm troubled by D's template instantiation algorithm in combination with
overloaded functions. If I have

Z[] zip(X,Y,Z)(Z function(X, Y) f, X[] x, Y[] y)
{ Z[] z; z.length = x.length;
  for(size_t i = 0; i < x.length; i++) { z[i] = f(x[i], y[i]); }
  return z;
}

and some functions

double plus(double x, double y) {return x+y;}
long plus(long x, long y) {return x+y;}
int plus(int x, int y) {return x+y;}

and now do

int[] a = [1,2,0]; int[] b = [0,1,0];
writefln(zip(&plus, a,b));

the compiler (gdc 0.23) tells me:

functional.d:186: template functional.zip(X,Y,Z) does not match any template
declaration
functional.d:186: template functional.zip(X,Y,Z) cannot deduce template
function from argument types (double(*)(double x, double y),int[],int[])

By switching the order of declaration of the overloaded plus functions I
discovered that the compiler will only consider the first declaration, no
matter what. Does this behavior adhere to the D specification?

Falk



More information about the Digitalmars-d-learn mailing list