std::ranges...ccess_range(3)

πŸ“¦
version 2024.07.05 Β· 3 versions
, in nixpkgs unstable, opensuse-leap 16.0

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 satisfies contiguous_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