[英]How to create function-template, that expands parameter-pack which doesn't represent function argument(s)?
[英]Iterate in reverse order through the parameter-pack of a variadic template function
我试图通过可变参数模板 function 的参数包以相反的顺序迭代。 我的想法是使用尾递归和一个专门的“空”模板 function 来停止递归:
#include <iostream>
template<>
void f() {}
template<int H, int... T>
void f()
{
f<T...>();
std::cout << H << std::endl;
}
int main()
{
f<1,2,3,4,5>();
return 0;
}
但是上面的代码无法编译:
p.cc:25:8: error: 'f' is not a template function void f() {} ^ p.cc: In instantiation of 'void f() [with int H = 5; int...T = {}]': p.cc:30:12: recursively required from 'void f() [with int H = 2; int...T = {3, 4, 5}]' p.cc:30:12: required from 'void f() [with int H = 1; int...T = {2, 3, 4, 5}]' p.cc:36:18: required from here p.cc:30:12: error: no matching function for call to 'f()' f<T...>(); ^ p.cc:28:6: note: candidate: template<int H, int...T> void f() void f() ^ p.cc:28:6: note: template argument deduction/substitution failed: p.cc:30:12: note: couldn't deduce template parameter 'H' f<T...>();
我觉得这只是一个语法错误——但我自己无法找到解决方案。 任何想法?
因为您应该在专业化之前提供模板声明:
#include <iostream>
template<typename...> void f();
template<>
void f() {}
template<int H, int... T>
void f()
{
f<T...>();
std::cout << H << std::endl;
}
int main()
{
f<1,2,3,42,5>();
return 0;
}
这里我们 go: https://ideone.com/TZal7p
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.