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_PUSH_MACROS
27 #include <__undef_macros>
28 
29 _LIBCPP_BEGIN_NAMESPACE_STD
30 
31 template <class _InputIter>
32 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
33     typename enable_if<__is_cpp17_input_iterator<_InputIter>::value, _InputIter>::type
34     next(_InputIter __x, typename iterator_traits<_InputIter>::difference_type __n = 1) {
35   _LIBCPP_ASSERT(__n >= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
36                  "Attempt to next(it, n) with negative n on a non-bidirectional iterator");
37 
38   _VSTD::advance(__x, __n);
39   return __x;
40 }
41 
42 #if !defined(_LIBCPP_HAS_NO_RANGES)
43 
44 namespace ranges {
45 struct __next_fn final : private __function_like {
46   _LIBCPP_HIDE_FROM_ABI
47   constexpr explicit __next_fn(__tag __x) noexcept : __function_like(__x) {}
48 
49   template <input_or_output_iterator _Ip>
50   _LIBCPP_HIDE_FROM_ABI
51   constexpr _Ip operator()(_Ip __x) const {
52     ++__x;
53     return __x;
54   }
55 
56   template <input_or_output_iterator _Ip>
57   _LIBCPP_HIDE_FROM_ABI
58   constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n) const {
59     ranges::advance(__x, __n);
60     return __x;
61   }
62 
63   template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp>
64   _LIBCPP_HIDE_FROM_ABI
65   constexpr _Ip operator()(_Ip __x, _Sp __bound) const {
66     ranges::advance(__x, __bound);
67     return __x;
68   }
69 
70   template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp>
71   _LIBCPP_HIDE_FROM_ABI
72   constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n, _Sp __bound) const {
73     ranges::advance(__x, __n, __bound);
74     return __x;
75   }
76 };
77 
78 inline constexpr auto next = __next_fn(__function_like::__tag());
79 } // namespace ranges
80 
81 #endif // !defined(_LIBCPP_HAS_NO_RANGES)
82 
83 _LIBCPP_END_NAMESPACE_STD
84 
85 _LIBCPP_POP_MACROS
86 
87 #endif // _LIBCPP___ITERATOR_PRIMITIVES_H
88