[libcxx-commits] [libcxx] [libc++] Make optional::iterator experimental (PR #173470)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Wed Dec 24 01:30:24 PST 2025


https://github.com/philnik777 created https://github.com/llvm/llvm-project/pull/173470

We haven't yet decided what we want the `optional::iterator` type to be in the end, so let's make it experimental for now so that we don't commit to an ABI yet.


>From 59c49ab0c1f498d6796f25327b8ae5cd1c8776c5 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Wed, 24 Dec 2025 10:29:28 +0100
Subject: [PATCH] [libc++] Make optional::iterator experimental

---
 libcxx/include/__configuration/experimental.h |  1 +
 libcxx/include/optional                       | 26 +++++++++++--------
 2 files changed, 16 insertions(+), 11 deletions(-)

diff --git a/libcxx/include/__configuration/experimental.h b/libcxx/include/__configuration/experimental.h
index d14df3e5175f3..bb38d8297c63d 100644
--- a/libcxx/include/__configuration/experimental.h
+++ b/libcxx/include/__configuration/experimental.h
@@ -33,5 +33,6 @@
 #define _LIBCPP_HAS_EXPERIMENTAL_TZDB _LIBCPP_HAS_EXPERIMENTAL_LIBRARY
 #define _LIBCPP_HAS_EXPERIMENTAL_SYNCSTREAM _LIBCPP_HAS_EXPERIMENTAL_LIBRARY
 #define _LIBCPP_HAS_EXPERIMENTAL_HARDENING_OBSERVE_SEMANTIC _LIBCPP_HAS_EXPERIMENTAL_LIBRARY
+#define _LIBCPP_HAS_EXPERIMENTAL_OPTIONAL_ITERATOR _LIBCPP_HAS_EXPERIMENTAL_LIBRARY
 
 #endif // _LIBCPP___CONFIGURATION_EXPERIMENTAL_H
diff --git a/libcxx/include/optional b/libcxx/include/optional
index 440fdf73a4310..b790b206a18b6 100644
--- a/libcxx/include/optional
+++ b/libcxx/include/optional
@@ -681,6 +681,8 @@ struct __is_std_optional<optional<_Tp>> : true_type {};
 template <class _Tp, class = void>
 struct __optional_iterator {};
 
+#    if _LIBCPP_HAS_EXPERIMENTAL_OPTIONAL_ITERATOR
+
 template <class _Tp>
 struct __optional_iterator<
     _Tp,
@@ -691,14 +693,14 @@ private:
   using __const_pointer _LIBCPP_NODEBUG = add_pointer_t<const remove_reference_t<_Tp>>;
 
 public:
-#    if _LIBCPP_STD_VER >= 26
-#      ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_OPTIONAL
+#      if _LIBCPP_STD_VER >= 26
+#        ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_OPTIONAL
   using iterator       = __bounded_iter<__wrap_iter<__pointer>>;
   using const_iterator = __bounded_iter<__wrap_iter<__const_pointer>>;
-#      else
+#        else
   using iterator       = __wrap_iter<__pointer>;
   using const_iterator = __wrap_iter<__const_pointer>;
-#      endif
+#        endif
 
   // [optional.iterators], iterator support
   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr iterator begin() noexcept {
@@ -710,14 +712,14 @@ public:
       return std::addressof(__derived_self.__get());
     }();
 
-#      ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_OPTIONAL
+#        ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_OPTIONAL
     return std::__make_bounded_iter(
         __wrap_iter<__pointer>(__ptr),
         __wrap_iter<__pointer>(__ptr),
         __wrap_iter<__pointer>(__ptr) + (__derived_self.has_value() ? 1 : 0));
-#      else
+#        else
     return iterator(__ptr);
-#      endif
+#        endif
   }
 
   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const_iterator begin() const noexcept {
@@ -729,14 +731,14 @@ public:
       return std::addressof(__derived_self.__get());
     }();
 
-#      ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_OPTIONAL
+#        ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_OPTIONAL
     return std::__make_bounded_iter(
         __wrap_iter<__const_pointer>(__ptr),
         __wrap_iter<__const_pointer>(__ptr),
         __wrap_iter<__const_pointer>(__ptr) + (__derived_self.has_value() ? 1 : 0));
-#      else
+#        else
     return const_iterator(__ptr);
-#      endif
+#        endif
   }
 
   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr iterator end() noexcept {
@@ -745,9 +747,11 @@ public:
   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const_iterator end() const noexcept {
     return begin() + (static_cast<const optional<_Tp>&>(*this).has_value() ? 1 : 0);
   }
-#    endif
+#      endif // _LIBCPP_STD_VER >= 26
 };
 
+#    endif // _LIBCPP_HAS_EXPERIMENTAL_OPTIONAL_ITERATOR
+
 template <class _Tp>
 class _LIBCPP_DECLSPEC_EMPTY_BASES optional
     : private __optional_move_assign_base<_Tp>,



More information about the libcxx-commits mailing list