Using the filter view in practice
summary
The C++20 filter view works by wrapping a range and evaluating the predicate on iteration so the very first call to begin can be linear and implementations often use begin caching to make subsequent begins cheap. That caching plus inconsistent const propagation (ref vs owning view for temporaries) and non‑atomic begin modifications yield real surprises: undefined behavior when predicate breaks (mutating an element so it no longer matches), surprising compile errors for const, and thread‑unsafe runtime bugs. Concrete fixes: avoid const iteration, avoid concurrent begins, or copy into an owning view.
AI-written summary · about
chapters (10)
- 0:00C++20 views introduction
- 1:06Filter view basics
- 6:04How views work
- 17:05Performance of filter views
- 28:32Constraints and caching
- 37:10Const breaks filter views
- 44:46Pitfalls with filter views
- 51:22Concurrency issues
- 56:43Best practices
- 59:57Conclusion
related talks
06mLightning Talk: Unlocking More Ways To Use Range-Based For
01h 04mC++ Concepts
01h 01mBack to Basics: Concepts in C++
01h 03mTemplates Made Easy with C++20
01h 02mDiscovering a User-Facing Concept
01h 11mRanges++: Are Output Range Adaptors the Next Iteration of C++ Ranges?
01h 02mDiscovering a User-Facing Concept
01h 02mUniversal/Forwarding References
