如何编写可变参数模板的递归函数? [英] How to write a variadic template recursive function?
问题描述
我正在尝试编写可变参数模板 constexpr
函数,该函数计算给定模板参数的总和。这是我的代码:
I'm trying to write a variadic template constexpr
function which calculates sum of the template parameters given. Here's my code:
template<int First, int... Rest>
constexpr int f()
{
return First + f<Rest...>();
}
template<int First>
constexpr int f()
{
return First;
}
int main()
{
f<1, 2, 3>();
return 0;
}
不幸的是,它不会编译并报告错误消息错误C2668: f:尝试解析
的模糊调用。 f< 3,>()
调用时对重载函数
Unfortunately, it does not compile reporting an error message error C2668: 'f': ambiguous call to overloaded function
while trying to resolve f<3,>()
call.
我还尝试将递归基本情况更改为接受0个模板参数,而不是1个:
I also tried to change my recursion base case to accept 0 template arguments instead of 1:
template<>
constexpr int f()
{
return 0;
}
但此代码也无法编译(消息错误C2912:显式专业化'int f(void)'不是函数模板的专业化
)。
But this code also does not compile (message error C2912: explicit specialization 'int f(void)' is not a specialization of a function template
).
我可以提取第一个和第二个模板使其编译和工作的参数,例如:
I could extract first and second template arguments to make this compile and work, like this:
template<int First, int Second, int... Rest>
constexpr int f()
{
return First + f<Second, Rest...>();
}
但这似乎不是最佳选择。因此,问题是:如何以一种简洁的方式编写此计算?
But this does not seem to be the best option. So, the question is: how to write this calculation in an elegant way?
UP :我也尝试将其作为单个函数编写:
UP: I also tried to write this as a single function:
template<int First, int... Rest>
constexpr int f()
{
return sizeof...(Rest) == 0 ? First : (First + f<Rest...>());
}
这也不起作用:错误C2672: 'f':找不到匹配的重载函数
。
推荐答案
您的基本情况错误。您需要为空列表添加一个大小写,但是正如编译器建议的那样,您的第二次尝试不是有效的模板专业化。为零参数定义有效实例化的一种方法是创建一个接受空列表的重载
Your base case was wrong. You need a case for the empty list, but as the compiler suggests, your second try was not a valid template specialization. One way to define a valid instantiation for zero arguments is to create an overload that accepts an empty list
template<class none = void>
constexpr int f()
{
return 0;
}
template<int First, int... Rest>
constexpr int f()
{
return First + f<Rest...>();
}
int main()
{
f<1, 2, 3>();
return 0;
}
编辑:出于完整性考虑还有我的第一个答案,即@ alexeykuzmin0通过添加条件来修复:
for completeness sake also my first answer, that @alexeykuzmin0 fixed by adding the conditional:
template<int First=0, int... Rest>
constexpr int f()
{
return sizeof...(Rest)==0 ? First : First + f<Rest...>();
}
这篇关于如何编写可变参数模板的递归函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!