1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef _LIBCPP___ALGORITHM_ITERATOR_OPERATIONS_H 10 #define _LIBCPP___ALGORITHM_ITERATOR_OPERATIONS_H 11 12 #include <__algorithm/iter_swap.h> 13 #include <__config> 14 #include <__iterator/advance.h> 15 #include <__iterator/distance.h> 16 #include <__iterator/iter_move.h> 17 #include <__iterator/iter_swap.h> 18 #include <__iterator/iterator_traits.h> 19 #include <__iterator/next.h> 20 #include <__iterator/readable_traits.h> 21 #include <__utility/declval.h> 22 #include <__utility/forward.h> 23 #include <__utility/move.h> 24 #include <type_traits> 25 26 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 27 # pragma GCC system_header 28 #endif 29 30 _LIBCPP_BEGIN_NAMESPACE_STD 31 32 template <class _AlgPolicy> struct _IterOps; 33 34 #if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) 35 struct _RangeAlgPolicy {}; 36 37 template <> 38 struct _IterOps<_RangeAlgPolicy> { 39 40 template <class _Iter> 41 using __value_type = iter_value_t<_Iter>; 42 43 static constexpr auto advance = ranges::advance; 44 static constexpr auto distance = ranges::distance; 45 static constexpr auto __iter_move = ranges::iter_move; 46 static constexpr auto iter_swap = ranges::iter_swap; 47 static constexpr auto next = ranges::next; 48 static constexpr auto __advance_to = ranges::advance; 49 }; 50 51 #endif 52 53 struct _ClassicAlgPolicy {}; 54 55 template <> 56 struct _IterOps<_ClassicAlgPolicy> { 57 58 template <class _Iter> 59 using __value_type = typename iterator_traits<_Iter>::value_type; 60 61 // advance 62 template <class _Iter, class _Distance> 63 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 64 static void advance(_Iter& __iter, _Distance __count) { 65 std::advance(__iter, __count); 66 } 67 68 // distance 69 template <class _Iter> 70 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 71 static typename iterator_traits<_Iter>::difference_type distance(_Iter __first, _Iter __last) { 72 return std::distance(__first, __last); 73 } 74 75 template <class _Iter> 76 using __deref_t = decltype(*std::declval<_Iter&>()); 77 78 template <class _Iter> 79 using __move_t = decltype(std::move(*std::declval<_Iter&>())); 80 81 template <class _Iter> 82 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 83 static void __validate_iter_reference() { 84 static_assert(is_same<__deref_t<_Iter>, typename iterator_traits<__uncvref_t<_Iter> >::reference>::value, 85 "It looks like your iterator's `iterator_traits<It>::reference` does not match the return type of " 86 "dereferencing the iterator, i.e., calling `*it`. This is undefined behavior according to [input.iterators] " 87 "and can lead to dangling reference issues at runtime, so we are flagging this."); 88 } 89 90 // iter_move 91 template <class _Iter> 92 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 static 93 // If the result of dereferencing `_Iter` is a reference type, deduce the result of calling `std::move` on it. Note 94 // that the C++03 mode doesn't support `decltype(auto)` as the return type. 95 __enable_if_t< 96 is_reference<__deref_t<_Iter> >::value, 97 __move_t<_Iter> > 98 __iter_move(_Iter&& __i) { 99 __validate_iter_reference<_Iter>(); 100 101 return std::move(*std::forward<_Iter>(__i)); 102 } 103 104 template <class _Iter> 105 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 static 106 // If the result of dereferencing `_Iter` is a value type, deduce the return value of this function to also be a 107 // value -- otherwise, after `operator*` returns a temporary, this function would return a dangling reference to that 108 // temporary. Note that the C++03 mode doesn't support `auto` as the return type. 109 __enable_if_t< 110 !is_reference<__deref_t<_Iter> >::value, 111 __deref_t<_Iter> > 112 __iter_move(_Iter&& __i) { 113 __validate_iter_reference<_Iter>(); 114 115 return *std::forward<_Iter>(__i); 116 } 117 118 // iter_swap 119 template <class _Iter1, class _Iter2> 120 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 121 static void iter_swap(_Iter1&& __a, _Iter2&& __b) { 122 std::iter_swap(std::forward<_Iter1>(__a), std::forward<_Iter2>(__b)); 123 } 124 125 // next 126 template <class _Iterator> 127 _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR_AFTER_CXX11 128 _Iterator next(_Iterator, _Iterator __last) { 129 return __last; 130 } 131 132 template <class _Iter> 133 _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR_AFTER_CXX11 134 __uncvref_t<_Iter> next(_Iter&& __it, 135 typename iterator_traits<__uncvref_t<_Iter> >::difference_type __n = 1){ 136 return std::next(std::forward<_Iter>(__it), __n); 137 } 138 139 template <class _Iter> 140 _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR_AFTER_CXX11 141 void __advance_to(_Iter& __first, _Iter __last) { 142 __first = __last; 143 } 144 }; 145 146 _LIBCPP_END_NAMESPACE_STD 147 148 #endif // _LIBCPP___ALGORITHM_ITERATOR_OPERATIONS_H 149