D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 2803 - template + default argument = doesn't work
Summary: template + default argument = doesn't work
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords: pull, rejects-valid
Depends on:
Blocks: 6613
  Show dependency treegraph
 
Reported: 2009-04-05 17:35 UTC by Andrei Alexandrescu
Modified: 2015-06-17 21:04 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Andrei Alexandrescu 2009-04-05 17:35:11 UTC
struct S {}
ref S getS();

void fun(T, U)(T t, ref U u = getS)
{
}

void main()
{
    fun(1);
}

./test.d(10): Error: template test.fun(T,U) does not match any function template declaration
./test.d(10): Error: template test.fun(T,U) cannot deduce template function from argument types !()(int)
Comment 1 Stewart Gordon 2009-04-05 19:00:33 UTC
I'm not sure that this is supposed to work.  It would appear that the process of binding template parameters to arguments happens strictly before that of applying default arguments.  Moreover, even if U is given, if it's anything but S then it triggers a type mismatch on the default argument.

It would appear that you need two templates

void fun(T)(T t)
{
    fun(t, getS());
}

void fun(T, U)(T t, U u)
{
}
Comment 2 Martin Nowak 2013-04-14 22:56:07 UTC
To me it looks like it should work. When the arity requires it, the default argument should be added to the list of function arguments before deducing the template arguments.
Comment 3 Denis Shelomovskii 2014-07-13 10:12:25 UTC
Simplified testcase:
---
void fun(T)(T = 0) { }

void main() { fun(); }
---
main.d(3): Error: template main.fun cannot deduce function from argument types !()(), candidates are:
main.d(1):        main.fun(T)(T = 0)
---
Comment 5 github-bugzilla 2015-04-18 03:09:15 UTC
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/766e4656c06ffddf0a4f6f4ce5a41d90207a2c57
fix Issue 2803 - template + default argument = doesn't work

https://github.com/D-Programming-Language/dmd/commit/596aef690e558e50db01c2abb7e31fbafdfa3d16
Merge pull request #4261 from 9rnsr/fix2803

Issue 2803 - template + default argument = doesn't work
Comment 6 github-bugzilla 2015-06-17 21:04:26 UTC
Commits pushed to stable at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/766e4656c06ffddf0a4f6f4ce5a41d90207a2c57
fix Issue 2803 - template + default argument = doesn't work

https://github.com/D-Programming-Language/dmd/commit/596aef690e558e50db01c2abb7e31fbafdfa3d16
Merge pull request #4261 from 9rnsr/fix2803