iter_move.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 <__config>
  12. #include <__iterator/iterator_traits.h>
  13. #include <__utility/forward.h>
  14. #include <__utility/move.h>
  15. #include <concepts> // __class_or_enum
  16. #include <type_traits>
  17. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  18. # pragma GCC system_header
  19. #endif
  20. _LIBCPP_BEGIN_NAMESPACE_STD
  21. #if !defined(_LIBCPP_HAS_NO_CONCEPTS)
  22. // [iterator.cust.move]
  23. namespace ranges {
  24. namespace __iter_move {
  25. void iter_move();
  26. template <class _Tp>
  27. concept __unqualified_iter_move =
  28. __class_or_enum<remove_cvref_t<_Tp>> &&
  29. requires (_Tp&& __t) {
  30. iter_move(_VSTD::forward<_Tp>(__t));
  31. };
  32. // [iterator.cust.move]/1
  33. // The name ranges::iter_move denotes a customization point object.
  34. // The expression ranges::iter_move(E) for a subexpression E is
  35. // expression-equivalent to:
  36. struct __fn {
  37. // [iterator.cust.move]/1.1
  38. // iter_move(E), if E has class or enumeration type and iter_move(E) is a
  39. // well-formed expression when treated as an unevaluated operand, [...]
  40. template<class _Ip>
  41. requires __class_or_enum<remove_cvref_t<_Ip>> && __unqualified_iter_move<_Ip>
  42. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator()(_Ip&& __i) const
  43. noexcept(noexcept(iter_move(_VSTD::forward<_Ip>(__i))))
  44. {
  45. return iter_move(_VSTD::forward<_Ip>(__i));
  46. }
  47. // [iterator.cust.move]/1.2
  48. // Otherwise, if the expression *E is well-formed:
  49. // 1.2.1 if *E is an lvalue, std::move(*E);
  50. // 1.2.2 otherwise, *E.
  51. template<class _Ip>
  52. requires (!(__class_or_enum<remove_cvref_t<_Ip>> && __unqualified_iter_move<_Ip>)) &&
  53. requires(_Ip&& __i) { *_VSTD::forward<_Ip>(__i); }
  54. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator()(_Ip&& __i) const
  55. noexcept(noexcept(*_VSTD::forward<_Ip>(__i)))
  56. {
  57. if constexpr (is_lvalue_reference_v<decltype(*_VSTD::forward<_Ip>(__i))>) {
  58. return _VSTD::move(*_VSTD::forward<_Ip>(__i));
  59. } else {
  60. return *_VSTD::forward<_Ip>(__i);
  61. }
  62. }
  63. // [iterator.cust.move]/1.3
  64. // Otherwise, ranges::iter_move(E) is ill-formed.
  65. };
  66. } // namespace __iter_move
  67. inline namespace __cpo {
  68. inline constexpr auto iter_move = __iter_move::__fn{};
  69. } // namespace __cpo
  70. } // namespace ranges
  71. template<__dereferenceable _Tp>
  72. requires requires(_Tp& __t) { { ranges::iter_move(__t) } -> __can_reference; }
  73. using iter_rvalue_reference_t = decltype(ranges::iter_move(declval<_Tp&>()));
  74. #endif // !defined(_LIBCPP_HAS_NO_CONCEPTS)
  75. _LIBCPP_END_NAMESPACE_STD
  76. #endif // _LIBCPP___ITERATOR_ITER_MOVE_H