[libc-commits] [libc] [libc] Expand cpp::span and adopt in CPP utilities (PR #244026)

Pavel Labath via libc-commits libc-commits at lists.llvm.org
Thu Nov 19 03:00:10 PST 2026


================
@@ -43,6 +45,27 @@ template <typename T> class span {
   LIBC_INLINE_VAR static constexpr bool is_compatible_v =
       cpp::is_same_v<U, T> || is_const_view_v<U>;
 
+  LIBC_INLINE static constexpr void assert_precondition(bool cond) {
+#ifndef NDEBUG
+    if (LIBC_UNLIKELY(!cond))
+      __builtin_trap();
+#else
+    (void)cond;
+#endif
+  }
+
+  template <typename End>
+  LIBC_INLINE static constexpr size_t distance_to_end(T *first, End end) {
+    if constexpr (cpp::is_null_pointer_v<End>) {
+      assert_precondition(first == nullptr);
+      return 0;
+    } else {
+      T *end_ptr = end;
+      assert_precondition(first <= end_ptr);
+      return first == end_ptr ? 0 : static_cast<size_t>(end_ptr - first);
----------------
labath wrote:

What is the equality check necessary? I hope it's not necessary, but if it is for some reason, it's definitely worth a comment.

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


More information about the libc-commits mailing list