Given the following code:
void foo(int x, int y, int z, int u)
{
}
template<typename... Args> class A;
template<> class A<>{};
template<typename T, typename... Args>
class A<T, Args...> : public A<Args...>
{
public:
A(){}
A(T a, Args... v) : m(a), A<Args...>(v...)
{
}
void call()
{
foo(m, A<V>::m...);
}
T m;
};
A<int, int, int> a{1, 2, 3};
a.call();
How do I unpack the template variable params in the function foo as follows?
foo(m, A<int, int>::m, A<int>::m);
However the code above unpacks the params as:
foo(m, A<int>::m, A<int>::m);
It seems a couple of issues in your code need to be addressed. First, you must unpack the template arguments in the
callfunction properly, and second, you should ensure that the template parameter packArgsis consistently used throughout your code.Here is the corrected version of your code (corrected version of my corrected version of your code):
Changes made:
static_castto access the membermfrom thecallfunction's base class(A<Args...>).#include <iostream>to usestd::coutin thefoofunction.With these changes, the code should now correctly unpack the template variable parameters in the
foofunction as follows:This ensures that each instantiation of the
Aclass contributes its membermto the parameter list of thefoofunction in the correct order. I hope this helps you.After the edit: In this version, I added a
callHelperfunction inside theAclass template, which recursively unpacks the template parameters and calls thefoofunction. This should resolve the compilation error you encountered. I appreciate your understanding, and I hope this helps!