将函数参数类型提取为参数包

时间:2015-06-18 22:17:44

标签: c++ c++11 function-pointers variadic-templates

这是"unpacking" a tuple to call a matching function pointer的后续问题,该问题询问如何以通用方式将std::tuple的值作为参数提供给函数。给出的解决方案如下:

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;
};

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};
   std::cout << saved.delayed_dispatch() << std::endl;
}

我的问题是,是否可以制作save_it_for_later的备用版本,仅以foo作为模板参数,因此我们不必提供{{ 1}}&#39; s参数类型作为模板参数(或将其返回类型烘焙到foo)。像

这样的东西
save_it_for_later

我可以使用某种宏包装int main(void) { ... save_it_for_later2<foo> saved = {t}; ... } 来提取所需的类型:

foo

这个问题似乎与原始问题完全正交,以保证自己的票。

2 个答案:

答案 0 :(得分:2)

#include <tuple>
#include <utility>

template <typename> struct save_it_for_later_t;
template <typename Result, typename... Args>
struct save_it_for_later_t<Result (*)(Args...)> {
    std::tuple<Args...>   params;
    Result              (*fun)(Args...);
    template <typename... Params>
    save_it_for_later_t(Result (*fun)(Args...), Params&&... params)
        : params(std::forward<Params>(params)...)
        , fun(fun) {
    }
    // ... 
};
template <typename Result, typename... Args, typename... Params>
save_it_for_later_t<Result(*)(Args...)>
save_it_for_later(Result (*fun)(Args...), Params&&... params) {
    return save_it_for_later_t<Result(*)(Args...)>(fun, std::forward<Params>(params)...);
}

double foo(float, float, double);
int main() {
    auto saved = save_it_for_later(foo, 1.2f, 3.4f, 5.6);
    // ...
}

答案 1 :(得分:0)

我只是羞怯地发现我去年曾问过一个类似的问题(Unpacking arguments of a functional parameter to a C++ template class),这也引出了答案:

#include <functional>
#include <iostream>
#include <tuple>

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;
};

double foo(int x, float y, double z)
{
   return x + y + z;
}

template<typename T>
struct save_it_for_later;

template <typename Result, typename... Args>
struct save_it_for_later<Result(Args...)>
{
   std::tuple<Args...> params;
   Result (*func)(Args...);

   Result delayed_dispatch()
   {
    return callFunc(typename gens<sizeof...(Args)>::type());
   }

   template<int ...S>
   Result 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<decltype(foo)> saved = {t, foo};
   std::cout << saved.delayed_dispatch() << std::endl;
}