C++11 生成模板参数

C++11 Generate template arguments

是否可以以某种方式生成模板参数包?

我有以下代码在工作:

zip<0,1,2>.expand(c);

我的目标是在编译时生成列表 0,1,2,因为它将与可变参数模板一起使用,例如:

zip<make_sequence<3>>.expand(c);

我需要在编译时生成它,因为 expand 会触发一些模板化函数,这些函数会根据 Layer/Filter 类型进行优化,以便我可以启动其他代码。 这背后的想法是能够确定在编译时生成的层或过滤器列表,并删除一些 ifs(和其他情况),因为这将在非 HPC 环境中使用(并且在关键路径内)。

里面是这个class(简体版):

template<class... Layer>
class TRAV{  
  template <int...CS> struct zip{
    static void expand(int c){
      constexpr int b = sizeof...(Layers); 
      expand2((TRAV::path<CS,b,typename Layers::MONAD_TYPE>(c),0)...);
    }
  template<typename...IS>
    static void expand2(IS&&...) {
    }
 };
 void call(int c){ 
  zip<0,1,2>.expand(c); 
 }
};

我还尝试了在以下位置提出的解决方案:

Implementation C++14 make_integer_sequence

How do I generate a variadic parameter pack?

但其中 none 对我有用。我收到此错误:

error: type/value mismatch at argument 1 in template parameter list for >‘template

error: expected a constant of type ‘int’, got ‘make_integer_sequence’

有什么建议吗? 非常感谢!!

你需要一个帮手:

template<int... Seq>
void call(int c, std::integer_sequence<int, Seq...>){
    zip<Seq...>::expand(c);
}

void call(int c){ 
    call(c, std::make_integer_sequence<int, 3>());
}

除了@T.C的解决方案。已经表明,您还可以使类似于 zip<make_sequence<3>> 的东西起作用。它看起来像这样:

apply_indices<zip<>,std::make_index_sequence<3>>

这样一个助手的实现是:

#include <utility>

template<typename,typename> struct apply_indices_impl;

template<typename T,template<T...> class C,T... Ns>
struct apply_indices_impl<C<>,std::integer_sequence<T,Ns...>>
{
    using type = C<Ns...>;
};

template<typename T,typename I>
using apply_indices = typename apply_indices_impl<T,I>::type;

Live example