c++ - 从 std::integer_sequence 调用带有模板参数的模板
问题描述
抓着我的头。鉴于我有以下整数序列:
std::integer_sequence<int,0,1,2>
我有以下模板:
template<int a, int b, int c> void myFunction() {}
有没有办法以整数序列作为模板参数调用模板?
myFunction<std::integer_sequence<int,0,1,2>>();
这不编译
我在这里找到了一些关于堆栈溢出的示例,如何将整数序列作为函数参数传递,但不幸的是,这不是我的选择。我也不能使用参数包,因为我已经将参数包用于同一上下文中的其他内容。
我正在使用 C++17
高度赞赏帮助!
解决方案
您可能会编写一个帮助模板,例如
template<typename T, T... I>
auto helper(std::integer_sequence<T, I...>)
{
myFunction<I...>();
}
然后将其称为
helper(std::integer_sequence<int,0,1,2>{});
推荐阅读
- jupyter-notebook - 创建 dask 本地集群并在不同的 Jupiter notebook 之间共享
- python - 如何使任务与 UTC 时间同步并每 6 小时运行一次 discord.py
- javascript - 在 javascript 数组中添加和删除函数 (React-Hooks)
- windows - 在 MSYS2 中更新软件包时 PGP 签名无效 - 尽管已修复
- mysql - 如何解决将数据插入表中的错误
- python - 如何在 Python 中计算优先矩阵?
- firebase - 如何使用 Getx 获取/检索数组 Firestore 并在颤振上显示
- r - 将行转换为列并汇总
- javascript - 未找到 React / Webpack 本地图像
- c# - Visual Studio 2019 安装程序在解压后崩溃