Martin Perry Martin Perry - 1 year ago 89
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>());

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>());

=> defined as interface
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
universal for any number of arguments (Tx), so it can work with any
. I can add variadic template to it, but I dont know, how to generate tupple.

Answer Source

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>()...);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download