iter_move.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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_ITER_MOVE_H
  10. #define _LIBCPP___ITERATOR_ITER_MOVE_H
  11. #include <__concepts/class_or_enum.h>
  12. #include <__config>
  13. #include <__iterator/iterator_traits.h>
  14. #include <__type_traits/is_reference.h>
  15. #include <__type_traits/remove_cvref.h>
  16. #include <__utility/declval.h>
  17. #include <__utility/forward.h>
  18. #include <__utility/move.h>
  19. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  20. # pragma GCC system_header
  21. #endif
  22. _LIBCPP_PUSH_MACROS
  23. #include <__undef_macros>
  24. _LIBCPP_BEGIN_NAMESPACE_STD
  25. #if _LIBCPP_STD_VER >= 20
  26. // [iterator.cust.move]
  27. namespace ranges {
  28. namespace __iter_move {
  29. void iter_move();
  30. template <class _Tp>
  31. concept __unqualified_iter_move =
  32. __class_or_enum<remove_cvref_t<_Tp>> &&
  33. requires (_Tp&& __t) {
  34. // NOLINTNEXTLINE(libcpp-robust-against-adl) iter_swap ADL calls should only be made through ranges::iter_swap
  35. iter_move(std::forward<_Tp>(__t));
  36. };
  37. template<class _Tp>
  38. concept __move_deref =
  39. !__unqualified_iter_move<_Tp> &&
  40. requires (_Tp&& __t) {
  41. *__t;
  42. requires is_lvalue_reference_v<decltype(*__t)>;
  43. };
  44. template<class _Tp>
  45. concept __just_deref =
  46. !__unqualified_iter_move<_Tp> &&
  47. !__move_deref<_Tp> &&
  48. requires (_Tp&& __t) {
  49. *__t;
  50. requires (!is_lvalue_reference_v<decltype(*__t)>);
  51. };
  52. // [iterator.cust.move]
  53. struct __fn {
  54. // NOLINTBEGIN(libcpp-robust-against-adl) iter_move ADL calls should only be made through ranges::iter_move
  55. template<class _Ip>
  56. requires __unqualified_iter_move<_Ip>
  57. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator()(_Ip&& __i) const
  58. noexcept(noexcept(iter_move(std::forward<_Ip>(__i))))
  59. {
  60. return iter_move(std::forward<_Ip>(__i));
  61. }
  62. // NOLINTEND(libcpp-robust-against-adl)
  63. template<class _Ip>
  64. requires __move_deref<_Ip>
  65. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Ip&& __i) const
  66. noexcept(noexcept(std::move(*std::forward<_Ip>(__i))))
  67. -> decltype( std::move(*std::forward<_Ip>(__i)))
  68. { return std::move(*std::forward<_Ip>(__i)); }
  69. template<class _Ip>
  70. requires __just_deref<_Ip>
  71. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Ip&& __i) const
  72. noexcept(noexcept(*std::forward<_Ip>(__i)))
  73. -> decltype( *std::forward<_Ip>(__i))
  74. { return *std::forward<_Ip>(__i); }
  75. };
  76. } // namespace __iter_move
  77. inline namespace __cpo {
  78. inline constexpr auto iter_move = __iter_move::__fn{};
  79. } // namespace __cpo
  80. } // namespace ranges
  81. template<__dereferenceable _Tp>
  82. requires requires(_Tp& __t) { { ranges::iter_move(__t) } -> __can_reference; }
  83. using iter_rvalue_reference_t = decltype(ranges::iter_move(std::declval<_Tp&>()));
  84. #endif // _LIBCPP_STD_VER >= 20
  85. _LIBCPP_END_NAMESPACE_STD
  86. _LIBCPP_POP_MACROS
  87. #endif // _LIBCPP___ITERATOR_ITER_MOVE_H