rbegin.h 3.8 KB

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