ericniebler / range-v3

Range library for C++14/17/20, basis for C++20's std::ranges
Other
4.05k stars 437 forks source link

Fix zip/zip_with distance_to #1776

Closed PkXwmpgN closed 1 year ago

PkXwmpgN commented 1 year ago

The distance between iterators is wrongly calculated when the first zipped range is empty:

    std::vector<int> a = {1, 2, 3, 4, 5};
    std::vector<int> b;

    auto z0 = zip(b, a);
    auto z1 = zip(a, b);

    auto d0 = ranges::distance(ranges::begin(z0), ranges::end(z0));
    auto d1 = ranges::distance(ranges::begin(z1), ranges::end(z1));

    assert(d0 == 0); // failed d0 == 5
    assert(d1 == 0); // pass

With MSVC

    std::vector<int> a = {1, 2, 3, 4, 5};
    std::vector<int> b; 
    auto v = zip(b, a) | ranges::to_vector; // access violation
brevzin commented 1 year ago

Thanks for the fix!