我正在尝试编写一个可变参数模板 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;
}
不幸的是,它在尝试解析 f<3,>()
error C2668: 'f': ambiguous call to重载函数> 打电话.
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;
}
但此代码也无法编译(消息 error C2912: explicit specialization 'int f(void)' is not a specialization of a function template
).
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...>());
}
这也不起作用:error 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...>();
}
这篇关于如何编写可变参数模板递归函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!