[libcxx-commits] [libcxx] [libcxx] Optimize std::generate for segmented iterators (PR #163006)
A. Jiang via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Oct 14 21:16:42 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:
We need to handle iterators dereferencing to rvalues, as indicated by the error messages.
```suggestion
std::for_each(__first, __last, [&](auto&& __element) { static_cast<decltype(__element)>(__element) = __gen(); });
```
https://github.com/llvm/llvm-project/pull/163006
More information about the libcxx-commits
mailing list