调用函数时生成隐式元组
generate an implicit tuple while calling a function
我想调用一个模板函数并将两个参数集作为元组传递。但是在将其作为参数传递之前,调用此函数始终需要使用 std::make_tuple
手动构建元组。
示例:
template < typename ... OUT, typename ... IN>
void Command( std::tuple<OUT...>, std::tuple<IN...>)
{
}
int main()
{
// Send Parameter
uint8_t mode;
uint16_t addr;
// Receive Parameter
uint16_t neighbour0;
uint16_t neighbour1;
Command( std::make_tuple( mode, addr ),
std::make_tuple( neighbour0, neighbour1 ));
}
是否有任何 chance/trick 删除函数调用中的 std::make_tuple
以便我可以编写如下内容:
Command( {mode, addr}, {neighbour0, neighbour1} );
不,但是你可以用这样的怪物来节省一点打字时间:
template<class...Ts>
auto _(Ts&&...ts)
{
return std::make_tuple(std::forward<Ts>(ts)...);
}
...
Command(_(mode, addr),
_(neighbour0, neighbour1));
通常的警告适用 - 仅仅因为我们可以,并不意味着我们应该...
如果写法
Command(mode, addr)(neighbour0, neighbour1);
是可以接受的,Command()
可以 return 本质上是一个首先具有绑定的函数对象 std::tuple<...>
,它将在接收到其他参数时调用实际函数。那就是实现将遵循
template <typename... Out, typename... In>
void realCommand(std::tuple<Out...>, std::tuple<In...>);
template <typename... Out>
auto Command(Out&&... out) {
return [&](auto&&... in){
realCommand(std::make_tuple(std::forward<Out>(out)...),
std::make_tuple(std::forward<decltype(in)>(in)...));
}
}
我想调用一个模板函数并将两个参数集作为元组传递。但是在将其作为参数传递之前,调用此函数始终需要使用 std::make_tuple
手动构建元组。
示例:
template < typename ... OUT, typename ... IN>
void Command( std::tuple<OUT...>, std::tuple<IN...>)
{
}
int main()
{
// Send Parameter
uint8_t mode;
uint16_t addr;
// Receive Parameter
uint16_t neighbour0;
uint16_t neighbour1;
Command( std::make_tuple( mode, addr ),
std::make_tuple( neighbour0, neighbour1 ));
}
是否有任何 chance/trick 删除函数调用中的 std::make_tuple
以便我可以编写如下内容:
Command( {mode, addr}, {neighbour0, neighbour1} );
不,但是你可以用这样的怪物来节省一点打字时间:
template<class...Ts>
auto _(Ts&&...ts)
{
return std::make_tuple(std::forward<Ts>(ts)...);
}
...
Command(_(mode, addr),
_(neighbour0, neighbour1));
通常的警告适用 - 仅仅因为我们可以,并不意味着我们应该...
如果写法
Command(mode, addr)(neighbour0, neighbour1);
是可以接受的,Command()
可以 return 本质上是一个首先具有绑定的函数对象 std::tuple<...>
,它将在接收到其他参数时调用实际函数。那就是实现将遵循
template <typename... Out, typename... In>
void realCommand(std::tuple<Out...>, std::tuple<In...>);
template <typename... Out>
auto Command(Out&&... out) {
return [&](auto&&... in){
realCommand(std::make_tuple(std::forward<Out>(out)...),
std::make_tuple(std::forward<decltype(in)>(in)...));
}
}