Redirect to different overloads at compile time?

David Bregman via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jun 29 19:24:09 PDT 2014


Suppose I have a C library which implements a function for 
several types. C doesn't have overloading, so their names will be 
distinct.

extern(C):
double foo_double(double);
float foo_float(float);

Now I want to build a D wrapper, and merge them into a single 
function with overloading:

T foo(T)

I could write out the two overloads manually:
double foo(double d) { return foo_double(d); }
float foo(float f) { return foo_float(f); }

but this isn't compile time, it will generate a stub function for 
each overload, meaning the wrapper will have performance overhead 
unless inlining can be guaranteed somehow.

Is it possible to do something like
alias foo = foo_double;
alias foo = foo_float;

or

template(T) foo {
   static if(T is double) {
     alias foo = foo_double;
   } else {
     // etc.
   }
}

These doesn't work of course.

I don't fully understand the template syntax yet, but I have a 
feeling this is possible with templates. Is it possible to do 
what I'm trying to do?


More information about the Digitalmars-d-learn mailing list