next.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #ifndef _LIBCPP___ITERATOR_NEXT_H
  10. #define _LIBCPP___ITERATOR_NEXT_H
  11. #include <__assert>
  12. #include <__config>
  13. #include <__iterator/advance.h>
  14. #include <__iterator/concepts.h>
  15. #include <__iterator/incrementable_traits.h>
  16. #include <__iterator/iterator_traits.h>
  17. #include <type_traits>
  18. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  19. # pragma GCC system_header
  20. #endif
  21. _LIBCPP_BEGIN_NAMESPACE_STD
  22. template <class _InputIter>
  23. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  24. typename enable_if<__is_cpp17_input_iterator<_InputIter>::value, _InputIter>::type
  25. next(_InputIter __x, typename iterator_traits<_InputIter>::difference_type __n = 1) {
  26. _LIBCPP_ASSERT(__n >= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
  27. "Attempt to next(it, n) with negative n on a non-bidirectional iterator");
  28. _VSTD::advance(__x, __n);
  29. return __x;
  30. }
  31. #if !defined(_LIBCPP_HAS_NO_CONCEPTS) && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
  32. // [range.iter.op.next]
  33. namespace ranges {
  34. namespace __next {
  35. struct __fn {
  36. template <input_or_output_iterator _Ip>
  37. _LIBCPP_HIDE_FROM_ABI
  38. constexpr _Ip operator()(_Ip __x) const {
  39. ++__x;
  40. return __x;
  41. }
  42. template <input_or_output_iterator _Ip>
  43. _LIBCPP_HIDE_FROM_ABI
  44. constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n) const {
  45. ranges::advance(__x, __n);
  46. return __x;
  47. }
  48. template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp>
  49. _LIBCPP_HIDE_FROM_ABI
  50. constexpr _Ip operator()(_Ip __x, _Sp ___bound) const {
  51. ranges::advance(__x, ___bound);
  52. return __x;
  53. }
  54. template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp>
  55. _LIBCPP_HIDE_FROM_ABI
  56. constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n, _Sp ___bound) const {
  57. ranges::advance(__x, __n, ___bound);
  58. return __x;
  59. }
  60. };
  61. } // namespace __next
  62. inline namespace __cpo {
  63. inline constexpr auto next = __next::__fn{};
  64. } // namespace __cpo
  65. } // namespace ranges
  66. #endif // !defined(_LIBCPP_HAS_NO_CONCEPTS) && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
  67. _LIBCPP_END_NAMESPACE_STD
  68. #endif // _LIBCPP___ITERATOR_NEXT_H