Can range-v3 iota_view iterator be random access for 64 bit initial value?

48 Views Asked by At

It appears that the range-v3 iota_view iterator is not random access when you use a 64 bit type for the initial value. It works ok for gcc iota_view. It works for range-v3 with a 32 bit initial value. Here is a simple example in compiler explorer: https://godbolt.org/z/dEG7aKnoc. It uses gcc 13 and trunk for range-v3. I also list the code below.

Is there a way to make it be random access?

#if 1
// range v3
#include <range/v3/all.hpp>
namespace rng = ::ranges;

#else
#include <ranges>
namespace rng = std::ranges;
#endif

int main()
{

    // works
    // int base = 100;

    // does not work
    //std::size_t base = 100;
    //long base = 100;
    ssize_t base = 100;

    static_assert(std::random_access_iterator<decltype(rng::begin(rng::views::iota(base)))>);
}
1

There are 1 best solutions below

0
rscohn2 On

A local C++ expert had the answer. Use rng::random_access_iterator instead of std::random_access_iterator.

I believe the issue here is that the difference type for this range std::ranges::__detail::__max_diff_type is not a signed integral type. I think this will go away if you use rng::random_access_iterator. (i.e. range-v3 uses a weird 128-bit difference_type for iota with 64-bit integers, and this is not recognized as a valid difference type by the standard library) (libstdc++ uses a 64-bit difference type and just doesn’t worry about the potential for overflow/underflow)