std::ranges::random_access_range

From cppreference.com
< cpp‎ | ranges
 
 
Ranges library
Range access
Range conversions
(C++23)
Range primitives



Dangling iterator handling
Range concepts
Views

Factories






Adaptors
Range adaptor objects
Range adaptor closure objects
Helper items
 
Defined in header <ranges>
template< class T >

  concept random_access_range =

    ranges::bidirectional_range<T> && std::random_access_iterator<ranges::iterator_t<T>>;
(since C++20)

The random_access_range concept is a refinement of range for which ranges::begin returns a model of random_access_iterator.

Example

#include <ranges>
#include <vector>
#include <array>
#include <deque>
#include <valarray>
#include <list>
#include <set>
 
template<typename T> concept RAR = std::ranges::random_access_range<T>;
 
int main()
{
    int a[4];
    static_assert(
            RAR<std::vector<int>> and
            RAR<std::vector<bool>> and
            RAR<std::deque<int>> and
            RAR<std::valarray<int>> and
            RAR<decltype(a)> and
        not RAR<std::list<int>> and
        not RAR<std::set<int>> and
            RAR<std::array<std::list<int>,42>>
    );
}

See also

specifies that a range knows its size in constant time
(concept)
specifies a range whose iterator type satisfies contiguous_iterator
(concept)