2 * Copyright 2017-present Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <type_traits>
22 #include <folly/Traits.h>
25 * include or backport:
27 * * std::invoke_result
28 * * std::invoke_result_t
30 * * std::is_invocable_r
31 * * std::is_nothrow_invocable
32 * * std::is_nothrow_invocable_r
35 #if __cpp_lib_invoke >= 201411 || _MSC_VER
39 /* using override */ using std::invoke;
47 // mimic: std::invoke, C++17
48 template <typename F, typename... Args>
49 constexpr auto invoke(F&& f, Args&&... args) noexcept(
50 noexcept(static_cast<F&&>(f)(static_cast<Args&&>(args)...)))
51 -> decltype(static_cast<F&&>(f)(static_cast<Args&&>(args)...)) {
52 return static_cast<F&&>(f)(static_cast<Args&&>(args)...);
54 template <typename M, typename C, typename... Args>
55 constexpr auto invoke(M(C::*d), Args&&... args)
56 -> decltype(std::mem_fn(d)(static_cast<Args&&>(args)...)) {
57 return std::mem_fn(d)(static_cast<Args&&>(args)...);
64 // Only available in >= MSVC 2017 15.3
65 #if __cpp_lib_is_invocable >= 201703 || _MSC_VER >= 1911
69 /* using override */ using std::invoke_result;
70 /* using override */ using std::invoke_result_t;
71 /* using override */ using std::is_invocable;
72 /* using override */ using std::is_invocable_r;
73 /* using override */ using std::is_nothrow_invocable;
74 /* using override */ using std::is_nothrow_invocable_r;
84 template <typename F, typename... Args>
85 using invoke_result_ =
86 decltype(invoke(std::declval<F>(), std::declval<Args>()...));
88 template <typename F, typename... Args>
89 struct invoke_nothrow_
90 : std::integral_constant<
92 noexcept(invoke(std::declval<F>(), std::declval<Args>()...))> {};
94 // from: http://en.cppreference.com/w/cpp/types/result_of, CC-BY-SA
96 template <typename Void, typename F, typename... Args>
97 struct invoke_result {};
99 template <typename F, typename... Args>
100 struct invoke_result<void_t<invoke_result_<F, Args...>>, F, Args...> {
101 using type = invoke_result_<F, Args...>;
104 template <typename Void, typename F, typename... Args>
105 struct is_invocable : std::false_type {};
107 template <typename F, typename... Args>
108 struct is_invocable<void_t<invoke_result_<F, Args...>>, F, Args...>
111 template <typename Void, typename R, typename F, typename... Args>
112 struct is_invocable_r : std::false_type {};
114 template <typename R, typename F, typename... Args>
115 struct is_invocable_r<void_t<invoke_result_<F, Args...>>, R, F, Args...>
116 : std::is_convertible<invoke_result_<F, Args...>, R> {};
118 template <typename Void, typename F, typename... Args>
119 struct is_nothrow_invocable : std::false_type {};
121 template <typename F, typename... Args>
122 struct is_nothrow_invocable<void_t<invoke_result_<F, Args...>>, F, Args...>
123 : invoke_nothrow_<F, Args...> {};
125 template <typename Void, typename R, typename F, typename... Args>
126 struct is_nothrow_invocable_r : std::false_type {};
128 template <typename R, typename F, typename... Args>
129 struct is_nothrow_invocable_r<void_t<invoke_result_<F, Args...>>, R, F, Args...>
131 std::is_convertible<invoke_result_<F, Args...>, R>,
132 invoke_nothrow_<F, Args...>> {};
134 } // namespace detail
136 // mimic: std::invoke_result, C++17
137 template <typename F, typename... Args>
138 struct invoke_result : detail::invoke_result<void, F, Args...> {};
140 // mimic: std::invoke_result_t, C++17
141 template <typename F, typename... Args>
142 using invoke_result_t = typename invoke_result<F, Args...>::type;
144 // mimic: std::is_invocable, C++17
145 template <typename F, typename... Args>
146 struct is_invocable : detail::is_invocable<void, F, Args...> {};
148 // mimic: std::is_invocable_r, C++17
149 template <typename R, typename F, typename... Args>
150 struct is_invocable_r : detail::is_invocable_r<void, R, F, Args...> {};
152 // mimic: std::is_nothrow_invocable, C++17
153 template <typename F, typename... Args>
154 struct is_nothrow_invocable : detail::is_nothrow_invocable<void, F, Args...> {};
156 // mimic: std::is_nothrow_invocable_r, C++17
157 template <typename R, typename F, typename... Args>
158 struct is_nothrow_invocable_r
159 : detail::is_nothrow_invocable_r<void, R, F, Args...> {};
168 template <typename Invoke>
169 struct member_invoke_proxy {
171 template <typename O, typename... Args>
172 struct invoke_result : folly::invoke_result<Invoke, O, Args...> {};
173 template <typename O, typename... Args>
174 using invoke_result_t = folly::invoke_result_t<Invoke, O, Args...>;
175 template <typename O, typename... Args>
176 struct is_invocable : folly::is_invocable<Invoke, O, Args...> {};
177 template <typename R, typename O, typename... Args>
178 struct is_invocable_r : folly::is_invocable_r<R, Invoke, O, Args...> {};
179 template <typename O, typename... Args>
180 struct is_nothrow_invocable
181 : folly::is_nothrow_invocable<Invoke, O, Args...> {};
182 template <typename R, typename O, typename... Args>
183 struct is_nothrow_invocable_r
184 : folly::is_nothrow_invocable_r<R, Invoke, O, Args...> {};
186 template <typename O, typename... Args>
187 static constexpr auto invoke(O&& o, Args&&... args) noexcept(
188 noexcept(folly::invoke(
191 static_cast<Args&&>(args)...)))
192 -> decltype(folly::invoke(
195 static_cast<Args&&>(args)...)) {
196 return folly::invoke(
197 Invoke{}, static_cast<O&&>(o), static_cast<Args&&>(args)...);
201 } // namespace detail
205 * FOLLY_CREATE_MEMBER_INVOKE_TRAITS
207 * Used to create traits container, bound to a specific member-invocable name,
208 * with the following member traits types and aliases:
214 * * is_nothrow_invocable
215 * * is_nothrow_invocable_r
217 * The container also has a static member function:
221 * These members have behavior matching the behavior of C++17's corresponding
222 * invocation traits types, aliases, and functions, but substituting canonical
223 * invocation with member invocation.
227 * FOLLY_CREATE_MEMBER_INVOKE_TRAITS(foo_invoke_traits, foo);
229 * The traits container type `foo_invoke_traits` is generated in the current
230 * namespace and has the listed member types and aliases. They may be used as
234 * int foo(Bar const&) { return 1; }
235 * int foo(Car&&) noexcept { return 2; }
238 * using traits = foo_invoke_traits;
240 * traits::invoke(CanFoo{}, Bar{}) // 1
242 * traits::invoke_result<CanFoo, Bar&&> // has member
243 * traits::invoke_result_t<CanFoo, Bar&&> // int
244 * traits::invoke_result<CanFoo, Bar&> // empty
245 * traits::invoke_result_t<CanFoo, Bar&> // error
247 * traits::is_invocable<CanFoo, Bar&&>::value // true
248 * traits::is_invocable<CanFoo, Bar&>::value // false
250 * traits::is_invocable_r<int, CanFoo, Bar&&>::value // true
251 * traits::is_invocable_r<char*, CanFoo, Bar&&>::value // false
253 * traits::is_nothrow_invocable<CanFoo, Bar&&>::value // false
254 * traits::is_nothrow_invocable<CanFoo, Car&&>::value // true
256 * traits::is_nothrow_invocable<int, CanFoo, Bar&&>::value // false
257 * traits::is_nothrow_invocable<char*, CanFoo, Bar&&>::value // false
258 * traits::is_nothrow_invocable<int, CanFoo, Car&&>::value // true
259 * traits::is_nothrow_invocable<char*, CanFoo, Car&&>::value // false
261 #define FOLLY_CREATE_MEMBER_INVOKE_TRAITS(classname, membername) \
262 struct classname##__folly_detail_member_invoke { \
263 template <typename O, typename... Args> \
264 constexpr auto operator()(O&& o, Args&&... args) noexcept(noexcept( \
265 static_cast<O&&>(o).membername(static_cast<Args&&>(args)...))) \
267 static_cast<O&&>(o).membername(static_cast<Args&&>(args)...)) { \
268 return static_cast<O&&>(o).membername(static_cast<Args&&>(args)...); \
271 struct classname : ::folly::detail::member_invoke_proxy< \
272 classname##__folly_detail_member_invoke> {}