Some more template syntax sugar

Kristian kjkilpi at gmail.com
Tue Aug 29 01:50:47 PDT 2006


On Tue, 29 Aug 2006 11:22:19 +0300, Walter Bright  
<newshound at digitalmars.com> wrote:

> Reiner Pope wrote:
>> I think function templates still require too much in the way of type  
>> annotations. Take the canonical example, sqr:
>>  T sqr(T) (T x)
>> {
>>     return x*x;
>> }
>>  In this example, we have to declare T three times, even though I think  
>> this could be inferred. I propose an alternative syntax:
>>  auto sqr(x)
>> {
>>     return x*x;
>> }
>>  which IMHO looks very elegant.
>
> I know. The problem is that it is indistinguishable from:
>
> 	typedef int x;
> 	auto sqr(x);
>
> i.e. when x is a typedef and no parameter name is given, or when x is a  
> parameter name and no type is given.

Can't we make parameter names to be mandatory here?

typedef int x;
auto sqr(x);          //'x' is treated as parameter name
auto sqr2(x val, y);  //'x' is treated as typedef


Hmm, if someone would like to use non-auto return values with function  
templates, then how about adding '!' to function template names? For  
example:

int myTemplate!(x, y) {
     return(x < y ? -1 : (x > y ? 1 : 0));
}



More information about the Digitalmars-d mailing list