Template with equivalent function names

Andrej Mitrovic andrej.mitrovich at gmail.com
Sun Aug 15 20:18:27 PDT 2010


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!).

There seem to be plenty of ways of making templates, sometimes you explicitly add the template keyword, sometimes not.. I kind of wish the syntax was more unified in this regard.

Well anyway, that page (http://www.digitalmars.com/d/2.0/template.html) reveals some really powerfull stuff you can do with templates. If only template error messages were as exciting to look at! :p



More information about the Digitalmars-d-learn mailing list