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)
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) { }
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.
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) ---
https://github.com/D-Programming-Language/dmd/pull/4261
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
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