[libcxx-commits] [libcxx] [libc++] Optimize ranges::{for_each, for_each_n} for segmented iterators (PR #132896)

Peng Liu via libcxx-commits libcxx-commits at lists.llvm.org
Thu Apr 3 09:49:56 PDT 2025


================
@@ -40,11 +42,17 @@ struct __for_each_n {
   template <input_iterator _Iter, class _Proj = identity, indirectly_unary_invocable<projected<_Iter, _Proj>> _Func>
   _LIBCPP_HIDE_FROM_ABI constexpr for_each_n_result<_Iter, _Func>
   operator()(_Iter __first, iter_difference_t<_Iter> __count, _Func __func, _Proj __proj = {}) const {
-    while (__count-- > 0) {
-      std::invoke(__func, std::invoke(__proj, *__first));
-      ++__first;
+    if constexpr (forward_iterator<_Iter>) {
----------------
winner245 wrote:

The problem is now fixed since `ranges::for_each_n` now directly calls `for_each_n` and `for_each_n` has the updated `enable_if` constraint. 

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


More information about the libcxx-commits mailing list