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.
Comment #1 by maxsamukha — 2021-06-29T09:33:59Z
(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
Comment #2 by maxsamukha — 2021-06-29T09:40:48Z
(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.
Comment #3 by maxsamukha — 2021-06-29T10:01:22Z
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
Comment #4 by robert.schadek — 2024-12-13T19:17:27Z