rend.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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___RANGES_REND_H
  10. #define _LIBCPP___RANGES_REND_H
  11. #include <__concepts/class_or_enum.h>
  12. #include <__concepts/same_as.h>
  13. #include <__config>
  14. #include <__iterator/concepts.h>
  15. #include <__iterator/readable_traits.h>
  16. #include <__iterator/reverse_iterator.h>
  17. #include <__ranges/access.h>
  18. #include <__ranges/rbegin.h>
  19. #include <__type_traits/decay.h>
  20. #include <__type_traits/is_reference.h>
  21. #include <__type_traits/remove_cvref.h>
  22. #include <__type_traits/remove_reference.h>
  23. #include <__utility/auto_cast.h>
  24. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  25. # pragma GCC system_header
  26. #endif
  27. _LIBCPP_BEGIN_NAMESPACE_STD
  28. #if _LIBCPP_STD_VER >= 20
  29. // [range.access.rend]
  30. namespace ranges {
  31. namespace __rend {
  32. template <class _Tp>
  33. concept __member_rend =
  34. __can_borrow<_Tp> &&
  35. __workaround_52970<_Tp> &&
  36. requires(_Tp&& __t) {
  37. ranges::rbegin(__t);
  38. { _LIBCPP_AUTO_CAST(__t.rend()) } -> sentinel_for<decltype(ranges::rbegin(__t))>;
  39. };
  40. void rend(auto&) = delete;
  41. void rend(const auto&) = delete;
  42. template <class _Tp>
  43. concept __unqualified_rend =
  44. !__member_rend<_Tp> &&
  45. __can_borrow<_Tp> &&
  46. __class_or_enum<remove_cvref_t<_Tp>> &&
  47. requires(_Tp&& __t) {
  48. ranges::rbegin(__t);
  49. { _LIBCPP_AUTO_CAST(rend(__t)) } -> sentinel_for<decltype(ranges::rbegin(__t))>;
  50. };
  51. template <class _Tp>
  52. concept __can_reverse =
  53. __can_borrow<_Tp> &&
  54. !__member_rend<_Tp> &&
  55. !__unqualified_rend<_Tp> &&
  56. requires(_Tp&& __t) {
  57. { ranges::begin(__t) } -> same_as<decltype(ranges::end(__t))>;
  58. { ranges::begin(__t) } -> bidirectional_iterator;
  59. };
  60. class __fn {
  61. public:
  62. template <class _Tp>
  63. requires __member_rend<_Tp>
  64. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
  65. noexcept(noexcept(_LIBCPP_AUTO_CAST(__t.rend())))
  66. {
  67. return _LIBCPP_AUTO_CAST(__t.rend());
  68. }
  69. template <class _Tp>
  70. requires __unqualified_rend<_Tp>
  71. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
  72. noexcept(noexcept(_LIBCPP_AUTO_CAST(rend(__t))))
  73. {
  74. return _LIBCPP_AUTO_CAST(rend(__t));
  75. }
  76. template <class _Tp>
  77. requires __can_reverse<_Tp>
  78. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
  79. noexcept(noexcept(ranges::begin(__t)))
  80. {
  81. return std::make_reverse_iterator(ranges::begin(__t));
  82. }
  83. void operator()(auto&&) const = delete;
  84. };
  85. } // namespace __rend
  86. inline namespace __cpo {
  87. inline constexpr auto rend = __rend::__fn{};
  88. } // namespace __cpo
  89. } // namespace ranges
  90. // [range.access.crend]
  91. namespace ranges {
  92. namespace __crend {
  93. struct __fn {
  94. template <class _Tp>
  95. requires is_lvalue_reference_v<_Tp&&>
  96. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
  97. constexpr auto operator()(_Tp&& __t) const
  98. noexcept(noexcept(ranges::rend(static_cast<const remove_reference_t<_Tp>&>(__t))))
  99. -> decltype( ranges::rend(static_cast<const remove_reference_t<_Tp>&>(__t)))
  100. { return ranges::rend(static_cast<const remove_reference_t<_Tp>&>(__t)); }
  101. template <class _Tp>
  102. requires is_rvalue_reference_v<_Tp&&>
  103. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
  104. constexpr auto operator()(_Tp&& __t) const
  105. noexcept(noexcept(ranges::rend(static_cast<const _Tp&&>(__t))))
  106. -> decltype( ranges::rend(static_cast<const _Tp&&>(__t)))
  107. { return ranges::rend(static_cast<const _Tp&&>(__t)); }
  108. };
  109. } // namespace __crend
  110. inline namespace __cpo {
  111. inline constexpr auto crend = __crend::__fn{};
  112. } // namespace __cpo
  113. } // namespace ranges
  114. #endif // _LIBCPP_STD_VER >= 20
  115. _LIBCPP_END_NAMESPACE_STD
  116. #endif // _LIBCPP___RANGES_REND_H