ranges/zipviewempty.cpp

The following code example is taken from the book
C++23 - The Complete Guide by Nicolai M. Josuttis, Leanpub, 2026
The code is licensed under a Creative Commons Attribution 4.0 International License. Creative Commons License

// raw code

#include <iostream>
#include <array>
#include <ranges>

int innerProduct(auto&&... rgs)
{
  int result = 0;
  for (auto tpl : std::views::zip(rgs...))
    std::apply([&](auto&&... vals) {
                 result += (1 * ... * vals);
               },
               tpl);
  return result;
}

int main()
{
  std::cout << innerProduct(std::array{1, 2, 3},
                            std::array{4, 5, 6}) << '\n';  // 32
  std::cout << innerProduct() << '\n';                     // 0
}