std::result_of, std::invoke_result
來自 cppreference.com
定義於標頭檔案 <type_traits> |
||
template< class > class result_of; // 未定義 |
(1) | (C++11 起) (C++17 中已棄用) (C++20 中移除) |
template< class F, class... ArgTypes > class invoke_result; |
(2) | (C++17 起) |
在編譯時推導 INVOKE
表示式的返回型別。
|
(C++11 起) (直至 C++14) |
|
(C++14 起) |
如果程式為此頁上描述的任何模板新增特化,則行為未定義。
目錄 |
[編輯] 成員型別
成員型別 | 定義 |
型別
|
用引數 ArgTypes... 呼叫 可呼叫型別 F 的返回型別。僅當 F 可以在未求值語境中用引數 ArgTypes... 呼叫時才定義。(C++14 起) |
[編輯] 輔助型別
template< class T > using result_of_t = typename result_of<T>::type; |
(1) | (C++14 起) (C++17 中已棄用) (C++20 中移除) |
template< class F, class... ArgTypes > using invoke_result_t = typename invoke_result<F, ArgTypes...>::type; |
(2) | (C++17 起) |
[編輯] 可能的實現
namespace detail { template<class T> struct is_reference_wrapper : std::false_type {}; template<class U> struct is_reference_wrapper<std::reference_wrapper<U>> : std::true_type {}; template<class T> struct invoke_impl { template<class F, class... Args> static auto call(F&& f, Args&&... args) -> decltype(std::forward<F>(f)(std::forward<Args>(args)...)); }; template<class B, class MT> struct invoke_impl<MT B::*> { template<class T, class Td = typename std::decay<T>::type, class = typename std::enable_if<std::is_base_of<B, Td>::value>::type> static auto get(T&& t) -> T&&; template<class T, class Td = typename std::decay<T>::type, class = typename std::enable_if<is_reference_wrapper<Td>::value>::type> static auto get(T&& t) -> decltype(t.get()); template<class T, class Td = typename std::decay<T>::type, class = typename std::enable_if<!std::is_base_of<B, Td>::value>::type, class = typename std::enable_if<!is_reference_wrapper<Td>::value>::type> static auto get(T&& t) -> decltype(*std::forward<T>(t)); template<class T, class... Args, class MT1, class = typename std::enable_if<std::is_function<MT1>::value>::type> static auto call(MT1 B::*pmf, T&& t, Args&&... args) -> decltype((invoke_impl::get( std::forward<T>(t)).*pmf)(std::forward<Args>(args)...)); template<class T> static auto call(MT B::*pmd, T&& t) -> decltype(invoke_impl::get(std::forward<T>(t)).*pmd); }; template<class F, class... Args, class Fd = typename std::decay<F>::type> auto INVOKE(F&& f, Args&&... args) -> decltype(invoke_impl<Fd>::call(std::forward<F>(f), std::forward<Args>(args)...)); } // namespace detail // Minimal C++11 implementation: template<class> struct result_of; template<class F, class... ArgTypes> struct result_of<F(ArgTypes...)> { using type = decltype(detail::INVOKE(std::declval<F>(), std::declval<ArgTypes>()...)); }; // Conforming C++14 implementation (is also a valid C++11 implementation): namespace detail { template<typename AlwaysVoid, typename, typename...> struct invoke_result {}; template<typename F, typename...Args> struct invoke_result< decltype(void(detail::INVOKE(std::declval<F>(), std::declval<Args>()...))), F, Args...> { using type = decltype(detail::INVOKE(std::declval<F>(), std::declval<Args>()...)); }; } // namespace detail template<class> struct result_of; template<class F, class... ArgTypes> struct result_of<F(ArgTypes...)> : detail::invoke_result<void, F, ArgTypes...> {}; template<class F, class... ArgTypes> struct invoke_result : detail::invoke_result<void, F, ArgTypes...> {};
[編輯] 注意
在 C++11 中,當 INVOKE(std::declval<F>(), std::declval<ArgTypes>()...)
格式錯誤時(例如,當 F 根本不是可呼叫型別時),std::result_of
的行為是未定義的。C++14 將其更改為 SFINAE(當 F 不可呼叫時,std::result_of<F(ArgTypes...)>
根本沒有 type
成員)。
std::result_of
的動機是確定呼叫可呼叫物件的結果,特別是當結果型別對於不同的引數集不同時。
F(Args...) 是一個函式型別,其中 Args...
是引數型別,F
是返回型別。因此,std::result_of
存在一些怪癖,導致它在 C++17 中被棄用,轉而使用 std::invoke_result
:
-
F
不能是函式型別或陣列型別(但可以是它們的引用); - 如果任何
Args
的型別是“T
的陣列”或函式型別T
,則會自動調整為T*
; F
和任何Args...
都不能是抽象類型別;- 如果任何
Args...
具有頂層 cv 限定符,則會將其丟棄; - 任何
Args...
都不能是 void 型別。
為了避免這些怪癖,result_of
經常將引用型別用作 F
和 Args...
。例如
template<class F, class... Args> std::result_of_t<F&&(Args&&...)> // instead of std::result_of_t<F(Args...)>, which is wrong my_invoke(F&& f, Args&&... args) { /* implementation */ }
[編輯] 注意
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_lib_result_of_sfinae |
201210L |
(C++14) | std::result_of 和 SFINAE |
__cpp_lib_is_invocable |
201703L |
(C++17) | std::is_invocable, std::invoke_result |
[編輯] 示例
執行此程式碼
#include <iostream> #include <type_traits> struct S { double operator()(char, int&); float operator()(int) { return 1.0; } }; template<class T> typename std::result_of<T(int)>::type f(T& t) { std::cout << "overload of f for callable T\n"; return t(0); } template<class T, class U> int f(U u) { std::cout << "overload of f for non-callable T\n"; return u; } int main() { // the result of invoking S with char and int& arguments is double std::result_of<S(char, int&)>::type d = 3.14; // d has type double static_assert(std::is_same<decltype(d), double>::value, ""); // std::invoke_result uses different syntax (no parentheses) std::invoke_result<S,char,int&>::type b = 3.14; static_assert(std::is_same<decltype(b), double>::value, ""); // the result of invoking S with int argument is float std::result_of<S(int)>::type x = 3.14; // x has type float static_assert(std::is_same<decltype(x), float>::value, ""); // result_of can be used with a pointer to member function as follows struct C { double Func(char, int&); }; std::result_of<decltype(&C::Func)(C, char, int&)>::type g = 3.14; static_assert(std::is_same<decltype(g), double>::value, ""); f<C>(1); // may fail to compile in C++11; calls the non-callable overload in C++14 }
輸出
overload of f for non-callable T
[編輯] 參閱
(C++17)(C++23) |
用給定引數呼叫任何可呼叫物件並可指定返回型別(C++23 起) (函式模板) |
檢查一個型別是否可以使用給定引數型別進行呼叫(如同透過 std::invoke) (類模板) | |
(C++11) |
獲取模板型別引數物件的引用,用於未求值上下文 (函式模板) |