1 // -*- C++ -*- 2 //===----------------------------------------------------------------------===// 3 // 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 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef _LIBCPP___ITERATOR_NEXT_H 11 #define _LIBCPP___ITERATOR_NEXT_H 12 13 #include <__config> 14 #include <__debug> 15 #include <__function_like.h> 16 #include <__iterator/advance.h> 17 #include <__iterator/concepts.h> 18 #include <__iterator/incrementable_traits.h> 19 #include <__iterator/iterator_traits.h> 20 #include <type_traits> 21 22 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 23 #pragma GCC system_header 24 #endif 25 26 _LIBCPP_BEGIN_NAMESPACE_STD 27 28 template <class _InputIter> 29 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 30 typename enable_if<__is_cpp17_input_iterator<_InputIter>::value, _InputIter>::type 31 next(_InputIter __x, typename iterator_traits<_InputIter>::difference_type __n = 1) { 32 _LIBCPP_ASSERT(__n >= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value, 33 "Attempt to next(it, n) with negative n on a non-bidirectional iterator"); 34 35 _VSTD::advance(__x, __n); 36 return __x; 37 } 38 39 #if !defined(_LIBCPP_HAS_NO_RANGES) 40 41 namespace ranges { 42 // TODO(varconst): rename `__next_fn` to `__fn`. 43 struct __next_fn final : private __function_like { 44 _LIBCPP_HIDE_FROM_ABI 45 constexpr explicit __next_fn(__tag __x) noexcept : __function_like(__x) {} 46 47 template <input_or_output_iterator _Ip> 48 _LIBCPP_HIDE_FROM_ABI 49 constexpr _Ip operator()(_Ip __x) const { 50 ++__x; 51 return __x; 52 } 53 54 template <input_or_output_iterator _Ip> 55 _LIBCPP_HIDE_FROM_ABI 56 constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n) const { 57 ranges::advance(__x, __n); 58 return __x; 59 } 60 61 template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp> 62 _LIBCPP_HIDE_FROM_ABI 63 constexpr _Ip operator()(_Ip __x, _Sp __bound) const { 64 ranges::advance(__x, __bound); 65 return __x; 66 } 67 68 template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp> 69 _LIBCPP_HIDE_FROM_ABI 70 constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n, _Sp __bound) const { 71 ranges::advance(__x, __n, __bound); 72 return __x; 73 } 74 }; 75 76 inline constexpr auto next = __next_fn(__function_like::__tag()); 77 } // namespace ranges 78 79 #endif // !defined(_LIBCPP_HAS_NO_RANGES) 80 81 _LIBCPP_END_NAMESPACE_STD 82 83 #endif // _LIBCPP___ITERATOR_NEXT_H 84