Skip to content

Commit 7bf39a5

Browse files
[ADT] Refactor SmallVector::assertSafeToAddRange with "constexpr if" (#160004)
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.
1 parent 591e60b commit 7bf39a5

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

llvm/include/llvm/ADT/SmallVector.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -212,17 +212,16 @@ class SmallVectorTemplateCommon
212212
void assertSafeToReferenceAfterClear(ItTy, ItTy) {}
213213

214214
/// Check whether any part of the range will be invalidated by growing.
215-
void assertSafeToAddRange(const T *From, const T *To) {
216-
if (From == To)
217-
return;
218-
this->assertSafeToAdd(From, To - From);
219-
this->assertSafeToAdd(To - 1, To - From);
215+
template <class ItTy> void assertSafeToAddRange(ItTy From, ItTy To) {
216+
if constexpr (std::is_pointer_v<ItTy> &&
217+
std::is_same_v<std::remove_cv_t<std::remove_pointer_t<ItTy>>,
218+
T>) {
219+
if (From == To)
220+
return;
221+
this->assertSafeToAdd(From, To - From);
222+
this->assertSafeToAdd(To - 1, To - From);
223+
}
220224
}
221-
template <
222-
class ItTy,
223-
std::enable_if_t<!std::is_same<std::remove_const_t<ItTy>, T *>::value,
224-
bool> = false>
225-
void assertSafeToAddRange(ItTy, ItTy) {}
226225

227226
/// Reserve enough space to add one element, and return the updated element
228227
/// pointer in case it was a reference to the storage.

0 commit comments

Comments
 (0)