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

Connector Switch via libcxx-commits libcxx-commits at lists.llvm.org
Sat Oct 18 04:49:22 PDT 2025


================
@@ -20,8 +22,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, [&](decltype(*__first) __element) { __element = __gen(); });
----------------
c8ef wrote:

I am not an expert on std::forward, but from my understanding, forwarding references are either:

1. function parameter of a function template declared as rvalue reference to cv-unqualified [type template parameter](https://en.cppreference.com/w/cpp/language/template_parameters.html#Type_template_parameter) of that same function template.
2. auto&& except when deduced from a brace-enclosed initializer list or, when representing a template parameter of a class template during [class template argument deduction](https://en.cppreference.com/w/cpp/language/ctad.html).

To me, `__iter_ref` does not fit into either category.

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


More information about the libcxx-commits mailing list