私は2つのベクトルのスカラ積を数える関数を書くことを試みています。 ここにコードがあり、それは動作します。複数の型を持つ再帰的テンプレート関数
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;
}
しかし、ここでは問題である - 私は、関数のシグネチャは、この
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;
}
のようになめらかになりますように、テンプレートの種類と、このイテレータを交換したい。しかし、これは動作しません - 私がコンパイル取得しますエラーC2768:明示的なテンプレート引数の不正使用。愚かなようですが、私はこのエラーを避けるために何を変えるべきかを知ることができませんでした。
この参照:http://stackoverflow.com/questions/3716799/partial-specialization-を – Nim
['std :: inner_product'](http://en.cppreference.com/w/cpp/algorithm/inner_product)を使わないのはなぜですか?ループをインライン展開することはとても重要ですか? – gwiazdorrr
@Nimリンクありがとう - それは助けました。 – htzfun