Understanding the Filter View to Use It Right
summary
C++20 filter_view caches the first matching iterator so initial begin can be amortized constant but later calls become cheap, which creates hidden state: modifying elements that make them fail the predicate or concurrently calling begin/empty/front yields undefined behavior. Templates can fail to accept piped filters unless you use forwarding references or wrap with std::ranges::subrange or std::views::all. C++23 adds owning view fixes; measured microbenchmarks show a manual cache (reverse+drop trick) can be much faster for repeated iterations. Use filters carefully, prefer single-pass or explicit caching.
AI-written summary · about
chapters (8)
- 0:00Filter views in C++20
- 8:59How filter views work
- 26:55Performance issues with views
- 39:50Caching in filter views
- 51:43Solutions for view limits
- 1:02:28Is caching worth it?
- 1:16:33Constness and ownership in views
- 1:23:45Safe use of filter views
related talks
01h 36mC++ Standard Views
01h 10mSafe and Readable Code - Monadic Operations in C++23
39mWhy Iterators Got It All Wrong
01h 25mC++23 Ranges: Conceptual Changes and Useful Practicalities
57mRange Algorithms, Views and Actions: A Comprehensive Guide
56mWhat a View! Building Your Own (Lazy) Range Adaptors (part 2 of 2)
23mA Modern C++ Point of View of Programming in Image Processing
01h 02mC++20 - My Favourite Code Examples
