[libcxx-commits] [libcxx] [libc++][string] P3044R2: sub-`string_view` from `string` (PR #147095)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Mon Sep 8 01:08:08 PDT 2025


================
@@ -465,6 +467,16 @@ public:
                           : basic_string_view(__assume_valid(), data() + __pos, std::min(__n, size() - __pos));
   }
 
+#  if _LIBCPP_STD_VER >= 26
+  _LIBCPP_HIDE_FROM_ABI constexpr basic_string_view subview(size_type __pos = 0, size_type __n = npos) const {
+    // Use the `__assume_valid` form of the constructor to avoid an unnecessary check. Any subview of a view is a
+    // valid view. In particular, `size()` is known to be smaller than `numeric_limits<difference_type>::max()`, so the
+    // new size is also smaller.
+    return __pos > size() ? (__throw_out_of_range("string_view::subview"), basic_string_view())
+                          : basic_string_view(__assume_valid(), data() + __pos, std::min(__n, size() - __pos));
+  }
----------------
philnik777 wrote:

Why doesn't this forward to `substr` anymore?


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


More information about the libcxx-commits mailing list