iter_move.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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() = delete;
  30. template <class _Tp>
  31. concept __unqualified_iter_move = __class_or_enum<remove_cvref_t<_Tp>> && requires(_Tp&& __t) {
  32. // NOLINTNEXTLINE(libcpp-robust-against-adl) iter_swap ADL calls should only be made through ranges::iter_swap
  33. iter_move(std::forward<_Tp>(__t));
  34. };
  35. template <class _Tp>
  36. concept __move_deref = !__unqualified_iter_move<_Tp> && requires(_Tp&& __t) {
  37. *__t;
  38. requires is_lvalue_reference_v<decltype(*__t)>;
  39. };
  40. template <class _Tp>
  41. concept __just_deref = !__unqualified_iter_move<_Tp> && !__move_deref<_Tp> && requires(_Tp&& __t) {
  42. *__t;
  43. requires(!is_lvalue_reference_v<decltype(*__t)>);
  44. };
  45. // [iterator.cust.move]
  46. struct __fn {
  47. // NOLINTBEGIN(libcpp-robust-against-adl) iter_move ADL calls should only be made through ranges::iter_move
  48. template <class _Ip>
  49. requires __unqualified_iter_move<_Ip>
  50. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator()(_Ip&& __i) const
  51. noexcept(noexcept(iter_move(std::forward<_Ip>(__i)))) {
  52. return iter_move(std::forward<_Ip>(__i));
  53. }
  54. // NOLINTEND(libcpp-robust-against-adl)
  55. template <class _Ip>
  56. requires __move_deref<_Ip>
  57. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Ip&& __i) const
  58. noexcept(noexcept(std::move(*std::forward<_Ip>(__i)))) -> decltype(std::move(*std::forward<_Ip>(__i))) {
  59. return std::move(*std::forward<_Ip>(__i));
  60. }
  61. template <class _Ip>
  62. requires __just_deref<_Ip>
  63. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Ip&& __i) const
  64. noexcept(noexcept(*std::forward<_Ip>(__i))) -> decltype(*std::forward<_Ip>(__i)) {
  65. return *std::forward<_Ip>(__i);
  66. }
  67. };
  68. } // namespace __iter_move
  69. inline namespace __cpo {
  70. inline constexpr auto iter_move = __iter_move::__fn{};
  71. } // namespace __cpo
  72. } // namespace ranges
  73. template <__dereferenceable _Tp>
  74. requires requires(_Tp& __t) {
  75. { ranges::iter_move(__t) } -> __can_reference;
  76. }
  77. using iter_rvalue_reference_t = decltype(ranges::iter_move(std::declval<_Tp&>()));
  78. #endif // _LIBCPP_STD_VER >= 20
  79. _LIBCPP_END_NAMESPACE_STD
  80. _LIBCPP_POP_MACROS
  81. #endif // _LIBCPP___ITERATOR_ITER_MOVE_H