[llvm] Relax iterator constraints on all_equal (PR #106400)

via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 28 07:50:52 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-adt

Author: None (aws-taylor)

<details>
<summary>Changes</summary>

The previous `all_equal` implementation contained `Begin + 1`, which implicitly requires `Begin` to model the [random_access_iterator](https://en.cppreference.com/w/cpp/iterator/random_access_iterator) concept due to the usage of the `+` operator. By swapping this out with `std::next`, this method can be used with weaker iterator concepts, such as [forward_iterator](https://en.cppreference.com/w/cpp/iterator/forward_iterator).

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


1 Files Affected:

- (modified) llvm/include/llvm/ADT/STLExtras.h (+1-1) 


``````````diff
diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h
index b2df0cd650ad7b..e11d6cac7685e4 100644
--- a/llvm/include/llvm/ADT/STLExtras.h
+++ b/llvm/include/llvm/ADT/STLExtras.h
@@ -2062,7 +2062,7 @@ bool equal(L &&LRange, R &&RRange, BinaryPredicate P) {
 template <typename R> bool all_equal(R &&Range) {
   auto Begin = adl_begin(Range);
   auto End = adl_end(Range);
-  return Begin == End || std::equal(Begin + 1, End, Begin);
+  return Begin == End || std::equal(std::next(Begin), End, Begin);
 }
 
 /// Returns true if all Values in the initializer lists are equal or the list

``````````

</details>


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


More information about the llvm-commits mailing list