std::ranges...:operator[](3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… std::ranges...:operator[](3) πŸ’…

okay bestie, this page is from the C++ Standard Libary manual (yes, that's how the header spells it β€” we're keeping the typo, no cap 🫢). It's the std::ranges::adjacent_view::iterator::operator[] page, and it's giving C++23 iterator energy.

🌸 NAME 🌸

std::ranges::adjacent_view::iterator::operator[] -
std::ranges::adjacent_view::iterator::operator[]

literally that's the name: std::ranges::adjacent_view::iterator::operator[] twice with a hyphen in the middle. slay.

✨ Synopsis ✨

okay so this operator[] has been a thing since C++23, and it only exists when this constraint is satisfied:

          constexpr auto operator[]( difference_type n ) const  (since C++23)
              requires ranges::random_access_range<Base>;

It returns an element at specified relative location. Let current_ be an underlying array of iterators. Equivalent to:

        return __tuple_transform([&](auto& i) -> decltype(auto) { return i[n];
       }, current_);

πŸŽ€ Parameters πŸŽ€

  • n β€” position relative to current location. that's it, that's the whole parameter πŸ’…

πŸ’– Return value πŸ’–

The element at displacement n relative to the current location. periodt. ✨

πŸ¦‹ Example πŸ¦‹

run this code, bestie:

       // Run this code

        #include <ranges>
        #include <tuple>

        int main()
        {
            constexpr static auto v = {0, 1, 2, 3, 4, 5};
            //                               β””β”€β”€β”¬β”€β”€β”˜
            //                                  └─────────────────┐
            constexpr auto view = v | std::views::adjacent<3>; // β”‚
            //                 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
            //                 β”‚                β”Œβ”€β”€β”΄β”€β”€β”
            static_assert(view[2] == std::tuple{2, 3, 4});
        }

the static_assert says view[2] == std::tuple{2, 3, 4} and it passes, no cap πŸ”₯

πŸ’Œ sourced from http://cppreference.com, version dated 2024.06.10, page std::ranges...:operator[](3) β€” bye bestie, stay random-access πŸ’œβœ¨
πŸ“¦ where this page comes from