[libcxx-commits] [libcxx] [libcxx] Modify `std::__for_each{, _n}` to accept r-values in `__f` (PR #179451)

Connector Switch via libcxx-commits libcxx-commits at lists.llvm.org
Wed Feb 4 04:54:21 PST 2026


================
@@ -17,32 +17,36 @@
 #include <__iterator/segmented_iterator.h>
 #include <__type_traits/invoke.h>
 #include <__type_traits/is_same.h>
+#include <__utility/forward.h>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 template <class _InputIterator, class _Sent, class _Func, class _Proj>
 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _InputIterator
-__for_each(_InputIterator __first, _Sent __last, _Func& __func, _Proj& __proj) {
+__for_each(_InputIterator __first, _Sent __last, _Func&& __f, _Proj& __proj) {
 #ifndef _LIBCPP_CXX03_LANG
   if constexpr (using _SpecialAlg =
                     __specialized_algorithm<_Algorithm::__for_each, __iterator_pair<_InputIterator, _Sent>>;
                 _SpecialAlg::__has_algorithm) {
-    _SpecialAlg()(__first, __last, __func, __proj);
+    _SpecialAlg()(__first, __last, std::forward<_Func>(__f), __proj);
----------------
c8ef wrote:

I suggest merging this patch as-is and adding a test at https://github.com/llvm/llvm-project/pull/177853. Since `fold_left_with_iter` directly calls `__for_each`, we can easily add a `std::set` test at https://github.com/llvm/llvm-project/blob/d8da229d7172700f4f93fafe3ac22144ca04f6e9/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/left_folds.pass.cpp#L315.

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


More information about the libcxx-commits mailing list