#include <iostream>
#include <functional>
#include <tuple>
#include <iostream>
using namespace std::placeholders;
// simple function to be called
double foo_fn(int x, float y, double z)
{
double res = x + y + z;
std::cout << "foo_fn called. x = " << x << " y = " << y << " z = " << z
<< " res=" << res;
return res;
}
// helpers for tuple unrolling
template<int ...> struct seq {};
template<int N, int ...S> struct gens : gens<N-1, N-1, S...> {};
template<int ...S> struct gens<0, S...>{ typedef seq<S...> type; };
// invocation helper
template<typename FN, typename P, int ...S>
double call_fn_internal(const FN& fn, const P& params, const seq<S...>)
{
return fn(std::get<S>(params) ...);
}
// call function with arguments stored in std::tuple
template<typename Ret, typename ...Args>
Ret call_fn(const std::function<Ret(Args...)>& fn,
const std::tuple<Args...>& params)
{
return call_fn_internal(fn, params, typename gens<sizeof...(Args)>::type());
}
int main(void)
{
// arguments
std::tuple<int, float, double> t = std::make_tuple(1, 5, 10);
// function to call
std::function<double(int, float, double)> fn = foo_fn;
// invoke a function with stored arguments
call_fn(fn, t);
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8ZnVuY3Rpb25hbD4KI2luY2x1ZGUgPHR1cGxlPgojaW5jbHVkZSA8aW9zdHJlYW0+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOjpwbGFjZWhvbGRlcnM7CgovLyBzaW1wbGUgZnVuY3Rpb24gdG8gYmUgY2FsbGVkCmRvdWJsZSBmb29fZm4oaW50IHgsIGZsb2F0IHksIGRvdWJsZSB6KQp7CiAgIGRvdWJsZSByZXMgPSAgeCArIHkgKyB6OwogICBzdGQ6OmNvdXQgPDwgImZvb19mbiBjYWxsZWQuIHggPSAiIDw8IHggPDwgIiB5ID0gIiA8PCB5IDw8ICIgeiA9ICIgPDwgegoJICAgICAgICAgPDwgIiByZXM9IiA8PCByZXM7CiAgIHJldHVybiByZXM7Cn0KCi8vIGhlbHBlcnMgZm9yIHR1cGxlIHVucm9sbGluZwp0ZW1wbGF0ZTxpbnQgLi4uPiBzdHJ1Y3Qgc2VxIHt9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