std:: result_of, std:: invoke_result
|
定义于头文件
<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
表达式
的返回类型。
|
|
(since C++11)
(until C++14) |
|
|
(since C++14) |
如果程序对本页面描述的任何模板添加特化,则行为未定义。
目录 |
成员类型
| 成员类型 | 定义 |
type
|
当使用参数
ArgTypes...
调用
Callable
类型
F
时的返回类型。
仅当在未求值上下文中可以用参数
ArgTypes...
调用 F 时定义。
(since 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)...)); } // 命名空间 detail // 最小 C++11 实现:</span
注释
正如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&&...)> // 替代错误的 std::result_of_t<F(Args...)> my_invoke(F&& f, Args&&... args) { /* 实现部分 */ }
注释
| 功能测试 宏 | 值 | 标准 | 功能特性 |
|---|---|---|---|
__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() { // 使用 char 和 int& 参数调用 S 的结果类型是 double std::result_of<S(char, int&)>::type d = 3.14; // d 的类型为 double static_assert(std::is_same<decltype(d), double>::value, ""); // std::invoke_result 使用不同的语法(无括号) std::invoke_result<S,char,int&>::type b = 3.14; static_assert(std::is_same<decltype(b), double>::value, ""); // 使用 int 参数调用 S 的结果类型是 float std::result_of<S(int)>::type x = 3.14; // x 的类型为 float static_assert(std::is_same<decltype(x), float>::value, ""); // result_of 可按如下方式用于成员函数指针 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); // 在 C++11 中可能编译失败;在 C++14 中调用不可调用重载 }
输出:
overload of f for non-callable T
参见
|
(C++17)
(C++23)
|
以给定参数调用任意
Callable
对象
并可指定返回类型
(C++23 起)
(函数模板) |
|
检查类型是否能以给定参数类型调用(如同通过
std::invoke
)
(类模板) |
|
|
(C++11)
|
在未求值上下文中获取模板类型参数的引用
(函数模板) |