Thread overview
[Issue 8687] Variadic templates do not work properly with default arguments
Mar 19, 2015
Marc Schütz
Apr 18, 2017
Jonathan M Davis
Jan 07, 2018
Martin Nowak
Feb 04, 2018
Timothee Cour
March 19, 2015
https://issues.dlang.org/show_bug.cgi?id=8687

Marc Schütz <schuetzm@gmx.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schuetzm@gmx.net

--
April 18, 2017
https://issues.dlang.org/show_bug.cgi?id=8687

Jonathan M Davis <issues.dlang@jmdavisProg.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |issues.dlang@jmdavisProg.co
                   |                            |m

--- Comment #4 from Jonathan M Davis <issues.dlang@jmdavisProg.com> ---
It looks like the status quo right now (with the development version of dmd 2.075) is that default arguments work with variadic templates if the template is explicitly instantiated but not if IFTI is used. So,

-----
void foo(T...)(T args, string file = __FILE__) { }

void main()
{
    foo();
}
-----

won't compile, but

-----
void foo(T...)(T args, string file = __FILE__) { }

void main()
{
    foo!int(42);
}
-----

will. So, it looks like this is probably now purely an IFTI issue. And presumably, IFTI could be made to just assume that the default arguments are always used (since it has no way of differentiating between explicit arguments for those parameters and variadic arguments that have the same type, and anyone who wants to provid explicit arguments can always explicitly instantiate the template, which even works right now). That _seems_ like it would be straightforward, but I'm not at all familiar with how IFTI is implemented in the compiler.

Regardless, having it fixed so that the default arguments compile when IFTI is used would have a significant impact on stuff like std.experimental.logger, which currently has __FILE__, __LINE__, __FUNCTION__, __PRETTY_FUNCTION__, and __MODULE__ as default template arguments, which results in a function template being instantiated for every log call.

--
January 07, 2018
https://issues.dlang.org/show_bug.cgi?id=8687

Martin Nowak <code@dawg.eu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |code@dawg.eu

--- Comment #5 from Martin Nowak <code@dawg.eu> ---
While working on limited lifetimes (@safe RC/Uniq) I ran into another use-case where a default argument is needed.

// use implicit allocator argument
RC!T rc(T, Alloc, Args...)(Args args, return scope Alloc = Mallocator.instance)
if (!Args.length || !isAllocator!(Args[$-1]));

Adding the default argument inside of the function body (instead of in the signature), wouldn't be able to convey the lifetime information from the argument to the return value, hence it has to be a default argument which is evaluated on the call-site.

An algorithm for IFTI would be fairly simple, greedily match the variadic parameter to all remaining arguments, then add in all the default arguments.

The above case would have another overload for non-default allocators.

// last argument is explicit allocator
RC!T rc(T, Args...)(return scope Args args) if (Args.length &&
isAllocator!(Args[$-1]));

Indeed this shouldn't be too complex to support in the compiler.

--
February 04, 2018
https://issues.dlang.org/show_bug.cgi?id=8687

Timothee Cour <timothee.cour2@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |timothee.cour2@gmail.com

--- Comment #6 from Timothee Cour <timothee.cour2@gmail.com> ---
fixed in pending PR: https://github.com/dlang/dmd/pull/7831

--
February 04, 2018
https://issues.dlang.org/show_bug.cgi?id=8687

--- Comment #7 from github-bugzilla@puremagic.com ---
Commit pushed to master at https://github.com/dlang/dmd

https://github.com/dlang/dmd/commit/36af6b103207cd9805e9faefe4130447a98a8372 Fix Issue 8687 - Variadic templates do not work properly with default arguments

--
July 05, 2018
https://issues.dlang.org/show_bug.cgi?id=8687

Steven Schveighoffer <schveiguy@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://issues.dlang.org/sh
                   |                            |ow_bug.cgi?id=19057

--