xref: /freebsd/contrib/llvm-project/libcxx/include/__iterator/next.h (revision 5956d97f4b3204318ceb6aa9c77bd0bc6ea87a41)
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 <__iterator/advance.h>
16 #include <__iterator/concepts.h>
17 #include <__iterator/incrementable_traits.h>
18 #include <__iterator/iterator_traits.h>
19 #include <type_traits>
20 
21 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22 #pragma GCC system_header
23 #endif
24 
25 _LIBCPP_BEGIN_NAMESPACE_STD
26 
27 template <class _InputIter>
28 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
29     typename enable_if<__is_cpp17_input_iterator<_InputIter>::value, _InputIter>::type
30     next(_InputIter __x, typename iterator_traits<_InputIter>::difference_type __n = 1) {
31   _LIBCPP_ASSERT(__n >= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
32                  "Attempt to next(it, n) with negative n on a non-bidirectional iterator");
33 
34   _VSTD::advance(__x, __n);
35   return __x;
36 }
37 
38 #if !defined(_LIBCPP_HAS_NO_CONCEPTS) && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
39 
40 // [range.iter.op.next]
41 
42 namespace ranges {
43 namespace __next {
44 
45 struct __fn {
46   template <input_or_output_iterator _Ip>
47   _LIBCPP_HIDE_FROM_ABI
48   constexpr _Ip operator()(_Ip __x) const {
49     ++__x;
50     return __x;
51   }
52 
53   template <input_or_output_iterator _Ip>
54   _LIBCPP_HIDE_FROM_ABI
55   constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n) const {
56     ranges::advance(__x, __n);
57     return __x;
58   }
59 
60   template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp>
61   _LIBCPP_HIDE_FROM_ABI
62   constexpr _Ip operator()(_Ip __x, _Sp __bound) const {
63     ranges::advance(__x, __bound);
64     return __x;
65   }
66 
67   template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp>
68   _LIBCPP_HIDE_FROM_ABI
69   constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n, _Sp __bound) const {
70     ranges::advance(__x, __n, __bound);
71     return __x;
72   }
73 };
74 
75 } // namespace __next
76 
77 inline namespace __cpo {
78   inline constexpr auto next = __next::__fn{};
79 } // namespace __cpo
80 } // namespace ranges
81 
82 #endif // !defined(_LIBCPP_HAS_NO_CONCEPTS) && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
83 
84 _LIBCPP_END_NAMESPACE_STD
85 
86 #endif // _LIBCPP___ITERATOR_NEXT_H
87