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