Iteration Revisited
Tristan Brindle
“Safety” is the word on everyone’s lips at the moment. Unfortunately for C++ programmers, one of our most fundamental abstractions — iterators — are fraught with danger . Prone to out-of-bounds memory accesses, unexpected invalidation and dangling, iterators are a UB minefield in which even experts can find themselves in trouble.
Fortunately there is something we can do about it.
In this talk we’ll look at an alternative, safer abstraction for iterating over sequences and introduce Flux, a new C++20 library implementing these ideas. We’ll see how Flux retains all of the power and flexibility of the existing STL, but greatly reduces the potential for UB through careful design and implementation choices — all while offering compatibility with existing code.
We’ll also take a look at performance and examine the cost of universal bounds checking. Spoiler: it’s probably a lot less than you’d think. In fact, we’ll see how Flux code can actually outperform equivalent C++20 Ranges code in some common situations.
Finally, we’ll take a quick look at some other quality-of-life improvements that Flux brings.
If you’re interested in the STL and the ranges and algorithms in C++20 and are looking for an easy-to-use way of making your codebase more resilient, then this is the talk for you.
Tristan Brindle
Tristan Brindle is a C++ consultant and trainer based in London. With over 15 years C++ experience, he started his career working in high-performance computing in the oil industry in Australia before returning home to his native UK in 2018. He is an active member of the ISO C++ Standards Committee (WG21) and the BSI C++ Panel. He is a regular speaker at C++ conferences around the world, and is a director of C++ London Uni, a non-profit organisation offering free introductory programming classes in London and online.