Template functions, can we make it more simple?

SteveGuo steveguo at outlook.com
Fri Aug 2 13:34:02 PDT 2013


I'm not an expert on programming language, if I made a naive 
issue, just forgive me:p

Can we declare a template function like this?

auto Add(a, b) // Note a, b do not have type, that means a and b 
use template type
{
     return a + b;
}

auto Sub(a, int b) // a uses template type, b is fixed to int
{
     return a - b;
}

When we call the function,

Add(1, 2); // deduced to be Add(int, int);
Add(1.5, 2.3); // deduced to be Add(double, double);
Add(1.5, "Hello"); // compiler error!

Sub(1.5, 1); // deduced to be Add(double, int);
Sub(1, 1.1); // deduced to be Add(int, int); compiler error, 
double can not converted to int automatically



More information about the Digitalmars-d mailing list