Recursive template function with multiple types

Viewed 1234

I'm trying to write function that counts scalar product for two vectors. Here is the code and it works.

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

But here is the problem - i want to replace this iterators with template type, so that signature of function will look smth like this

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

But this doesn't work - I get compile error C2768: illegal use of explicit template arguments. It seems silly, but I couldn't find out what should I change to avoid this error.

2 Answers
Related