2 //===----------------------------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___ALGORITHM_FOR_EACH_H
11 #define _LIBCPP___ALGORITHM_FOR_EACH_H
13 #include <__cxx03/__algorithm/for_each_segment.h>
14 #include <__cxx03/__config>
15 #include <__cxx03/__iterator/segmented_iterator.h>
16 #include <__cxx03/__ranges/movable_box.h>
17 #include <__cxx03/__type_traits/enable_if.h>
18 #include <__cxx03/__utility/in_place.h>
19 #include <__cxx03/__utility/move.h>
21 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22 # pragma GCC system_header
26 #include <__cxx03/__undef_macros>
28 _LIBCPP_BEGIN_NAMESPACE_STD
30 template <class _InputIterator
, class _Function
>
31 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Function
32 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
) {
33 for (; __first
!= __last
; ++__first
)
38 // __movable_box is available in C++20, but is actually a copyable-box, so optimization is only correct in C++23
39 #if _LIBCPP_STD_VER >= 23
40 template <class _SegmentedIterator
, class _Function
>
41 requires __is_segmented_iterator
<_SegmentedIterator
>::value
42 _LIBCPP_HIDE_FROM_ABI
constexpr _Function
43 for_each(_SegmentedIterator __first
, _SegmentedIterator __last
, _Function __func
) {
44 ranges::__movable_box
<_Function
> __wrapped_func(in_place
, std::move(__func
));
45 std::__for_each_segment(__first
, __last
, [&](auto __lfirst
, auto __llast
) {
47 ranges::__movable_box
<_Function
>(in_place
, std::for_each(__lfirst
, __llast
, std::move(*__wrapped_func
)));
49 return std::move(*__wrapped_func
);
51 #endif // _LIBCPP_STD_VER >= 23
53 _LIBCPP_END_NAMESPACE_STD
57 #endif // _LIBCPP___ALGORITHM_FOR_EACH_H