首页 > 解决方案 > 从 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

高度赞赏帮助!

标签: c++templatesc++17template-meta-programming

解决方案


您可能会编写一个帮助模板,例如

template<typename T, T... I>
auto helper(std::integer_sequence<T, I...>)
{
    myFunction<I...>();
}

然后将其称为

helper(std::integer_sequence<int,0,1,2>{});

居住


推荐阅读