ericniebler / range-v3

Range library for C++14/17/20, basis for C++20's std::ranges

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Inconsistent behaviour of ranges::distance with ranges::view::zip using infinite views.

tremmelg opened this issue · comments

Calling ranges::distance with a ranges::zip_view that has an infinite view component gives different results depending on the specific infinite view.

#include <range/v3/all.hpp>
#include <vector>
#include <iostream>

int main() {
  std::vector<int> input(1024);

  auto cycle = ranges::view::cycle(input);
  auto zip_cycle = ranges::view::zip(input, cycle);

  std::cout << "Cycle: " << ranges::distance(zip_cycle) << "\n";

  auto repeat = ranges::view::repeat(42);
  auto zip_repeat = ranges::view::zip(input, repeat);

  std::cout << "Repeat: " << ranges::distance(zip_repeat) << "\n";

  auto iota = ranges::view::iota(0);
  auto zip_iota = ranges::view::zip(input, iota);

  std::cout << "Iota: " << ranges::distance(zip_iota) << "\n";

  auto generate = ranges::view::generate([]() { return 42; });
  auto zip_generate = ranges::view::zip(input, generate);

  std::cout << "Generate: " << ranges::distance(zip_generate) << "\n";
}

Compiling the above example against 2ff4cf2 and running it gives:

% ./zip_distance
Cycle: 0
Repeat: 0
Iota: 1024
Generate: 1024