【发布时间】:2021-10-29 08:17:31
【问题描述】:
This 已经是一个很好的答案,但是,当我尝试对作为 typename 的参数(不知道确切的词)执行相同操作时,它们传递了任意数量的参数作为参数,例如:
int sum=0;
int func()
{
return sum;
}
template <int first, int ... rest>
int func()
{
sum += first;
return func(rest...); //Error C2660 'func': function does not take 4 arguments
/*
return func<rest...>(); this also doesn't work: Error 'int func(void)': could not deduce template argument for 'first' and 'func': no matching overloaded function found
*/
}
int main()
{
cout << func<1,2,3,4,5>();
}
为什么会出现错误?有没有可能的修复?另外,我需要将参数作为类型名而不是参数传递。
【问题讨论】:
标签: c++ recursion variadic-templates variadic-functions template-meta-programming