std::ranges...::chunk_view(3)

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

πŸŽ€ NAME πŸŽ€

std::ranges::chunk_view::chunk_view β€” okay bestie, this is the constructor for std::ranges::chunk_view::chunk_view, straight outta the C++ Standard Libary man pages. Yes, the name is doubled, it’s giving β€œI have two names and I’m using both,” but we respect it. πŸ’…


✨ Synopsis ✨

The signature is literally this, no cap:

constexpr explicit chunk_view( V base, ranges::range_difference_t<V> n );

(since C++23) β€” we love a modern era.

So what does it do? It constructs a chunk_view, and it initializes the underlying data members like the good little constructor it is:

  • move constructs base_ with std::move(base),
  • initializes the "chunk size" n_ with n.

And plot twist 🫒 β€” in addition, if V models exactly the input_range, the constructor also initializes these exposition-only data members:

  • initializes remainder_ with 0,
  • default-initializes current_.
🚩 Behavior check, bestie: the behavior is undefined if n is less than or equal to 0. That is not a vibe. Do not do it.

🌸 Parameters 🌸

  • base β€” the adapted view. The thing being adapted, the main character of this operation.
  • n β€” the chunk size. How many elements per chunk, periodt.

πŸ’» Example πŸ’»

Run this code, it’s cute:

// Run this code

#include <algorithm>
#include <iostream>
#include <ranges>

int main()
{
    auto i = std::views::iota(0, 10);
    auto w = std::ranges::chunk_view(i, 4);

    std::ranges::for_each(w, [](auto const v)
    {
        for (auto e : v)
            std::cout << e << ' ';
        std::cout << '\n';
    });
}

πŸ“ Output πŸ“

It prints this, slay:

0 1 2 3
4 5 6 7
8 9

That’s the whole page, bestie 🫢 β€” source: http://cppreference.com, dated 2024.06.10, for std::ranges...::chunk_view(3). mwah πŸ’‹βœ¨

πŸ“¦ where this page comes from