Method-call based on template argument

nrgyzer nrgyzer at gmail.com
Wed May 9 03:33:16 PDT 2012


I'm trying to do the following:

module myModule;

int calculate(T)(int a, int b) if(T == "add") {
   return a + b;
}

int calculate(T)(int a, int b) if(T == "subtract) {
   return a - b;
}

But when I try to run the following code, I get "template myModule.calculate"
does not match any function template declaration":

void main() {

   int a = 10;
   int b = 20;
   std.stdio.writeln(calculate!("add")(a, b));
   std.stdio.writeln(calculate!("subtract")(a, b));

}

Is there any way to check if the given template argument is "add" or
"subtract"? I also tried:

int calculate(string T)(int a, int b) if(T == "add") {
   return a + b;
}

int calculate(string T)(int a, int b) if(T == "subtract) {
   return a - b;
}

... without any success (I'll get an instantiation error).


More information about the Digitalmars-d-learn mailing list