Martin Perry Martin Perry - 1 month ago 10
C++ Question

C++ - make tupple from variadic template

I have C++11 code with variadic template class

template <typename R, typename ...ARGS>
class MyFunction
{
}


I have a methods defined like this

template <typename T, typename T2>
void Callback()
{
MyFunction<T, T2> * fptr12 = (MyFunction<T, T2> *)f;
fptr12->args = std::make_tuple(GetInput<T2>());
fptr12->call();
}

template <typename T, typename T2, typename T3>
void Callback()
{
MyFunction<T, T2, T3> * fptr12 = (MyFunction<T, T2, T3> *)f;
fptr12->args = std::make_tuple(GetInput<T2>(), GetInput<T3>());
fptr12->call();
}


f
=> defined as interface
IFunction
in a dictionary (it is retrieved from dictionary based on other parametrs, but correct function is always returned, so it can be casted)

I want to make method
Callback
universal for any number of arguments (Tx), so it can work with any
MyFunction
. I can add variadic template to it, but I dont know, how to generate tupple.

Answer

You just need to expand the variadic argument list:

template <typename T, typename... Tn>
void Callback() {
    MyFunction<T, Tn...>* fptr = static_cast<MyFunction<T, Tn...>*)(&f);
    fptr->args = std::make_tuple(GetInput<Tn>()...);
    fptr->call();
}