'Is there an std view for random access iterators, as std::span is for contiguous iterators?

Is there a standard view for random access iterators, as std::span is for contiguous iterators?

It is a piece of cake to implement such thing, but I ask before reinvent the wheel. I don't see something like that on cppreference.

c++


Solution 1:[1]

std::span is a pointer and a size (or equivalently two pointers). It doesn't need to do type erasure.

A random_access_view would have the overhead of type erasure, so I don't expect one to be standardised.

Instead I would suggest a template constrained to std::ranges::random_access_range, possibly in conjunction with std::ranges::viewable_range if you need a view specifically.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Caleth