> Markdown version of [/videos/1045-why-iterators-got-it-all-wrong-and-what-we-should-use-instead](https://www.wearedevelopers.com/videos/1045-why-iterators-got-it-all-wrong-and-what-we-should-use-instead). Every page supports `.md` or `Accept: text/markdown`. Links point to the HTML versions so they work for humans too. Agent guide: [/agents.md](https://www.wearedevelopers.com/agents.md). --- # Why Iterators Got It All Wrong — and what we should use instead An analysis of a million lines of code reveals a fundamental flaw: C++ iterators conflate elements and borders. Discover how strict semantic separation eliminates ambiguity in modern ranges. - **Speakers:** [Arno Schoedl](https://www.wearedevelopers.com/@arno-schoedl) - **Event:** World Congress 2024 - **Published:** August 20, 2024 - **Duration:** 26:12 - **URL:** https://www.wearedevelopers.com/videos/1045-why-iterators-got-it-all-wrong-and-what-we-should-use-instead ## Summary C++ iterators, modeled after low-level machine pointers since the Standard Template Library (STL) era, suffer from a fundamental semantic flaw: they conflate the concepts of "elements" and "borders." While modern C++20 Ranges and range adapters like "transform" and "filter" significantly improve code readability by reducing boilerplate, they expose the conceptual limits of iterators. When relying on operations like ".base()" to peel off chained adapters, mapping reversed or filtered iterators back to their original sequences works for individual elements but leads to ambiguous or undefined behavior for sequence borders. To address this, developers must explicitly separate borders conceptually from elements. An analysis of over one million lines of production code reveals that developers almost always have a distinct operational intent: algorithms like "find" predominantly seek elements, while binary searches like "lower_bound" seek structural boundaries. By introducing a "border concept" (which describes structural boundaries and is not dereferenceable) and an "element concept" (which represents actual data and cannot span beyond the last element), developers can enforce strictly defined semantics. Instead of overloading the "end()" iterator to indicate a failed search, returning a nullable element provides cleaner and safer logic that perfectly mimics a null pointer. Projects looking to implement these stronger semantics can leverage the open-source think-cell range library. This toolkit provides refined algorithms with intent-rich naming conventions, such as "find_unique" or "binary_find," and fully implements nullable elements to help avoid the foundational pitfalls inherent in traditional C++ iterators. **Keywords:** c++ iterators, c++20 ranges, standard template library, range adapters, iterator adapters, sequence borders vs elements, nullable elements, lazy evaluation, iterator invalidation, algorithm semantics, boundary logic, think-cell range library, dereferencing iterators, binary search bounds ## Chapters 1. **The history and dual nature of iterators** (00:02) — Traditional iterators inherently combine pointer semantics and logical segment bounds into a single ambiguous construct. 1. **Simplifying code readability with modern standard ranges** (01:06) — Adopting modern range syntax removes redundant boundaries and forces predicate checks to logically align. 1. **Separating projection and search with range adapters** (02:29) — Applying lazy transformation adapters separates complex structural projections from the underlying search algorithms. 1. **Issues with base function and adapter transformations** (04:03) — Reversing adapter transformations causes structural ambiguity when bounds no longer map to fixed memory elements. 1. **Resolving reverse adapter ambiguities with segment boundaries** (07:55) — Since reversing range parameters alters the index offset, simple dereferences fail to find original elements. 1. **Replacing iterators with explicit elements and boundaries** (11:25) — Introducing completely separate base interfaces for values and boundaries fully solves order permutation errors. 1. **Analyzing empirical iterator usage across large codebases** (14:00) — Auditing production application logic confirms developers rarely utilize actual border lookups outside edge scenarios. 1. **Designing strict interfaces for borders and elements** (17:27) — Refactoring data interfaces into purely nullable entities clarifies standard search intent without referencing useless boundaries. 1. **Adopting a strongly typed semantic range library** (20:42) — Moving standard abstractions to custom typed libraries enforces semantic correctness during everyday validation operations. 1. **Addressing invalidation and boundary logic inside maps** (22:15) — Reviewing discrete boundaries helps integrate mathematical set logic safely alongside data container invalidations. ## Related Moments - [Permitting variable references and unsafe code in iterators](https://www.wearedevelopers.com/videos/1225-c-13-unleashed-live-demos-of-my-top-10-cutting-edge-features) (from "C# 13 Unleashed: Live Demos of my Top 10 Cutting-Edge Features!") - [Filtering and transforming ranges with lazy evaluation views](https://www.wearedevelopers.com/videos/1409-functional-programming-in-c) (from "Functional Programming in C++") - [Overriding loops with ranges and stepping](https://www.wearedevelopers.com/videos/512-route-from-java-to-kotlin) (from "Route from Java to Kotlin") - [Summarizing range composition patterns and standard library algorithms](https://www.wearedevelopers.com/videos/1409-functional-programming-in-c) (from "Functional Programming in C++") - [Modernizing legacy programming architectures using iterative core guidelines](https://www.wearedevelopers.com/videos/441-c-in-constrained-environments) (from "C++ in constrained environments") - [Addressing iterative code generation and developer challenges](https://www.wearedevelopers.com/videos/1354-google-gemma-and-open-source-ai-models-clement-farabet) (from "Google Gemma and Open Source AI Models - Clement Farabet") ## Related Articles - [Dev Digest 139 - Soft and hard queries](https://www.wearedevelopers.com/magazine/487-dev-digest-139-soft-and-hard-queries) - [Dev Digest 118 - not a total recall](https://www.wearedevelopers.com/magazine/452-dev-digest-118-not-a-total-recall) - [Dev Digest 130 - The Diablo is in the details](https://www.wearedevelopers.com/magazine/470-dev-digest-130-the-diablo-is-in-the-details) - [Dev Digest 138 - Are you secure about this?](https://www.wearedevelopers.com/magazine/486-dev-digest-138-are-you-secure-about-this) ## Related Jobs - [Principal Engineer - AI Search & Vector Infrastructure](https://www.wearedevelopers.com/jobs/ext/381484-principal-engineer-ai-search-vector-infrastructure) at **Redis** - [Principal Engineer - AI Search & Vector Infrastructure](https://www.wearedevelopers.com/jobs/ext/353953-principal-engineer-ai-search-vector-infrastructure) at **Redis** - [Principal Engineer - AI Search & Vector Infrastructure](https://www.wearedevelopers.com/jobs/ext/319507-principal-engineer-ai-search-vector-infrastructure) at **Redis** - [Machine Learning Engineer](https://www.wearedevelopers.com/jobs/ext/1597388-machine-learning-engineer) at **ZEISS Group** - [Remote Senior Full-Stack Engineer](https://www.wearedevelopers.com/jobs/ext/683045-remote-senior-full-stack-engineer) at **Edge Impulse** - [Remote Senior Full-Stack Engineer](https://www.wearedevelopers.com/jobs/ext/644637-remote-senior-full-stack-engineer) at **Edge Impulse**