Skip to content

Commit db2a070

Browse files
committed
Use lambda instead of function object class template
1 parent 5aa59ab commit db2a070

File tree

3 files changed

+243
-168
lines changed

3 files changed

+243
-168
lines changed

libcxx/include/__algorithm/for_each.h

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,19 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __for_each(_InputIterat
3232
__f(*__first);
3333
}
3434

35-
// __segment_processor handles the per-segment processing by applying the function object __func_ to each
36-
// element within the segment.
37-
template <class _Func>
38-
struct __segment_processor {
39-
_Func& __func_;
40-
41-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __segment_processor(_Func& __f) : __func_(__f) {}
42-
43-
template <class _SegmentedIterator>
44-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
45-
operator()(typename __segmented_iterator_traits<_SegmentedIterator>::__local_iterator __lfirst,
46-
typename __segmented_iterator_traits<_SegmentedIterator>::__local_iterator __llast) {
47-
std::__for_each(__lfirst, __llast, __func_);
48-
}
49-
};
50-
35+
#ifndef _LIBCPP_CXX03_LANG
5136
template <class _SegmentedIterator,
5237
class _Function,
5338
__enable_if_t<__is_segmented_iterator<_SegmentedIterator>::value, int> = 0>
5439
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
5540
__for_each(_SegmentedIterator __first, _SegmentedIterator __last, _Function& __func) {
56-
std::__for_each_segment(__first, __last, std::__segment_processor<_Function>(__func));
41+
using _Traits = __segmented_iterator_traits<_SegmentedIterator>;
42+
std::__for_each_segment(
43+
__first, __last, [&](typename _Traits::__local_iterator __lfirst, typename _Traits::__local_iterator __llast) {
44+
std::__for_each(__lfirst, __llast, __func);
45+
});
5746
}
47+
#endif
5848

5949
template <class _InputIterator, class _Function>
6050
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Function

libcxx/include/__algorithm/for_each_segment.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,20 @@ __for_each_segment(_SegmentedIterator __first, _SegmentedIterator __last, _Funct
3232

3333
// We are in a single segment, so we might not be at the beginning or end
3434
if (__sfirst == __slast) {
35-
__func.template operator()<_SegmentedIterator>(_Traits::__local(__first), _Traits::__local(__last));
35+
__func(_Traits::__local(__first), _Traits::__local(__last));
3636
return;
3737
}
3838

3939
// We have more than one segment. Iterate over the first segment, since we might not start at the beginning
40-
__func.template operator()<_SegmentedIterator>(_Traits::__local(__first), _Traits::__end(__sfirst));
40+
__func(_Traits::__local(__first), _Traits::__end(__sfirst));
4141
++__sfirst;
4242
// iterate over the segments which are guaranteed to be completely in the range
4343
while (__sfirst != __slast) {
44-
__func.template operator()<_SegmentedIterator>(_Traits::__begin(__sfirst), _Traits::__end(__sfirst));
44+
__func(_Traits::__begin(__sfirst), _Traits::__end(__sfirst));
4545
++__sfirst;
4646
}
4747
// iterate over the last segment
48-
__func.template operator()<_SegmentedIterator>(_Traits::__begin(__sfirst), _Traits::__local(__last));
48+
__func(_Traits::__begin(__sfirst), _Traits::__local(__last));
4949
}
5050

5151
_LIBCPP_END_NAMESPACE_STD

0 commit comments

Comments
 (0)