在调用函数时生成隐式元组
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 ));
}
有没有机会/技巧删除函数调用中的std::make_tuple
,这样我就可以写这样的东西:
Command( {mode, addr}, {neighbour0, neighbour1} );
如果符号
Command(mode, addr)(neighbour0, neighbour1);
如果是可接受的,Command()
基本上可以返回一个具有绑定的第一个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)...));
}
}
没有,但你可以用这样一个怪物来节省一点打字:
template<class...Ts>
auto _(Ts&&...ts)
{
return std::make_tuple(std::forward<Ts>(ts)...);
}
Command(_(mode, addr),
_(neighbour0, neighbour1));
通常的警告适用-仅仅因为我们可以,并不意味着我们应该…
相关文章:
- C++:TypeDef使用元组
- Pybind11:将元组列表从Python传递到C++
- 重载元组索引运算符-C++
- 在C++中,如何通过几种类型从元组中选择多个元素
- 将fold表达式与std::一起用于两个元组
- std::ranges::elements_view,用于自定义类似元组的数据
- 将元组的向量转换/构造为堆
- 有没有办法扩展和调用 std::functions 的元组?
- 从可调用可变参数元组中的函数结果创建元组
- 从类型元组到通过调用模板函数返回的值数组
- 如何部分专业化功能以用元组元素作为参数调用功能
- 有没有办法在函数以编程方式返回的引用上多次调用函数? -元组动态访问
- 为什么GCC警告使用STD ::元组和虚拟继承来调用非平凡的动作分配运算符
- 对元组的每个元素进行泛型调用成员函数
- 在元组中迭代和调用异构函数
- 从可调用元组的结果创建元组
- 在调用函数时生成隐式元组
- 模板元组-在每个元素上调用一个函数
- "解包"元组以调用匹配函数指针
- 对一个对象上的每个元组元素调用函数,不进行递归