是否可以以某种方式生成模板参数包?
我有以下代码在工作:
zip<0,1,2>.expand(c);我的目标是在编译时生成列表0,1,2,因为它将与各种模板一起使用,例如:
zip<make_sequence<3>>.expand(c);我需要在编译时生成这一点,因为展开触发一些模板化函数,根据层/过滤器类型进行优化,这样我就可以启动其他代码了。这背后的想法是能够确定在编译时生成的层或过滤器的列表,并删除一些ifs (和其他情况),因为这将用于un环境(并且在关键路径中)。
这是在这个类中(简化的版本):
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);
}
};我还尝试了以下方面提出的解决办法:
How do I generate a variadic parameter pack?
但他们都没有为我工作。我知道这个错误:
错误:>“模板”模板参数列表中参数1处的类型/值不匹配 错误:期望一个‘int’类型的常量,得到‘make_integer_sequence’
有什么建议吗?非常感谢!!
发布于 2015-06-10 20:11:36
你需要一个帮手:
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>());
}发布于 2015-06-10 20:21:06
除了@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
https://stackoverflow.com/questions/30765901
复制相似问题