Template functions, can we make it more simple?
Flamaros
flamaros.xavier at gmail.com
Sat Aug 3 03:42:17 PDT 2013
On Friday, 2 August 2013 at 20:34:04 UTC, SteveGuo wrote:
> 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
I prefer the current template syntax, it's nice to want to reduce
the number of keywords/syntax, but generally to understand the
code it's better for productivity to have something really
explicit.
When you read an old code or something written by someone else
it's really important to see at the first sight what the code is
intended to do.
auto keyword hide too much how variables are used, is it ref?
const?,...
More information about the Digitalmars-d
mailing list