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(std::forward<F>(f)(std::forward<Args>(args)...)))
51 -> decltype(std::forward<F>(f)(std::forward<Args>(args)...)) {
52 return std::forward<F>(f)(std::forward<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)(std::forward<Args>(args)...)) {
57 return std::mem_fn(d)(std::forward<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 using invoke_nothrow_ = std::integral_constant<
91 noexcept(invoke(std::declval<F>(), std::declval<Args>()...))>;
93 // from: http://en.cppreference.com/w/cpp/types/result_of, CC-BY-SA
95 template <typename Void, typename F, typename... Args>
96 struct invoke_result {};
98 template <typename F, typename... Args>
99 struct invoke_result<void_t<invoke_result_<F, Args...>>, F, Args...> {
100 using type = invoke_result_<F, Args...>;
103 template <typename Void, typename F, typename... Args>
104 struct is_invocable : std::false_type {};
106 template <typename F, typename... Args>
107 struct is_invocable<void_t<invoke_result_<F, Args...>>, F, Args...>
110 template <typename Void, typename R, typename F, typename... Args>
111 struct is_invocable_r : std::false_type {};
113 template <typename R, typename F, typename... Args>
114 struct is_invocable_r<void_t<invoke_result_<F, Args...>>, R, F, Args...>
115 : std::is_convertible<invoke_result_<F, Args...>, R> {};
117 template <typename Void, typename F, typename... Args>
118 struct is_nothrow_invocable : std::false_type {};
120 template <typename F, typename... Args>
121 struct is_nothrow_invocable<void_t<invoke_result_<F, Args...>>, F, Args...>
122 : invoke_nothrow_<F, Args...> {};
124 template <typename Void, typename R, typename F, typename... Args>
125 struct is_nothrow_invocable_r : std::false_type {};
127 template <typename R, typename F, typename... Args>
128 struct is_nothrow_invocable_r<void_t<invoke_result_<F, Args...>>, R, F, Args...>
130 std::is_convertible<invoke_result_<F, Args...>, R>,
131 invoke_nothrow_<F, Args...>> {};
133 } // namespace detail
135 // mimic: std::invoke_result, C++17
136 template <typename F, typename... Args>
137 struct invoke_result : detail::invoke_result<void, F, Args...> {};
139 // mimic: std::invoke_result_t, C++17
140 template <typename F, typename... Args>
141 using invoke_result_t = typename invoke_result<F, Args...>::type;
143 // mimic: std::is_invocable, C++17
144 template <typename F, typename... Args>
145 struct is_invocable : detail::is_invocable<void, F, Args...> {};
147 // mimic: std::is_invocable_r, C++17
148 template <typename R, typename F, typename... Args>
149 struct is_invocable_r : detail::is_invocable_r<void, R, F, Args...> {};
151 // mimic: std::is_nothrow_invocable, C++17
152 template <typename F, typename... Args>
153 struct is_nothrow_invocable : detail::is_nothrow_invocable<void, F, Args...> {};
155 // mimic: std::is_nothrow_invocable_r, C++17
156 template <typename R, typename F, typename... Args>
157 struct is_nothrow_invocable_r
158 : detail::is_nothrow_invocable_r<void, R, F, Args...> {};