[libcxx-commits] [libcxx] [libcxx] Optimize `std::generate_n` for segmented iterators (PR #164266)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Tue Oct 21 01:01:32 PDT 2025


================
@@ -21,11 +23,10 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 template <class _OutputIterator, class _Size, class _Generator>
 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
 generate_n(_OutputIterator __first, _Size __orig_n, _Generator __gen) {
-  typedef decltype(std::__convert_to_integral(__orig_n)) _IntegralSize;
-  _IntegralSize __n = __orig_n;
-  for (; __n > 0; ++__first, (void)--__n)
-    *__first = __gen();
-  return __first;
+  using __iter_ref = decltype(*__first);
+  __identity __proj;
+  auto __f = [&](__iter_ref __element) { std::forward<__iter_ref>(__element) = __gen(); };
----------------
philnik777 wrote:

Let's not introduce a variable here.

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


More information about the libcxx-commits mailing list