fork(2) download
  1. #include <array>
  2. #include <iostream>
  3. #include <numeric>
  4. #include <type_traits>
  5. #include <utility>
  6.  
  7. template <typename T, typename U>
  8. using ProductType = decltype(std::declval<T>() * std::declval<U>());
  9.  
  10. template <size_t N, typename T = double>
  11. using Vector = std::array<T,N>;
  12.  
  13. template <size_t N, typename T, typename U>
  14. ProductType<T,U> dotproduct(const Vector<N, T>& t, const Vector<N, U>& u)
  15. {
  16. return std::inner_product(t.begin(), t.end(), u.begin(), ProductType<T,U>());
  17. }
  18.  
  19. int main()
  20. {
  21. const Vector<3, short> v1 { 1, 2, 3 };
  22. const Vector<3, signed char> v2 { 1, 2, 3 };
  23. auto res = dotproduct(v1, v2);
  24.  
  25. static_assert(std::is_same<int, decltype(res)>::value, "Unxpected return type");
  26.  
  27. std::cout << res << std::endl;
  28. }
Success #stdin #stdout 0s 4200KB
stdin
Standard input is empty
stdout
14