1. Problem It Solves
Different grouping tasks need different movement rules. C++23 adds fixed non-overlapping chunks, overlapping slides, predicate-defined adjacent groups, and regular strides through a range. The goal is to make the relevant rule visible in code instead of relying on an assumption about what “C++23 support” means.
2. Prerequisites
Day 43: overlapping adjacent windows.
Predicates and lazy range adaptors.
3. Core Idea
chunk cuts a loaf, slide moves a window, chunk_by keeps neighbors together while a relation holds, and stride samples every nth position. Read the syntax from left to right, identify the value, object, or range being transformed, and then check its resulting type, lifetime, or ownership. Standardization and implementation are separate: a C++23 mode may still lack one library component, so a feature-test macro is part of responsible portable use.
4. Minimal Syntax
auto groups = range | std::views::chunk(3);5. How It Works
The sample builds the smallest expression or object needed for Chunk, Slide, Chunk-By, and Stride Views.
It applies all four grouping or sampling rules to one small fixed sequence when supported. The compiler and library apply the relevant rule before the program observes the result.
The program prints or checks visibly different groups, windows, runs, and sampled elements, making the important behavior easy to verify.
6. Common Mistakes
A zero chunk size or stride violates preconditions; a
chunk_bypredicate must describe the intended relation between adjacent elements rather than a global bucket test.Enabling C++23 mode without checking the relevant feature macro may select code that the installed compiler or standard library does not implement yet.
7. When to Use It
Use it when the task involves batching, rolling windows, run grouping, and regular down-sampling.
Avoid it when the task involves random access patterns or grouping that depends on non-adjacent global state.
8. Simple Example
A sensor stream is chunked for upload, slid for moving analysis, grouped by status runs, and sampled every second reading. The downloadable program keeps the data fixed so the output can be compared without entering input.
Complete sample code
Source file
cpp23/44_chunk_slide_chunk_by_stride/main.cpp
#include <array>
#include <iostream>
#include <ranges>
#include <version>
int main() {
#if defined(__cpp_lib_ranges_chunk) && defined(__cpp_lib_ranges_slide) && \
defined(__cpp_lib_ranges_chunk_by) && defined(__cpp_lib_ranges_stride)
std::array values{1, 3, 2, 4, 5, 7};
for (auto group : values | std::views::chunk(2))
std::cout << '[' << *group.begin() << "] ";
std::cout << '\n';
for (auto window : values | std::views::slide(3))
std::cout << *window.begin() << ' ';
std::cout << '\n';
auto same_parity = [](int a, int b) { return a % 2 == b % 2; };
for (auto run : values | std::views::chunk_by(same_parity))
std::cout << *run.begin() << ' ';
std::cout << '\n';
for (int value : values | std::views::stride(2))
std::cout << value << ' ';
std::cout << '\n';
#else
std::cout << "C++23 grouping views unavailable\n";
#endif
}
9. Key Takeaways
Separate the language rule from compiler and library availability.
Keep lifetime, ownership, and deduced types visible when they affect correctness.
Prefer the smallest syntax that communicates the intent.
Guard facilities that are not yet uniformly implemented.
10. Self-Check Questions
Easy — What is the smallest syntax in Section 4, and what main job does it perform?
Medium — Read the sample program: which value, type, or branch is observed, and why?
Hard — Why can
chunk_by(eq)split equal values correctly only when equal values are adjacent, and what happens to a later equal value after a different one?