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

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

✨ std::ranges::elements_view::iterator::operator[] ✨

okay bestie, welcome to the C++ Standard Libary page for this iterator’s operator[] β€” yes, that’s the manual name, and yes, we’re keeping the spelling exactly as it appears up top. this is the C++20 era, so hydrate and let’s go πŸ’…


πŸŽ€ NAME πŸŽ€

std::ranges::elements_view::iterator::operator[] β€” std::ranges::elements_view::iterator::operator[]

periodt, that’s the name and the one-liner both 🫢


πŸ¦‹ Synopsis πŸ¦‹

the signature, verbatim, because we do not mess with signatures:

constexpr decltype(auto) operator[]( difference_type n ) const

(since C++20)

requires ranges::random_access_range<Base>;

it returns an element at specified relative location, as if by /*get-element*/(this->base() + n), where for an expression e, /*get-element*/(e) is:

  • std::get<N>(*e), if ranges::range_reference_t<Base> is a reference type.
  • Otherwise, static_cast<E>(std::get<N>(*e)), where E is std::remove_cv_t<std::tuple_element_t<N, ranges::range_reference_t<Base>>>.

🌸 Parameters 🌸

  • n β€” position relative to current location

πŸ’– Return value πŸ’–

The element at displacement n relative to the current location. that’s it, that’s the tea β˜•


πŸ“ Example πŸ“

// Run this code

#include <iostream>
#include <ranges>
#include <string_view>
#include <tuple>

int main()
{
    using T = std::tuple<int, char, std::string_view>;

    const auto il =
    {
        T{1, 'A', "Ξ±"},
        T{2, 'B', "Ξ²"},
        T{3, 'C', "Ξ³"},
    };

    std::cout << std::views::elements<0>(il)[1] << ' '   // 2
              << std::views::elements<1>(il)[1] << ' '   // B
              << std::views::elements<2>(il)[1] << '\n'; // Ξ²
}

⭐ Output ⭐

2 B Ξ²

πŸ‘‘ See also πŸ‘‘

  • operator* β€” accesses the N^th tuple element (C++20) (public member function)

source/version + date, as promised: http://cppreference.com Β· 2024.06.10 Β· std::ranges...:operator[](3) β€” mwah, go slay those tuple elements πŸ’‹βœ¨

πŸ“¦ where this page comes from