Advanced Ranges: Writing Modular, Clean, and Efficient Code with Custom Views
summary
Shows how custom C++20/23 views cut allocations and copies and can be plugged into pipelines using the range adapter closure (C++23) pattern. Demonstrates a sliding window median implemented with a deque and a stack-allocated circular array that avoids per-window heap allocations and yields up to >10x speedup for 30k elements versus the standard slide-based view. Covers iterator trait correctness, when to use value vs reference semantics, how laziness affects determinism, and practical gotchas that broke composition until fixed.
AI-written summary · about
chapters (23)
- 0:00Introduction and motivation
- 1:16Ranges overview and benefits
- 3:43Views, lazy evaluation and examples
- 8:18Composability with ranges
- 10:44Lazy vs eager evaluation
- 14:57Why build custom views
- 16:20Sliding window median problem
- 20:31Skip every n element demo
- 22:37Custom view boilerplate
- 28:54Sentinels and iterator design
- 30:40Range adapter closure
- 33:08Composition pitfalls
- 33:48Performance optimizations
- 36:35Circular array implementation
- 39:03Managing external state
- 43:34Custom view for state
- 46:46When to use custom views
- 47:51Performance optimization results
- 48:12Iterator traits and requirements
- 49:17External state by reference
- 50:19Best practices and pitfalls
- 51:21Gotchas and debugging tips
- 51:52Q&A and closing
