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

via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 28 07:49:51 PDT 2024


https://github.com/aws-taylor created https://github.com/llvm/llvm-project/pull/106400

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).

>From 2089ddd2b7759e5470487a2b6e78378f50ac63b9 Mon Sep 17 00:00:00 2001
From: aws-taylor <57725958+aws-taylor at users.noreply.github.com>
Date: Wed, 28 Aug 2024 07:48:08 -0700
Subject: [PATCH] Relax iterator constraints on all_equal

The previous `all_equal` implementation contained `Begin + 1`, which implicitly requires `Begin` to model the 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.
---
 llvm/include/llvm/ADT/STLExtras.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

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



More information about the llvm-commits mailing list