c++ - 将元组的部分解包为函数参数
问题描述
想象一下,我有tuple<...>
几种类型。而且我想将元组的值部分扩展为具有静态参数的函数的参数 - 即不一定是可变参数,以便函数参数应该与元组部分匹配。我怎样才能做到这一点 ?
解决方案
不确定您的问题到底是什么(您下次可以显示一些代码吗?)。但是您可以使用结构化绑定来“解包”这样的元组:
#include <tuple>
#include <iostream>
void f(int x, double y)
{
std::cout << x << ", " << y << "\n";
}
template<typename... args_t>
void g(const std::tuple<args_t...>& args)
{
// C++17
//const auto [x, y] = args;
// C++11
auto x = std::get<0>(args);
auto y = std::get<1>(args);
f(x, y);
}
int main()
{
auto tuple = std::make_tuple( 1,3.14159265 );
g(tuple);
}
推荐阅读
- java - 如何使用 Java NetBeans 改组图像
- opencart - opencart 1.5.6.4 版本小更新
- javascript - 如何在 Vue js 中将 FileReader 用于两个或多个输入字段?
- python - 如何创建一个包含单引号和双引号 python 的字符串?
- scala - Spark:mapPartitions 对象不可序列化
- html - 调整表格中单元格的大小
- sql - 具有唯一行集的左外连接
- python - 3D 条形图中的 LEGEND 作为颜色图
- angular - 重新加载时 angular + nginx + docker 路由配置问题
- excel - Excel中的区域独立数字连接