invoke.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Implementation of INVOKE -*- C++ -*-
  2. // Copyright (C) 2016-2023 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file include/bits/invoke.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{functional}
  23. */
  24. #ifndef _GLIBCXX_INVOKE_H
  25. #define _GLIBCXX_INVOKE_H 1
  26. #pragma GCC system_header
  27. #if __cplusplus < 201103L
  28. # include <bits/c++0x_warning.h>
  29. #else
  30. #include <type_traits>
  31. #include <bits/move.h> // forward
  32. namespace std _GLIBCXX_VISIBILITY(default)
  33. {
  34. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  35. /**
  36. * @addtogroup utilities
  37. * @{
  38. */
  39. // Used by __invoke_impl instead of std::forward<_Tp> so that a
  40. // reference_wrapper is converted to an lvalue-reference.
  41. template<typename _Tp, typename _Up = typename __inv_unwrap<_Tp>::type>
  42. constexpr _Up&&
  43. __invfwd(typename remove_reference<_Tp>::type& __t) noexcept
  44. { return static_cast<_Up&&>(__t); }
  45. template<typename _Res, typename _Fn, typename... _Args>
  46. constexpr _Res
  47. __invoke_impl(__invoke_other, _Fn&& __f, _Args&&... __args)
  48. { return std::forward<_Fn>(__f)(std::forward<_Args>(__args)...); }
  49. template<typename _Res, typename _MemFun, typename _Tp, typename... _Args>
  50. constexpr _Res
  51. __invoke_impl(__invoke_memfun_ref, _MemFun&& __f, _Tp&& __t,
  52. _Args&&... __args)
  53. { return (__invfwd<_Tp>(__t).*__f)(std::forward<_Args>(__args)...); }
  54. template<typename _Res, typename _MemFun, typename _Tp, typename... _Args>
  55. constexpr _Res
  56. __invoke_impl(__invoke_memfun_deref, _MemFun&& __f, _Tp&& __t,
  57. _Args&&... __args)
  58. {
  59. return ((*std::forward<_Tp>(__t)).*__f)(std::forward<_Args>(__args)...);
  60. }
  61. template<typename _Res, typename _MemPtr, typename _Tp>
  62. constexpr _Res
  63. __invoke_impl(__invoke_memobj_ref, _MemPtr&& __f, _Tp&& __t)
  64. { return __invfwd<_Tp>(__t).*__f; }
  65. template<typename _Res, typename _MemPtr, typename _Tp>
  66. constexpr _Res
  67. __invoke_impl(__invoke_memobj_deref, _MemPtr&& __f, _Tp&& __t)
  68. { return (*std::forward<_Tp>(__t)).*__f; }
  69. /// Invoke a callable object.
  70. template<typename _Callable, typename... _Args>
  71. constexpr typename __invoke_result<_Callable, _Args...>::type
  72. __invoke(_Callable&& __fn, _Args&&... __args)
  73. noexcept(__is_nothrow_invocable<_Callable, _Args...>::value)
  74. {
  75. using __result = __invoke_result<_Callable, _Args...>;
  76. using __type = typename __result::type;
  77. using __tag = typename __result::__invoke_type;
  78. return std::__invoke_impl<__type>(__tag{}, std::forward<_Callable>(__fn),
  79. std::forward<_Args>(__args)...);
  80. }
  81. #if __cplusplus >= 201703L
  82. // INVOKE<R>: Invoke a callable object and convert the result to R.
  83. template<typename _Res, typename _Callable, typename... _Args>
  84. constexpr enable_if_t<is_invocable_r_v<_Res, _Callable, _Args...>, _Res>
  85. __invoke_r(_Callable&& __fn, _Args&&... __args)
  86. noexcept(is_nothrow_invocable_r_v<_Res, _Callable, _Args...>)
  87. {
  88. using __result = __invoke_result<_Callable, _Args...>;
  89. using __type = typename __result::type;
  90. using __tag = typename __result::__invoke_type;
  91. if constexpr (is_void_v<_Res>)
  92. std::__invoke_impl<__type>(__tag{}, std::forward<_Callable>(__fn),
  93. std::forward<_Args>(__args)...);
  94. else
  95. return std::__invoke_impl<__type>(__tag{},
  96. std::forward<_Callable>(__fn),
  97. std::forward<_Args>(__args)...);
  98. }
  99. #else // C++11 or C++14
  100. // This is a non-SFINAE-friendly std::invoke_r<R>(fn, args...) for C++11/14.
  101. // It's used in std::function, std::bind, and std::packaged_task. Only
  102. // std::function is constrained on is_invocable_r, but that is checked on
  103. // construction so doesn't need to be checked again when calling __invoke_r.
  104. // Consequently, these __invoke_r overloads do not check for invocable
  105. // arguments, nor check that the invoke result is convertible to R.
  106. // INVOKE<R>: Invoke a callable object and convert the result to R.
  107. template<typename _Res, typename _Callable, typename... _Args>
  108. constexpr __enable_if_t<!is_void<_Res>::value, _Res>
  109. __invoke_r(_Callable&& __fn, _Args&&... __args)
  110. {
  111. using __result = __invoke_result<_Callable, _Args...>;
  112. using __type = typename __result::type;
  113. #if __has_builtin(__reference_converts_from_temporary)
  114. static_assert(!__reference_converts_from_temporary(_Res, __type),
  115. "INVOKE<R> must not create a dangling reference");
  116. #endif
  117. using __tag = typename __result::__invoke_type;
  118. return std::__invoke_impl<__type>(__tag{}, std::forward<_Callable>(__fn),
  119. std::forward<_Args>(__args)...);
  120. }
  121. // INVOKE<R> when R is cv void
  122. template<typename _Res, typename _Callable, typename... _Args>
  123. _GLIBCXX14_CONSTEXPR __enable_if_t<is_void<_Res>::value, _Res>
  124. __invoke_r(_Callable&& __fn, _Args&&... __args)
  125. {
  126. using __result = __invoke_result<_Callable, _Args...>;
  127. using __type = typename __result::type;
  128. using __tag = typename __result::__invoke_type;
  129. std::__invoke_impl<__type>(__tag{}, std::forward<_Callable>(__fn),
  130. std::forward<_Args>(__args)...);
  131. }
  132. #endif // C++11 or C++14
  133. _GLIBCXX_END_NAMESPACE_VERSION
  134. } // namespace std
  135. #endif // C++11
  136. #endif // _GLIBCXX_INVOKE_H