std::ranges...e_view::end(3)

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

πŸ’… std::ranges::slide_view::end(3) β€” the C++ Standard Library one

okay so bestie, buckle up, because this is the std::ranges::slide_view::end page πŸŽ€ β€” straight out of the C++ Standard Library, and it's honestly kind of a serve once you get past the underscores. it's giving "end iterator, but make it fashion" ✨


🌸 SYNOPSIS

no arguments, just vibes and constexpr:

constexpr auto end()
    requires (!(__simple_view<V> && __slide_caches_nothing<const V>));
(1) (since C++23)
constexpr auto end() const
(2) (since C++23)
    requires __slide_caches_nothing<const V>;

so there are literally two overloads, both constexpr, both since C++23 🌈 and both of them hand you a sentinel or an iterator that represents the end of the slide_view. that's the whole job. icon behaviour honestly.


✨ DESCRIPTION

🫢 Overload (1)

let base_ and n_ be the underlying data members (yes bestie, they're private, don't touch). this overload is equivalent to a little four-way vibe check:

  • if V models __slide_caches_nothing, return iterator<false>(ranges::begin(base_) + ranges::range_difference_t<V>(size()), n_);.
  • otherwise, if V models __slide_caches_last, return iterator<false>(ranges::prev(ranges::end(base_), n_ - 1, ranges::begin(base_)), n_);.
  • otherwise, if V models common_range, return iterator<false>(ranges::end(base_), ranges::end(base_), n_);.
  • otherwise, return sentinel(ranges::end(base_));.

and here's the tea β˜• β€” if V models __slide_caches_last, this function caches the result inside the slide_view::cached_end_ for use on subsequent calls. why? because that's literally necessary to provide the amortized constant-time complexity that the range is required to have. no cap, the caching is load-bearing πŸ’€

πŸ¦‹ Overload (2)

the const one, and it's short and sweet. equivalent to:

begin() + ranges::range_difference_t<const V>(size())

periodt. that's it. that's the whole overload πŸ’œ


πŸŽ€ PARAMETERS

  • (none) β€” not a single parameter, bestie. zero. nada. she takes nothing πŸ™ˆ

πŸ‘‘ RETURN VALUE

a sentinel or an iterator that represents the end of the slide_view ⭐ β€” same deal as above, just restated because the standard library loves a callback.


πŸ”₯ EXAMPLE

run this code, it's cute:

// Run this code

#include <iostream>
#include <ranges>

int main()
{
    static constexpr auto source = {'A', 'B', 'C', 'D'};

    for (const auto subrange: source | std::views::slide(3))
    {
        std::cout << "[ ";
        for (auto it = subrange.begin(); it != subrange.end(); ++it)
            std::cout << *it << ' ';
        std::cout << "]\n";
    }
}

Output:

[ A B C ]
[ B C D ]

look at those sliding windows go 😭 sliding so gracefully. main character energy fr fr 🌸


🌈 SEE ALSO

  • begin β€” returns an iterator to the beginning (C++23) (public member function)
  • ranges::begin β€” returns an iterator to the beginning of a range (C++20) (customization point object)
  • ranges::end β€” returns a sentinel indicating the end of a range (C++20) (customization point object)

πŸ’– tiny reminder bestie: those double-underscore names like __slide_caches_nothing, __slide_caches_last, __simple_view and __slide_caches_last are implementation-reserved β€” admire them from a distance, don't go writing them in your own code, iykyk 🫢

and that's the page!! shoutout to cppreference.com, dated 2024.06.10 β€” the source and version that made this whole little writeup possible πŸ’…βœ¨ go slide responsibly, mwah πŸŽ€

πŸ“¦ where this page comes from