Mutating Through a Filter -- Barry Revzin
Nico Josuttis gave a talk recently that included an example and I wanted to explain what’s going on in this example, what the issue is, and what (if anything) is broken.
Mutating Through a Filter
by Barry Revzin
From the article:
As with a lot of my explanations, we have to start from the beginning.
The C++ iterator model has a number of iterator categories: input, forward, bidirectional, random access, and (since C++20) contiguous. This post only needs to consider the first two.
An input range (a range whose iterator is an input iterator) is a single-pass range. You can only ever call
begin()one time on it. You can’t have multiple different input iterators into the same range - incrementing one immediately invalidates any existing copies.

Registration is now open for CppCon 2023! The conference starts on October 1 and will be held
Registration is now open for CppCon 2023! The conference starts on October 1 and will be held
C language was defined to cover a large range of computer architectures, including many which would be considered museum relics today. It therefore takes a very conservative view of what is permitted, so that it remains possible to write C programs for those ancient systems. (Which weren’t quite so ancient at the time.)
Registration is now open for CppCon 2023! The conference starts on October 1 and will be held
Registration is now open for CppCon 2023! The conference starts on October 1 and will be held