[libcxx-commits] [libcxx] [libc++] Implement P3168R2: Give optional range support (PR #149441)

William Tran-Viet via libcxx-commits libcxx-commits at lists.llvm.org
Mon Jul 21 10:28:37 PDT 2025


================
@@ -586,8 +614,17 @@ class _LIBCPP_DECLSPEC_EMPTY_BASES optional
       private __optional_sfinae_assign_base_t<_Tp> {
   using __base _LIBCPP_NODEBUG = __optional_move_assign_base<_Tp>;
 
+#    if _LIBCPP_STD_VER >= 26
+  using pointer       = std::add_pointer_t<std::remove_cvref_t<_Tp>>;
+  using const_pointer = std::add_pointer_t<const std::remove_cvref_t<_Tp>>;
+#    endif
+
 public:
   using value_type = _Tp;
+#    if _LIBCPP_STD_VER >= 26
+  using iterator       = __wrap_iter<pointer>;
----------------
smallp-o-p wrote:

I personally think the second option sounds good, but should go in a different PR of course. It might also be useful for other containers like `inplace_vector`.

As for the `__static_bounded_iter` suggestion, I originally implemented hardening with `__bounded_iter` (but kept it out of the PR to get some input first), which felt like the better choice since we can't know if the `optional` is going to have a range of 0 or 1 at compile time(?)

https://github.com/llvm/llvm-project/pull/149441


More information about the libcxx-commits mailing list