[Proposal] "Name inference" for function templates

Andrei Khropov andkhropov at nospam_mtu-net.ru
Fri Jun 16 01:46:39 PDT 2006


Markus Dangl wrote:

> Andrei Khropov schrieb:
> > I think retyping the same name twice for function templates is not a very
> > natural way, so I propose omitting the template name if there's only a
> > single declaration in the template's body and then this declaration's name
> > is "inferred " for subsequent use, i.e. :
> > ------------------------------------------------
> > template sqr(T)
> > {
> > 	T sqr( T x )
> > 	{
> > 		return x*x;
> > 	}
> > }
> > ------------------------------------------------
> 
> http://www.digitalmars.com/d/template.html
> If there's only a single declaration, you can even write:
> 
> T sqr(T) ( T x )
> {
>     return x*x;
> }
> 
> Which is already in the language and even shorter than your proposal ;)
> This is the same for class templates.

Well, besides that it isn't documented (as far as I can see)
I think it's rather confusing 
(two sequential pairs of parameters in parentheses - I a more complicated case
you really don't tell whether it is template or not at the first glance).
It's different from the class case where parameter list in parentheses in
umabiguous.

I've thought of that variant too but it seems to me that it's less readable.

Another variant maybe is to insert "!" before template parameters list in a
declaration too:

----------------------------
T sqr!(T) ( T x )
{
     return x*x;
}
----------------------------

-- 
AKhropov



More information about the Digitalmars-d mailing list