Thread overview
[Issue 22091] Unexpected behaviour with variadic template param followed by default parameter
Jun 29, 2021
Max Samukha
Jun 29, 2021
Max Samukha
Jun 29, 2021
Max Samukha
Dec 17, 2022
Iain Buclaw
June 29, 2021
https://issues.dlang.org/show_bug.cgi?id=22091

Max Samukha <maxsamukha@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |maxsamukha@gmail.com

--- Comment #1 from Max Samukha <maxsamukha@gmail.com> ---
(In reply to Sharon Hadas from comment #0)
> When using a variadic template parameter in a function, it is possible to define another parameter afterwards. This behaves in the expected way in most cases, however when the parameter is a default parameter, it is impossible to pass anything into it, since the variadic parameter seems to be greedy and always takes in whatever is passed to it.
> 
> Here's an example that shows this behaviour:
> 
> import std;
> void variadic(Args...)(Args args) {
>     writefln("variadic - Args len is %s", args.length);
>     variadic2(args, 5);
> }
> void variadic2(Args...)(Args args, int anotherArg = 4) {
>     writefln("variadic2 - Args len is %s, args is %s, anotherArg is %s",
> args.length, args, anotherArg);
> }
> void main()
> {
>     variadic();
> }
> 
> This example prints (seems to be consistent across compilers and operating
> systems):
> 
> variadic - Args len is 0
> variadic2 - Args len is 1, args is 5, anotherArg is 4
> 
> This is counterintuitive behaviour. I think the correct behaviour in this case would be for it to not compile.

FWIW, you can use explicit instantiation to work around this particular case:

import std;
void variadic(Args...)(Args args) {
    writefln("variadic - Args len is %s", args.length);
    variadic2!Args(args, 5);
}
void variadic2(Args...)(Args args, int anotherArg = 4) {
    writefln("variadic2 - Args len is %s, anotherArg is %s", args.length, args,
anotherArg);
}
void main()
{
    variadic();
}

variadic - Args len is 0
variadic2 - Args len is 0, anotherArg is 5

--
June 29, 2021
https://issues.dlang.org/show_bug.cgi?id=22091

--- Comment #2 from Max Samukha <maxsamukha@gmail.com> ---
(In reply to Max Samukha from comment #1)

> FWIW, you can use explicit instantiation to work around this particular case:

Never mind. That's useless.

--
June 29, 2021
https://issues.dlang.org/show_bug.cgi?id=22091

--- Comment #3 from Max Samukha <maxsamukha@gmail.com> ---
Just for kicks, this seems to simulate the expected behavior:

import std;
void variadic2(Args...)(Args args)
{
    static if (Args.length && is(Args[$ - 1]: int)) {
        alias anotherArg = args[$ - 1];
        alias _args = args[0..$ - 1];
    } else {
        int anotherArg = 5;
        alias _args = args;
    }

    writefln("variadic2 - Args len is %s, anotherArg is %s", _args.length,
anotherArg);
}

void main()
{
    variadic2();
    variadic2(4);
    variadic2(2, "foo");
    variadic2(2, 3);
}


variadic2 - Args len is 0, anotherArg is 5
variadic2 - Args len is 0, anotherArg is 4
variadic2 - Args len is 2, anotherArg is 5
variadic2 - Args len is 1, anotherArg is 3

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=22091

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P1                          |P3

--