Somehow I don’t get how variadic template parameter packs are expanded. What’s wrong with thie following code?
#include <iostream>
template <typename T>
struct print_one
{
static void run(const T& t)
{
std::cout << t << ' ';
}
};
template<typename... Args>
void print_all(Args&&... args)
{
// the next line doesn't compile:
print_one<Args>::run(std::forward<Args>(args))...;
}
int main()
{
print_all(1.23, "foo");
}
Clang says, Expression contains unexpanded parameter packs 'Args' and 'args'. Why?
The
...has to go inside the function call parentheses:Obviously, that won’t work for your function that takes only a single argument, so you need to find a way to expand the calls into a function call or other allowed construct:
Note the use of the comma operator to turn the
voidreturn ofprint_oneinto a value suitable to place in an argument list or initializer expression.The initializer-list forms are preferred to the function call forms, as they are (supposed to be) ordered LTR which function call arguments are not.
The forms where a parameter pack expansion can occur are covered by 14.5.3 [temp.variadic]:
Your original code is illegal because although textually it might appear that it should produce a statement consisting of a number of comma-operator expressions, that is not a context allowed by 14.5.3:4.