I am trying to learn variadic templates and functions. I can't understand why this code doesn't compile:
template<typename T>
static void bar(T t) {}
template<typename... Args>
static void foo2(Args... args)
{
(bar(args)...);
}
int main()
{
foo2(1, 2, 3, "3");
return 0;
}
When I compile it fails with the error:
Error C3520: 'args': parameter pack must be expanded in this context
(in function foo2
).
One of the places where a pack expansion can occur is inside a braced-init-list. You can take advantage of this by putting the expansion inside the initializer list of a dummy array:
To explain the content of the initializer in more detail:
Demo.
An important advantage of expanding in
{}
is that it guarantees left-to-right evaluation.With C++17 fold expressions, you can just write