[libcxx-commits] [libcxx] a3f8480 - [libcxx] Modify `std::__for_each{, _n}` to accept r-values in `__f` (#179451)
via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Feb 4 07:15:26 PST 2026
Author: Connector Switch
Date: 2026-02-04T23:15:21+08:00
New Revision: a3f848057be438a570655ee3ca48a1178e898658
URL: https://github.com/llvm/llvm-project/commit/a3f848057be438a570655ee3ca48a1178e898658
DIFF: https://github.com/llvm/llvm-project/commit/a3f848057be438a570655ee3ca48a1178e898658.diff
LOG: [libcxx] Modify `std::__for_each{, _n}` to accept r-values in `__f` (#179451)
This is necessary when optimizing algorithms for segmented iterators to
reduce boilerplate code.
related:
-
https://github.com/llvm/llvm-project/pull/177853#discussion_r2754820322
-
https://github.com/llvm/llvm-project/pull/164266#discussion_r2447129525
Added:
Modified:
libcxx/include/__algorithm/for_each.h
libcxx/include/__algorithm/for_each_n.h
libcxx/include/__algorithm/generate_n.h
Removed:
################################################################################
diff --git a/libcxx/include/__algorithm/for_each.h b/libcxx/include/__algorithm/for_each.h
index 85fedce3d936d..bb656a2db0188 100644
--- a/libcxx/include/__algorithm/for_each.h
+++ b/libcxx/include/__algorithm/for_each.h
@@ -26,7 +26,7 @@ _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&& __func, _Proj& __proj) {
#ifndef _LIBCPP_CXX03_LANG
if constexpr (using _SpecialAlg =
__specialized_algorithm<_Algorithm::__for_each, __iterator_pair<_InputIterator, _Sent>>;
diff --git a/libcxx/include/__algorithm/for_each_n.h b/libcxx/include/__algorithm/for_each_n.h
index 72c7adb093f95..7ff3f9b157c02 100644
--- a/libcxx/include/__algorithm/for_each_n.h
+++ b/libcxx/include/__algorithm/for_each_n.h
@@ -18,6 +18,7 @@
#include <__iterator/segmented_iterator.h>
#include <__type_traits/invoke.h>
#include <__utility/convert_to_integral.h>
+#include <__utility/forward.h>
#include <__utility/move.h>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -31,7 +32,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template <class _InputIterator, class _Size, class _Func, class _Proj>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _InputIterator
-__for_each_n(_InputIterator __first, _Size __orig_n, _Func& __f, _Proj& __proj) {
+__for_each_n(_InputIterator __first, _Size __orig_n, _Func&& __f, _Proj& __proj) {
typedef decltype(std::__convert_to_integral(__orig_n)) _IntegralSize;
_IntegralSize __n = __orig_n;
@@ -43,7 +44,7 @@ __for_each_n(_InputIterator __first, _Size __orig_n, _Func& __f, _Proj& __proj)
std::__for_each(__lfirst, __llast, __f, __proj);
});
} else {
- return std::__for_each(__first, __first + __n, __f, __proj);
+ return std::__for_each(__first, __first + __n, std::forward<_Func>(__f), __proj);
}
} else
#endif
diff --git a/libcxx/include/__algorithm/generate_n.h b/libcxx/include/__algorithm/generate_n.h
index 23899e49e0b65..ba04eb62e3c3a 100644
--- a/libcxx/include/__algorithm/generate_n.h
+++ b/libcxx/include/__algorithm/generate_n.h
@@ -29,8 +29,11 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
__generate_n(_OutputIterator __first, _Size __orig_n, _Generator& __gen) {
using __iter_ref = decltype(*__first);
__identity __proj;
- auto __f = [&](__iter_ref __element) { std::forward<__iter_ref>(__element) = __gen(); };
- return std::__for_each_n(std::move(__first), __orig_n, __f, __proj);
+ return std::__for_each_n(
+ std::move(__first),
+ __orig_n,
+ [&](__iter_ref __element) { std::forward<__iter_ref>(__element) = __gen(); },
+ __proj);
}
template <class _OutputIterator, class _Size, class _Generator>
More information about the libcxx-commits
mailing list