std::ranges...view::begin(3)

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

πŸ’… std::ranges::slide_view::begin

ok so this is a page straight out of the C++ Standard Library manual (yes, that one β€” cppreference energy, group-chat edition) and the star of the show is std::ranges::slide_view::begin πŸŽ€

Fair warning bestie: the header up top is literally the title again (std::ranges::slide_view::begin, if you were wondering), so like… yeah. It's giving "name so nice they said it twice." 🌸


✨ NAME ✨

std::ranges::slide_view::begin β€” and yeah, that's it, that's the name. std::ranges::slide_view::begin. The whole thing. No cap. πŸ’…


πŸ¦‹ Synopsis πŸ¦‹

okay so there are two overloads and they both showed up in C++23, and both are constexpr. Here's the exact shape, copy-paste energy:

constexpr auto begin()
    requires (!(__simple_view<V> && __slide_caches_nothing<const V>));

(1) (since C++23)

constexpr auto begin() const
    requires __slide_caches_nothing<const V>;

(2) (since C++23)

What this function does: it returns an iterator to the first element of the slide_view. Simple queen behaviour. πŸ‘‘

Now for the receipts β€” here's what each overload is equivalent to:

1) If V models __slide_caches_first, it's equivalent to:

return iterator<false>(
           ranges::begin(base_),
           ranges::next(ranges::begin(base_), n_ - 1,
ranges::end(base_)),
           n_
       );

Otherwise, it's equivalent to: return iterator<false>(ranges::begin(base_), n_);

And ooh, this part matters: if V models __slide_caches_first, this function caches the result within the slide_view::cached_begin_ for use on subsequent calls. Why, you ask? Because that's necessary to provide the amortized constant-time complexity that the range requires. So the cache is not optional, it's the whole point. ⭐

2) Equivalent to: return iterator<true>(ranges::begin(base_), n_);


πŸ’œ Parameters πŸ’œ

  • (none) β€” yeah, nothing. Empty. Nada. She takes no arguments, she's booked and busy. 🐝

πŸ”₯ Return value πŸ”₯

An iterator to the first element of slide_view, which points to the n_-sized subrange of the possibly const-qualified underlying view type β€” that's V for overload (1), or const V for overload (2). ✨

Like, which one you get depends on which overload you called. Keep that straight and you're slaying. πŸ’…


🌈 Example 🌈

// Run this code β€” go on, run it, I'll wait πŸ“

#include <iostream>
#include <ranges>
#include <string_view>
using namespace std::literals;

int main()
{
    static constexpr auto source = {"βˆ€x"sv, "βˆƒy"sv, "Ξ΅"sv, "Ξ΄"sv};
    auto view{std::ranges::slide_view(source, 2)};
    const auto subrange{*(view.begin())};
    for (std::string_view const s : subrange)
        std::cout << s << ' ';
    std::cout << '\n';
}

🌟 Output 🌟

βˆ€x βˆƒy

bestie look at those little logic symbols go 😭 absolutely iconic.


πŸ‘€ See also πŸ‘€

  • end β€” returns an iterator or a sentinel to the end (C++23) (public member function)
  • operator== β€” compares a sentinel with an iterator returned from slide_view::begin (C++23) (function)

ok that's the whole page!! 🫢 big shoutout to http://cppreference.com for hosting it, version 2024.06.10, and to the man page itself living its best life under the very cursed-but-iconic name std::ranges...view::begin(3) (yes, with the three dots, don't @ me πŸ’€). mwah, go read more man pages, ily πŸ§šπŸ’–

πŸ“¦ where this page comes from