next.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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/enable_if.h>
  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, __enable_if_t<__has_input_iterator_category<_InputIter>::value, int> = 0>
  23. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 _InputIter
  24. next(_InputIter __x, typename iterator_traits<_InputIter>::difference_type __n = 1) {
  25. // Calling `advance` with a negative value on a non-bidirectional iterator is a no-op in the current implementation.
  26. // Note that this check duplicates the similar check in `std::advance`.
  27. _LIBCPP_ASSERT_PEDANTIC(__n >= 0 || __has_bidirectional_iterator_category<_InputIter>::value,
  28. "Attempt to next(it, n) with negative n on a non-bidirectional iterator");
  29. std::advance(__x, __n);
  30. return __x;
  31. }
  32. #if _LIBCPP_STD_VER >= 20
  33. // [range.iter.op.next]
  34. namespace ranges {
  35. namespace __next {
  36. struct __fn {
  37. template <input_or_output_iterator _Ip>
  38. _LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x) const {
  39. ++__x;
  40. return __x;
  41. }
  42. template <input_or_output_iterator _Ip>
  43. _LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n) const {
  44. ranges::advance(__x, __n);
  45. return __x;
  46. }
  47. template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp>
  48. _LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x, _Sp ___bound_sentinel) const {
  49. ranges::advance(__x, ___bound_sentinel);
  50. return __x;
  51. }
  52. template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp>
  53. _LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n, _Sp ___bound_sentinel) const {
  54. ranges::advance(__x, __n, ___bound_sentinel);
  55. return __x;
  56. }
  57. };
  58. } // namespace __next
  59. inline namespace __cpo {
  60. inline constexpr auto next = __next::__fn{};
  61. } // namespace __cpo
  62. } // namespace ranges
  63. #endif // _LIBCPP_STD_VER >= 20
  64. _LIBCPP_END_NAMESPACE_STD
  65. #endif // _LIBCPP___ITERATOR_NEXT_H