std::ranges...ccess_range(3)
hey bestie π this is the std::ranges...ccess_range(3) page from the C++ Standard Libary manual β yes, that exact spelling, we donβt gatekeep typos here π«Ά
πΈ NAME πΈ
std::ranges::random_access_range - std::ranges::random_access_range
β¨ Synopsis β¨
Defined in header <ranges>
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. Itβs giving βrange but make it random-accessβ energy π
π¦ Example π¦
// Run this code
#include <array>
#include <deque>
#include <list>
#include <ranges>
#include <set>
#include <valarray>
#include <vector>
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>>
);
}
Not me staring at these static_asserts like theyβre the vibe check π RAR is literally the whole concept in one tiny alias, and the example runs it against all those containers. Iconic.
π See also π
ranges::sized_rangeβ specifies that a range knows its size in constant time β(C++20)(concept)ranges::contiguous_rangeβ specifies a range whose iterator type satisfiescontiguous_iteratorβ(C++20)(concept)
mwah π source: http://cppreference.com β’ version/date: 2024.06.10 β’ page: std::ranges...ccess_range(3) β periodt, bestie πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms