[libcxx-commits] [libcxx] [libc++] Implement any_of in terms of find_if (PR #207274)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Fri Jul 3 12:40:18 PDT 2026


https://github.com/ldionne updated https://github.com/llvm/llvm-project/pull/207274

>From 47d00f9d1665b51b2d0728ed6faecd5e7f75750d Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Thu, 2 Jul 2026 16:39:23 -0400
Subject: [PATCH 1/4] [libc++] Implement any_of in terms of find_if

This way, any optimizations in find_if will be picked up by any_of.

Closes #129310
---
 libcxx/include/__algorithm/any_of.h | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/libcxx/include/__algorithm/any_of.h b/libcxx/include/__algorithm/any_of.h
index 4b6eb94517286..fed39c8e4dc7c 100644
--- a/libcxx/include/__algorithm/any_of.h
+++ b/libcxx/include/__algorithm/any_of.h
@@ -10,9 +10,11 @@
 #ifndef _LIBCPP___ALGORITHM_ANY_OF_H
 #define _LIBCPP___ALGORITHM_ANY_OF_H
 
+#include <__algorithm/find_if.h>
 #include <__config>
 #include <__functional/identity.h>
 #include <__type_traits/invoke.h>
+#include <__utility/forward.h>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
@@ -23,18 +25,15 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 template <class _Iter, class _Sent, class _Proj, class _Pred>
 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
 __any_of(_Iter __first, _Sent __last, _Pred& __pred, _Proj& __proj) {
-  for (; __first != __last; ++__first) {
-    if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
-      return true;
-  }
-  return false;
+  auto __found = std::__find_if(std::move(__first), __last, std::forward<_Pred>(__pred), std::forward<_Proj>(__proj));
+  return __found != __last;
 }
 
 template <class _InputIterator, class _Predicate>
 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
 any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred) {
   __identity __proj;
-  return std::__any_of(__first, __last, __pred, __proj);
+  return std::__any_of(std::move(__first), std::move(__last), __pred, __proj);
 }
 
 _LIBCPP_END_NAMESPACE_STD

>From 2664ffa9c364847bbc04f398302f7144a2e2dee0 Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Thu, 2 Jul 2026 16:43:29 -0400
Subject: [PATCH 2/4] Clean up include

---
 libcxx/include/__algorithm/any_of.h | 1 -
 1 file changed, 1 deletion(-)

diff --git a/libcxx/include/__algorithm/any_of.h b/libcxx/include/__algorithm/any_of.h
index fed39c8e4dc7c..94a0917899d63 100644
--- a/libcxx/include/__algorithm/any_of.h
+++ b/libcxx/include/__algorithm/any_of.h
@@ -13,7 +13,6 @@
 #include <__algorithm/find_if.h>
 #include <__config>
 #include <__functional/identity.h>
-#include <__type_traits/invoke.h>
 #include <__utility/forward.h>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)

>From 70e38949da0c4bb9df744d8c47b2208cadd39b23 Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Fri, 3 Jul 2026 15:39:34 -0400
Subject: [PATCH 3/4] push/pop

---
 libcxx/include/__algorithm/any_of.h | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/libcxx/include/__algorithm/any_of.h b/libcxx/include/__algorithm/any_of.h
index 94a0917899d63..abf729c2086ab 100644
--- a/libcxx/include/__algorithm/any_of.h
+++ b/libcxx/include/__algorithm/any_of.h
@@ -19,6 +19,9 @@
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 template <class _Iter, class _Sent, class _Proj, class _Pred>
@@ -37,4 +40,6 @@ any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred) {
 
 _LIBCPP_END_NAMESPACE_STD
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP___ALGORITHM_ANY_OF_H

>From 9b0c4388b96e271aab114d5ea0550d7dc467afbc Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Fri, 3 Jul 2026 15:39:56 -0400
Subject: [PATCH 4/4] forwarding

---
 libcxx/include/__algorithm/any_of.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/include/__algorithm/any_of.h b/libcxx/include/__algorithm/any_of.h
index abf729c2086ab..5e1166c2803c3 100644
--- a/libcxx/include/__algorithm/any_of.h
+++ b/libcxx/include/__algorithm/any_of.h
@@ -27,7 +27,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 template <class _Iter, class _Sent, class _Proj, class _Pred>
 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
 __any_of(_Iter __first, _Sent __last, _Pred& __pred, _Proj& __proj) {
-  auto __found = std::__find_if(std::move(__first), __last, std::forward<_Pred>(__pred), std::forward<_Proj>(__proj));
+  auto __found = std::__find_if(std::move(__first), __last, __pred, __proj);
   return __found != __last;
 }
 



More information about the libcxx-commits mailing list