如何编写可变参数模板递归函数?

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;
}

不幸的是,它在尝试解析 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...>();
}

这篇关于如何编写可变参数模板递归函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How do compilers treat variable length arrays(编译器如何处理变长数组)
Deduce template argument from std::function call signature(从 std::function 调用签名推导出模板参数)
check if member exists using enable_if(使用 enable_if 检查成员是否存在)
Standard Library Containers with additional optional template parameters?(具有附加可选模板参数的标准库容器?)
Uses of a C++ Arithmetic Promotion Header(C++ 算术提升标头的使用)
Parameter pack must be at the end of the parameter list... When and why?(参数包必须位于参数列表的末尾...何时以及为什么?)