1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
#ifndef DOTPROD_HPP
#define DOTPROD_HPP

#include <cassert>
#include <iterator>

template<typename A, typename B>
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