I am not very familiar with variadic function templates, and I'm afraid that I have never seen an example in which the parameter pack is not at the end. This means that you can only process the first (or first N) variadic arguments, and then unpack the rest, whereas you want to unpack all the variadic arguments other than the last argument. I gave it a try:
Code:
#include <iostream>
void test(int& a, int& b)
{
std::cout << "end: " << a << ", " << b << std::endl;
}
template <typename T>
void test(int& a, int& b, T& c)
{
test(a, b);
std::cout << c << std::endl;
}
template <typename T, typename... Ts>
void test(int& a, int& b, Ts&... ts, T& c)
{
test(a, b, ts...);
std::cout << c << std::endl;
}
int main()
{
int a = 3;
int b = 5;
int c = 6;
int d = 4;
test(a, b, c, d);
return 0;
}
but it failed. You may want to change your requirements so you can write this instead:
Code:
#include <iostream>
void test(int& a, int& b)
{
std::cout << "end: " << a << ", " << b << std::endl;
}
template <typename T>
void test(int& a, int& b, T& c)
{
test(a, b);
std::cout << c << std::endl;
}
template <typename T, typename... Ts>
void test(int& a, int& b, T& c, Ts&... ts)
{
test(a, b, ts...);
std::cout << c << std::endl;
}
int main()
{
int a = 3;
int b = 5;
int c = 6;
int d = 4;
test(a, b, d, c);
return 0;
}
(Notice that c and d are swapped in the call in main.)