【问题标题】:Calling a template with template parameters from a std::integer_sequence从 std::integer_sequence 调用带有模板参数的模板
【发布时间】:2021-07-23 15:38:11
【问题描述】:

挠头。鉴于我有以下整数序列:

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

非常感谢您的帮助!

【问题讨论】:

  • 这很不清楚“我也不能使用参数包,因为我已经在同一上下文中为其他事情使用了参数包。”请发帖minimal reproducible example

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


【解决方案1】:

你可以写一个帮助模板,例如

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

然后称它为

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

LIVE

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-13
    • 2014-09-08
    • 2011-08-25
    • 1970-01-01
    • 2016-04-16
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    相关资源
    最近更新 更多