使用 std::function 作为参数的可变参数模板

Variadic templates using std::function as parameter

我怎样才能简化下面的代码。是否可以在这里使用元组?如果是,你能解释一下吗?

template<typename Out, typename T1, typename T2, typename T3, typename T4>
void ProcessIncomingCommand(PClientContext pClientContext,
    DWORD & bytesProcessed,
    const std::function<Out(T1, T2, T3, T4)> &function,
    const std::vector<UINT> &params);

template<typename Out, typename T1, typename T2, typename T3>
static void ProcessIncomingCommand(PClientContext pClientContext,
    DWORD & bytesProcessed,
    const std::function<Out(T1, T2, T3)> &function,
    const std::vector<UINT> &params);

template<typename Out, typename T1, typename T2>
static void ProcessIncomingCommand(PClientContext pClientContext,
    DWORD & bytesProcessed,
    const std::function<Out(T1, T2)> &function,
    const std::vector<UINT> &params);

在模板实现中,需要使用 UINTstd::vector 传递的参数调用函数。参数可能不同,因此需要将它们转换为适当的类型。

auto resFromFunction= function(params.at(0),
    params.at(1),
    static_cast<T3>(params.at(2)),
    static_cast<T4>(params.at(3)));

如何使用 log0 在这里回答?

template<typename Out, typename... T>static void ProcessIncomingCommand(PClientContext pClientContext,
DWORD & bytesProcessed,
const std::function<Out(T...)> &function,
const std::vector<UINT> &params)

编译成功。

template<typename Out, typename... T>
static void ProcessIncomingCommand(PClientContext pClientContext,
    DWORD & bytesProcessed,
    const std::function<Out(T...)> &function,
    const T&... params) { function(params...); }

int main()
{
  PClientContext p;
  DWORD d = 0.5;
  std::function<int(double, int, char)> f;
  double a;
  int b;
  char c;
  ProcessIncomingCommand(p, d, f, a, b, c);
}

如果你想将参数作为元组传递,那就更棘手了: 参见 How do I expand a tuple into variadic template function's arguments? 当然,除非您将 function 更改为采用元组而不是参数列表...

不确定...但我想您需要一个函数助手和 std::index_sequence(或类似的东西)。

一个可能的例子

template <typename Out, typename ... Ts, std::size_t ... Is>
static Out PIC_helper (
   PClientContext pClientContext,
   DWORD & bytesProcessed,
   const std::function<Out(Ts...)> &function,
   const std::vector<UINT> &params,
   std::index_sequence<Is...> const &)
 { return function( static_cast<Ts>(params.at(Is))... ); }

template <typename Out, typename ... Ts>
static void ProcessIncomingCommand (
   PClientContext pClientContext,
   DWORD & bytesProcessed,
   const std::function<Out(Ts...)> &function,
   const std::vector<UINT> &params)
 {
   Out resFromFunction
      = PIC_helper(pClientContext, bytesProcessed, function, params,
                   std::make_index_sequence<sizeof...(Ts)>());

   // other ...
 }

注意 std::index_sequencestd::make_index_sequence() 是 C++14 特性;但如果您需要 C++11 解决方案,您可以轻松地创建一些东西来替代它们。

看起来你想要这样的东西

template<typename ResultType, std::size_t... I>
ResultType tuple_from_vector_impl(std::vector<UINT> params, std::index_sequence<I...>)
{
    return std::make_tuple(static_cast<decltype(std::get<I>(std::declval<ResultType>()))>(params[I])...);
}

template<typename... Args, typename Indices = std::index_sequence_for<Args...>>
std::tuple<Args...> tuple_from_vector(std::vector<UINT> params)
{
    return tuple_from_vector_impl<std::tuple<Args...>>(params, Indices{});
}

template<typename Out, typename ... Args>
void ProcessIncomingCommand(PClientContext pClientContext,
                            DWORD & bytesProcessed,
                            const std::function<Out(Args...)> &function,
                            const std::vector<UINT> &params)
{
    // preamble
    std::tuple<Args...> args = tuple_from_vector<Args...>(params);
    Out result = std::apply(function, args);
    // postamble
}

template<typename ... Args>
void ProcessIncomingCommand(PClientContext pClientContext,
                            DWORD & bytesProcessed,
                            const std::function<void(Args...)> &function,
                            const std::vector<UINT> &params)
{
    // preamble
    std::tuple<Args...> args = tuple_from_vector<Args...>(params);
    std::apply(function, args);
    // postamble
}