std::ranges...::operator[](3)
okay so we're diving into std::ranges...::operator[](3) from the C++ Standard Libary β and yes, that's the manual name, spelled exactly like that in the header, no cap π
. This page is all about the default operator[] for views, and it's lowkey iconic.
π NAME π
std::ranges::view_interface::operator[] β and because the manual said it twice, we're saying it twice: std::ranges::view_interface::operator[]. It's giving "redundant but make it fashion." π
πΈ Synopsis πΈ
Here's the exact signature tea, verbatim with all its funky line breaks:
template<ranges::random_access_range R = D>
constexpr decltype(auto) operator[]( ranges::range_difference_t<R>
(1) (since C++20)
n );
template<ranges::random_access_range R = const D>
constexpr decltype(auto) operator[]( ranges::range_difference_t<R>
(2) (since C++20)
n ) const;
The default implementation of this operator[] member function obtains the element at the specified offset relative to the beginning iterator, and it does that by reusing the operator[] of the iterator type. Cute, very on brand. π¦
- Let
derivedbestatic_cast<D&>(*this). Equivalent toreturn ranges::begin(derived)[n];. - Same as (1), except that
derivedisstatic_cast<const D&>(*this).
π Parameters π
nβ position of the element to return. That's the whole parameter list, bestie. π
β¨ Return value β¨
The element at offset n relative to the beginning iterator. Slay. π«Ά
π₯ Notes π₯
In C++20, no type derived from std::ranges::view_interface in the standard library provides their own operator[] member function. Not a single one. π
However, these following derived types cannot use the default implementations, because they never satisfy random_access_range:
std::ranges::basic_istream_viewstd::ranges::filter_viewstd::ranges::join_viewstd::ranges::lazy_split_viewstd::ranges::split_view
The inherited operator[] member function is available for std::ranges::empty_view, but a call to it always results in undefined behavior.
π¨ undefined behavior alert: like, do not call it on std::ranges::empty_view. That's not a vibe check you want to fail. π
π Example π
This section is incomplete. Reason: no example. The audacity. π
No example yet, so we're all just out here vibing with the signatures. π§
β Category β
- Todo no example. Yes, that's the category. It's giving "we'll get to it." π
Source/version check: http://cppreference.com, dated 2024.06.10, and the page is std::ranges...::operator[](3). Bye bestie, go index responsibly! πβ¨
π¦ 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