如何简化以下代码。是否可以在这里使用元组?如果是,可以解释一下。
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> ¶ms);
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> ¶ms);
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> ¶ms);
在模板实现中需要使用std::vector
UINT
传递的参数来调用函数。参数可能不同,因此需要将它们转换为适当的类型。
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> ¶ms)
答案 0 :(得分:2)
不确定......但我想你需要一个函数助手和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> ¶ms,
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> ¶ms)
{
Out resFromFunction
= PIC_helper(pClientContext, bytesProcessed, function, params,
std::make_index_sequence<sizeof...(Ts)>());
// other ...
}
观察std::index_sequence
和std::make_index_sequence()
是C ++ 14的功能;但如果您需要C ++ 11解决方案,您可以轻松创建替代它们的东西。
答案 1 :(得分:0)
编译。
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);
}
如果你想将params作为一个元组传递,那将会更棘手:
见How do I expand a tuple into variadic template function's arguments?
当然,如果你改变function
来取一个元组而不是一个参数列表......
答案 2 :(得分:0)
看起来你想要像
这样的东西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> ¶ms)
{
// 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> ¶ms)
{
// preamble
std::tuple<Args...> args = tuple_from_vector<Args...>(params);
std::apply(function, args);
// postamble
}