www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Template specialization question

reply Steven Schveighoffer <schveiguy yahoo.com> writes:
void foo(T)(T t){writeln("a");}
void foo(T...)(T t){writeln("b");}

foo(1);

Compiles? If so, which prints out?

I was surprised by the answer. I can't find docs for it. Is the behavior 
intended?

-Steve
Nov 24 2016
next sibling parent reply ag0aep6g <anonymous example.com> writes:
On 11/24/2016 06:47 PM, Steven Schveighoffer wrote:
 void foo(T)(T t){writeln("a");}
 void foo(T...)(T t){writeln("b");}

 foo(1);

 Compiles? If so, which prints out?

 I was surprised by the answer. I can't find docs for it. Is the behavior
 intended?
Took me a bit to find it, but it is documented: "If both a template with a sequence parameter and a template without a sequence parameter exactly match a template instantiation, the template without a TemplateSequenceParameter is selected." https://dlang.org/spec/template.html#variadic_template_specialization
Nov 24 2016
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On Thursday, 24 November 2016 at 17:59:55 UTC, ag0aep6g wrote:
 Took me a bit to find it, but it is documented:

 "If both a template with a sequence parameter and a template 
 without a sequence parameter exactly match a template 
 instantiation, the template without a TemplateSequenceParameter 
 is selected."

 https://dlang.org/spec/template.html#variadic_template_specialization
Thanks. I did not find this! You learn something new about D every day -Steve
Nov 24 2016
prev sibling parent Stefan Koch <uplink.coder googlemail.com> writes:
On Thursday, 24 November 2016 at 17:47:04 UTC, Steven 
Schveighoffer wrote:
 void foo(T)(T t){writeln("a");}
 void foo(T...)(T t){writeln("b");}

 foo(1);

 Compiles? If so, which prints out?

 I was surprised by the answer. I can't find docs for it. Is the 
 behavior intended?

 -Steve
That is expected. The general rule is go for the narrowest match. Since a matches a smaller set then b. a is selected.
Nov 24 2016