Skip to content

Commit d8642b2

Browse files
committed
Use lambda instead of function object class template
1 parent a33aee8 commit d8642b2

File tree

3 files changed

+243
-173
lines changed

3 files changed

+243
-173
lines changed

libcxx/include/__algorithm/for_each.h

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121
# pragma GCC system_header
2222
#endif
2323

24-
_LIBCPP_PUSH_MACROS
25-
#include <__undef_macros>
26-
2724
_LIBCPP_BEGIN_NAMESPACE_STD
2825

2926
template <class _InputIterator, class _Sent, class _Func>
@@ -32,29 +29,19 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __for_each(_InputIterat
3229
__f(*__first);
3330
}
3431

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-
32+
#ifndef _LIBCPP_CXX03_LANG
5133
template <class _SegmentedIterator,
5234
class _Function,
5335
__enable_if_t<__is_segmented_iterator<_SegmentedIterator>::value, int> = 0>
5436
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
5537
__for_each(_SegmentedIterator __first, _SegmentedIterator __last, _Function& __func) {
56-
std::__for_each_segment(__first, __last, std::__segment_processor<_Function>(__func));
38+
using _Traits = __segmented_iterator_traits<_SegmentedIterator>;
39+
std::__for_each_segment(
40+
__first, __last, [&](typename _Traits::__local_iterator __lfirst, typename _Traits::__local_iterator __llast) {
41+
std::__for_each(__lfirst, __llast, __func);
42+
});
5743
}
44+
#endif
5845

5946
template <class _InputIterator, class _Function>
6047
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Function
@@ -65,6 +52,4 @@ for_each(_InputIterator __first, _InputIterator __last, _Function __f) {
6552

6653
_LIBCPP_END_NAMESPACE_STD
6754

68-
_LIBCPP_POP_MACROS
69-
7055
#endif // _LIBCPP___ALGORITHM_FOR_EACH_H

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)