Fun with templates
TommiT
tommitissari at hotmail.com
Sat Jul 6 05:51:17 PDT 2013
On Saturday, 6 July 2013 at 11:35:28 UTC, Manu wrote:
> The way that makes the most sense to me is:
>
> void f(T)(Unqual!T t) {}
But my syntax makes pretty much sense too:
void foo(inout T)(T var)
{
var = 42;
}
void main()
{
foo!(int)(1);
foo!(const int)(2);
foo!(immutable int)(3);
}
For all those calls to foo, the type parameter T is deduced to be
int, and the function template instantiates to the signature:
void foo(int var);
The function template foo accepts int, const(int) and
immutable(int) as the type parameter, because they're all
convertible to inout(int).
The same kind of logic can be seen with runtime arguments, when
they are qualified with something:
void bar(T)(const T var) { }
void main()
{
const int n;
bar(n);
}
Here, similarly, the type of T is deduced to be int, even though
the argument passed in is const(int).
More information about the Digitalmars-d
mailing list