[libcxx-commits] [libcxx] [libcxx] Optimize `std::generate_n` for segmented iterators (PR #164266)
Connector Switch via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Oct 21 02:54:09 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(); };
----------------
c8ef wrote:
The functions std::__for_each_n and std::__for_each declare `f` as `Func&`, which prevents directly passing a temporary lambda expression; therefore, I needed to use a variable instead.
https://github.com/llvm/llvm-project/pull/164266
More information about the libcxx-commits
mailing list