I looked at this SO question and I could not understand how the answer worked. I will send a copy of the code in one of the answers for reference:
template<int ...> struct seq {}; // How does this line work? template<int N, int ...S> struct gens : gens<N-1, N-1, S...> {}; template<int ...S> struct gens<0, S...>{ typedef seq<S...> type; }; double foo(int x, float y, double z) { return x + y + z; } template <typename ...Args> struct save_it_for_later { std::tuple<Args...> params; double (*func)(Args...); double delayed_dispatch() { return callFunc(typename gens<sizeof...(Args)>::type()); } template<int ...S> double callFunc(seq<S...>) { return func(std::get<S>(params) ...); } }; int main(void) { std::tuple<int, float, double> t = std::make_tuple(1, 1.2, 5); save_it_for_later<int,float, double> saved = {t, foo}; cout << saved.delayed_dispatch() << endl; }
The part I don't understand is this:
template<int N, int ...S> struct gens : gens<N-1, N-1, S...> {};
In the example in return callFunc(typename gens<sizeof...(Args)>::type());
I assume that sizeof..(Args)
will be 3
. So,
template<int N, int ...S> struct gens : gens<N-1, N-1, S...> {};
becomes
template<3, {}> struct gens : gens<3-1, 3-1, {}> {};
Is this correct, and if so, what comes from there?