template parameter list syntax

Patrick Schluter Patrick.Schluter at bbox.fr
Sun Jan 20 12:35:58 UTC 2019


On Saturday, 19 January 2019 at 22:52:25 UTC, Carl Sturtivant 
wrote:
> On Saturday, 19 January 2019 at 20:52:06 UTC, H. S. Teoh wrote:
>>
>> The template function declaration syntax is actually a 
>> shorthand for the so-called eponymous template.  When you 
>> write:
>>
>> 	auto myFunc(Args...)(Args args) { }
>>
>> it's actually a shorthand for:
>>
>> 	template myFunc(Args...) {
>> 		auto myFunc(Args args) { }
>> 	}
>>
>> For the shorthand to introduce ! would be incongruous because 
>> the template declaration syntax doesn't use !.
>>
>>
>> T
>
>
> I'm asking why template declaration syntax doesn't use ! so 
> it's parallel to template use syntax which does use ! .
>
> I was not talking about function templates as such.

Afaik it's a parser problem. Walter wanted to avoid the 
catastrophic state that exist in C++ of an ambiguous grammar. He 
insited that the the grammar of the D language be as 
straightforward as can be.
In the template declaration, both parenthesis pairs are required 
and the parser knows directly that first '(' == template 
parameter, second '(' rt parameter.
At the instanciation site, it was preferred to have parameter 
inference, i.e. that the template parameter [1] can be omitted if 
it can be deduced from the runtime parameter. This has the 
consequence that the starting '(' becomes ambiguous, it can mean 
template parameter start or runtime parameter start. Using 
another operator allows to lift the ambiguity. Forcing to have 
parenthesis would also lift the ambiguity, but it would be at the 
cost of parameter inference as it would be impossible distinguish 
between infered parameter or no parameter. ! was chosen because 
it is light and its only use is in prefix context, in template 
instantiation it's in a infix position and thus completely 
unambiguous with the ! operator.
[1]: https://tour.dlang.org/tour/en/basics/templates


More information about the Digitalmars-d mailing list