Persistent Iterators with Value Semantics

2026-04-15Programming Languages

Programming Languages
AI summary

The authors introduce a new type of iterator called persistent iterators that combine the easy-to-use style of iteration in languages like C++ with the safety of immutable data structures found in functional programming. These persistent iterators create a snapshot of the container when made, which prevents common problems like invalidation and unintended changes. They built a library called LibFPP, offering these persistent containers with similar performance to standard ones. Their work helps programmers write safer code without losing the familiar way of working with iterators.

iteratorpersistent data structuresimmutable datacontainerC++Standard Template Library (STL)iterationmutationaliasingdata races
Authors
Yihe Li, Gregory J. Duck
Abstract
Iterators are a fundamental programming abstraction for traversing and modifying elements in containers in mainstream imperative languages such as C++. Iterators provide a uniform access mechanism that hides low-level implementation details of the underlying data structure. However, iterators over mutable containers suffer from well-known hazards including invalidation, aliasing, data races, and subtle side effects. Immutable data structures, as used in functional programming languages, avoid the pitfalls of mutation but rely on a very different programming model based on recursion and higher-order combinators rather than iteration. However, these combinators are not always well-suited to expressing certain algorithms, and recursion can expose implementation details of the underlying data structure. In this paper, we propose persistent iterators -- a new abstraction that reconciles the familiar iterator-based programming style of imperative languages with the semantics of persistent data structures. A persistent iterator snapshots the version of its underlying container at creation, ensuring safety against invalidation and aliasing. Iterator operations operate on the iterator-local copy of the container, giving true value semantics: variables can be rebound to new persistent values while previous versions remain accessible. We implement our approach in the form of LibFPP -- a C++ container library providing persistent vectors, maps, sets, strings, and other abstractions as persistent counterparts to the Standard Template Library (STL). Our evaluation shows that LibFPP retains the expressiveness of iterator-based programming, eliminates iterator-invalidation, and achieves asymptotic complexities comparable to STL implementations. Our design targets use cases where persistence and safety are desired, while allowing developers to retain familiar iterator-based programming patterns.