[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:30 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.
+
+template <class _Iter, class _Container, std::size_t _ContainerMaxElements>
----------------
ldionne wrote:
```suggestion
template <class _Iter, class _Tag, std::size_t _RangeMaxElements>
```
We could rename this to `_Tag` since this doesn't have to be a container type strictly speaking. In fact, it might be a good idea *not* to use a container type since those are very long and will screw up compiler messages. Instead, perhaps an opaque nested struct of the container could be used in some cases since I believe this leads to shorter names (but this would have to be validated).
And `_RangeMaxElements` since there may not be a container.
https://github.com/llvm/llvm-project/pull/154239
More information about the libcxx-commits
mailing list