[llvm] [ADT] Refactor SmallVector::assertSafeToAddRange with "constexpr if" (PR #160004)

via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 21 13:06:54 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-adt

Author: Kazu Hirata (kazutakahirata)

<details>
<summary>Changes</summary>

This patch consolidates two implementations of assertSafeToAddRange
into a single template function.

The new implementation uses "constexpr if" to check for pointer
iterators, preserving the original behavior while simplifying the
code.


---
Full diff: https://github.com/llvm/llvm-project/pull/160004.diff


1 Files Affected:

- (modified) llvm/include/llvm/ADT/SmallVector.h (+9-10) 


``````````diff
diff --git a/llvm/include/llvm/ADT/SmallVector.h b/llvm/include/llvm/ADT/SmallVector.h
index 80f7734b86907..5577b09fee89c 100644
--- a/llvm/include/llvm/ADT/SmallVector.h
+++ b/llvm/include/llvm/ADT/SmallVector.h
@@ -212,17 +212,16 @@ class SmallVectorTemplateCommon
   void assertSafeToReferenceAfterClear(ItTy, ItTy) {}
 
   /// Check whether any part of the range will be invalidated by growing.
-  void assertSafeToAddRange(const T *From, const T *To) {
-    if (From == To)
-      return;
-    this->assertSafeToAdd(From, To - From);
-    this->assertSafeToAdd(To - 1, To - From);
+  template <class ItTy> void assertSafeToAddRange(ItTy From, ItTy To) {
+    if constexpr (std::is_pointer_v<ItTy> &&
+                  std::is_same_v<std::remove_cv_t<std::remove_pointer_t<ItTy>>,
+                                 T>) {
+      if (From == To)
+        return;
+      this->assertSafeToAdd(From, To - From);
+      this->assertSafeToAdd(To - 1, To - From);
+    }
   }
-  template <
-      class ItTy,
-      std::enable_if_t<!std::is_same<std::remove_const_t<ItTy>, T *>::value,
-                       bool> = false>
-  void assertSafeToAddRange(ItTy, ItTy) {}
 
   /// Reserve enough space to add one element, and return the updated element
   /// pointer in case it was a reference to the storage.

``````````

</details>


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


More information about the llvm-commits mailing list