std::result_of, std::invoke_result
来自 cppreference.cn
定义于头文件 <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) |
获取模板类型参数对象的引用,用于未求值上下文 (函数模板) |