[libcxx-commits] [libcxx] [libcxx] Optimize std::generate for segmented iterators (PR #163006)
Connector Switch via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Oct 15 08:00:19 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(); });
----------------
c8ef wrote:
```
# | C://llvm-project/build/libcxx/test-suite-install/include/c++/v1/__type_traits/invoke.h:88:69: note: candidate template i
gnored: substitution failure [with _Args = <(lambda at C://llvm-project/build/libcxx/test-suite-install/include/c++/v1/__alg
orithm/generate.h:24:34) &, LifetimeIterator::Reference>]: no type named 'type' in 'std::__invoke_result_impl<void, (lambda at C:/Users/Mario/Doc
uments/llvm-project/build/libcxx/test-suite-install/include/c++/v1/__algorithm/generate.h:24:34) &, LifetimeIterator::Reference>'
```
Thank you for the suggestions! I have made updates to the implementations.
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?
I have also included a release note based on a previous related patch.
https://github.com/llvm/llvm-project/pull/163006
More information about the libcxx-commits
mailing list