[libcxx-commits] [libcxx] [libc++] Implement a type-safe iterator for optional (PR #154239)
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Jan 14 14:07:25 PST 2026
================
@@ -0,0 +1,204 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___CAPACITY_AWARE_ITERATOR_H
+#define _LIBCPP___CAPACITY_AWARE_ITERATOR_H
+
+#include <__assert>
+#include <__compare/ordering.h>
+#include <__compare/three_way_comparable.h>
+#include <__config>
+#include <__cstddef/size_t.h>
+#include <__iterator/concepts.h>
+#include <__iterator/incrementable_traits.h>
+#include <__iterator/iterator_traits.h>
+#include <__memory/pointer_traits.h>
+#include <__type_traits/is_constructible.h>
+#include <__type_traits/is_convertible.h>
+#include <__utility/move.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+# pragma GCC system_header
+#endif
+
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
+#if _LIBCPP_STD_VER >= 26
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+// __capacity_aware_iterator is an iterator that wraps an underlying iterator.
+// It stores the underlying container type to prevent mixing iterators, and allow algorithms
+// to optimize based on the underlying container type.
+// It also encodes the container's (known at compile-time) maximum amount of elements as part of the type.
+// As of writing, the only standard library containers which have this property are inplace_vector and optional.
----------------
ldionne wrote:
```suggestion
// __capacity_aware_iterator is an iterator that wraps an underlying iterator, encoding the maximum number of elements that can appear in a range of such iterators. That maximum number of elements must be known at compile-time.
//
// It also embeds a tag type in its own type in order to prevent mixing iterators coming from e.g. different containers. The intent is for some algorithms to detect this iterator and perform optimizations based on the
// added semantic information.
//
// As of writing, the only standard library containers which have this property are inplace_vector and optional.
```
https://github.com/llvm/llvm-project/pull/154239
More information about the libcxx-commits
mailing list