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

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Wed Aug 13 16:29:57 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>;
----------------
ldionne wrote:

Type safety is a lot more important than build-time performance. The ability to catch that `std::sort(v.begin(), opt.end());` is wrong at compile-time is absolutely something we should retain, even at the cost of some build-time performance. Remember that this build time performance is only paid if you actually instantiate `optional::iterator`, which shouldn't be all that common in itself.

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


More information about the libcxx-commits mailing list