有的时能会需要将json的array转换成tuple,然后再通过apply传递给函数的参数:
#include <nlohmann/json.hpp>
#include <string>
#include <iostream>
#include <tuple>
using json = nlohmann::json;
using namespace std;
template<size_t... I>
auto j2t(const json& j, index_sequence<I...>)
{
return make_tuple(j[I]...);
}
template<size_t N>
auto make_tuple_from_json_array(const json& j)
{
return j2t(j, make_index_sequence<N>());
}
template<typename R, typename... Args>
R call_func_with_json(R(*func)(Args...), const json& j)
{
auto t = make_tuple_from_json_array<sizeof...(Args)>(j);
return apply(func, t);
}
auto f1(int p1, double p2, string p3)
{
cout<<"this is f1:&