Bug 2803 – template + default argument = doesn't work

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2009-04-05T17:35:00Z
Last change time
2015-06-17T21:04:26Z
Keywords
pull, rejects-valid
Assigned to
nobody
Creator
andrei
Blocks
6613

Comments

Comment #0 by andrei — 2009-04-05T17:35:11Z
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 by smjg — 2009-04-05T19:00:33Z
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 by code — 2013-04-14T22:56:07Z
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 by verylonglogin.reg — 2014-07-13T10:12:25Z
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 #4 by k.hara.pg — 2015-01-06T15:02:03Z
Comment #5 by github-bugzilla — 2015-04-18T03:09:15Z
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 by github-bugzilla — 2015-06-17T21:04:26Z