rbegin.h 3.7 KB

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