#ifndef DOTPROD_HPP #define DOTPROD_HPP #include #include template auto dotprod(const A& a, const B& b) -> decltype( std::end(a), std::end(b), *std::begin(a) + *std::begin(a) * *std::begin(b) ) { using T = decltype(*std::begin(a) * *std::begin(b)); auto it1 = std::begin(a); auto it2 = std::begin(b); T sum{}; for(;;) { bool end1 = it1 == std::end(a); bool end2 = it2 == std::end(b); assert(end1 == end2); if (end1) break; auto val1 = *it1++; auto val2 = *it2++; sum += val1 * val2; } return sum; } #endif