Andrej Mitrovic <andrej.mitrov...@gmail.com> wrote:
This isn't a question but more of an observation. Here's an interesting
template from the docs:
template Foo(T, R...)
{
void Foo(T t, R r)
{
writeln(t);
static if (r.length) // if more arguments
Foo(r); // do the rest of the arguments
}
}
void main()
{
Foo(1, 'a', 6.8);
}
What really intrigues me here is not the tuples which I already get, but
the fact that Foo is a function template. But if I remove the inner
Foo() function then it's not a function template anymore so the call in
main won't work. The inner function must have the same name as the
template, apparently (it doesn't even state this in the docs from what I
can tell!).
It is in fact mentioned, but not touted as an impressive feature:
"If a template declares exactly one member, and that member is a function
with the same name as the template, it is a function template
declaration."[1]
The same is true for struct, union, and class templates, as well as
'Implicit Template Properties', meaning any template with exactly one
member, if that member has the same name as the template itself.
In fact, shorthand eponymous templates, like struct foo(T){} or
void bar(T)(); translate internally to template foo(T){ struct foo{} }
and template bar(T){ void bar(){} }.
Other common patterns used with this trick are compile-time constants:
template foo!(int n) {
enum foo = n;
}
and alias types:
template foo(int n, T...) {
alias T[n] foo;
}
[1]: http://www.digitalmars.com/d/2.0/template.html#function-templates
--
Simen