具有多种类型的递归模板函数

htz*_*fun 6 c++ recursion templates

我正在尝试编写计算两个向量的标量积的函数.这是代码,它的工作原理.

    template <int N> 
    int scalar_product (std::vector<int>::iterator a, 
                        std::vector<int>::iterator b) {
        return (*a) * (*b) + scalar_product<N - 1>(a + 1, b + 1);
    }

    template <>
    int scalar_product<0>(std::vector<int>::iterator a,
                          std::vector<int>::iterator b) {
        return 0;
    }
Run Code Online (Sandbox Code Playgroud)

但这里是问题 - 我想用模板类型替换这个迭代器,这样函数的签名看起来就像这样

    template <typename Iterator ,int N> 
    int scalar_product (Iterator a, Iterator b) {
        return (*a) * (*b) + scalar_product<N - 1>(a + 1, b + 1);
    }

    template <typename Iterator>
    int scalar_product<0>(Iterator a,
                          Iterator b) {
        return 0;
    }
Run Code Online (Sandbox Code Playgroud)

但这不起作用 - 我得到编译错误C2768:非法使用显式模板参数.这看起来很傻,但我无法找出应该改变什么来避免这个错误.

gwi*_*rrr 5

实际上,你不必使用类型 - 我发现它们非常繁琐,而且它们的语义也不同.您不能部分地专门化一个函数,但是您可以通过提供默认参数值来重载它们并使它们像特化一样运行:

#include <type_traits>

template <typename Iterator>
int scalar_product(Iterator a, Iterator b, std::integral_constant<int, 0> = std::integral_constant<int, 0>()  ) 
{
    return 0;
}

template <int N, typename Iterator> 
int scalar_product (Iterator a, Iterator b, std::integral_constant<int, N> = std::integral_constant<int, N>() ) 
{
    return (*a) * (*b) + scalar_product(a + 1, b + 1, std::integral_constant<int, N-1>() );
}

int foo()
{
    int a[] = { 1, 2, 3, 4 };
    int b[] = { 1, 1, 1, 1 };
    return scalar_product<4>(a, b); // returns 10
}
Run Code Online (Sandbox Code Playgroud)


Nim*_*Nim 4

(AFAIK)不支持函数模板的部分专业化,要获得此功能,您需要稍微不同地做,例如:

template <int N>
struct scalar
{
  template <typename Iterator>
  static int product(Iterator a, Iterator b)
  { (*a) * (*b) + scalar<N - 1>::product(a + 1, b + 1); }
};

template <>
struct scalar<0>
{
  template <typename Iterator>
  static int product(Iterator a, Iterator b)
  { return 0; }
};
Run Code Online (Sandbox Code Playgroud)