range-v3 icon indicating copy to clipboard operation
range-v3 copied to clipboard

chunk_view<input_range>'s outer iterator calculates distance incorrectly

Open timsong-cpp opened this issue 3 years ago • 0 comments

    // synthesize a sized-sentinel input view
    std::vector v{1,2,3,4,5};
    ranges::any_view<int&, ranges::category::input> x = v;
    auto civ = ranges::views::counted(x.begin(), 5);

    auto cciv = civ | ranges::views::chunk(2);
    for(auto i = cciv.begin(); ; ++i) {
        if (i == cciv.end()){
            std::cout << "end " << cciv.end() - i << '\n';
            break;
        }
        auto inner = *i; 
        for(auto j = inner.begin() ; j != inner.end(); ++j){
            std::cout << *j << ' ' << cciv.end() - i << '\n';
        }
        std::cout << "inner end " << cciv.end() - i << '\n';
    }

prints

1 3
2 3
inner end 2
3 2
4 2
inner end 1
5 1
inner end 0
end 1

It looks like the result is wrong

  • when the inner range is iterated to completion but before the outer iterator has been incremented; and
  • when the outer iterator is past-the-end.

timsong-cpp avatar Sep 13 '21 06:09 timsong-cpp