[libcxx-commits] [libcxx] [libcxx] Optimize std::generate for segmented iterators (PR #163006)

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Wed Oct 15 18:10:30 PDT 2025


================
@@ -20,8 +21,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 template <class _ForwardIterator, class _Generator>
 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
 generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen) {
-  for (; __first != __last; ++__first)
-    *__first = __gen();
+  std::for_each(__first, __last, [&](auto& __element) { __element = __gen(); });
----------------
frederick-vs-ja wrote:

> Just to clarify, is it because when dereferencing `LifetimeIterator`, we get a type named `Reference` which is itself an rvalue, so `auto&` cannot bind to it?

Yes, it is.

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


More information about the libcxx-commits mailing list