dimanche 28 août 2016

Transform typelist with function at runtime

I have a typelist. I would like to create a tuple with the results of calling a function on each type in that list and then use that as arguments to another functor. So something like this:

template<typename F>
struct function_traits;

template<typename T, typename R, typename... Args>
struct function_traits<R(T::*)(Args...) const> {
    using return_type = R;
    using param_types = std::tuple<Args...>;
};

template<typename T> struct function_traits : public
function_traits<decltype(&T::operator())> {};

template <typename T>
T* get_arg(int id);

template <typename Func>
void call_func(Func&& func, int id)
{
    using param_types = function_traits<Func>::param_types>;

    func(*get_arg<param_types>(id)...);
}

call_func([](int& a, char& b) { }, 3);

That doesn't actually compile though since param_types is a tuple: "there are no parameter packs available to expand". What I would like to have happened though is to invoke:

func(*get_arg<int>(id), *get_arg<char>(id));

And to have that work for any number of arguments. Is there any way to get that expansion to work?

Aucun commentaire:

Enregistrer un commentaire