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
call
function properly, and second, you should ensure that the template parameter packArgs
is 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_cast
to access the memberm
from thecall
function's base class(A<Args...>)
.#include <iostream>
to usestd::cout
in thefoo
function.With these changes, the code should now correctly unpack the template variable parameters in the
foo
function as follows:This ensures that each instantiation of the
A
class contributes its memberm
to the parameter list of thefoo
function in the correct order. I hope this helps you.After the edit: In this version, I added a
callHelper
function inside theA
class template, which recursively unpacks the template parameters and calls thefoo
function. This should resolve the compilation error you encountered. I appreciate your understanding, and I hope this helps!