命名空间
变体
操作

std::result_of, std::invoke_result

来自 cppreference.cn
< cpp‎ | types
 
 
元编程库
类型特征
类型类别
(C++11)
(C++11)(DR*)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11) 
(C++11)
(C++11)
类型属性
(C++11)
(C++11)
(C++14)
(C++11)(C++26 弃用)
(C++11)(直至 C++20*)
(C++11)(C++20 弃用)
(C++11)
类型特征常量
元函数
(C++17)
支持的操作
关系和属性查询
类型修改
(C++11)(C++11)(C++11)
类型变换
(C++11)(C++23 弃用)
(C++11)(C++23 弃用)
(C++11)
result_ofinvoke_result
(C++11)(直至 C++20*)(C++17)

(C++11)
(C++17)
编译时有理算术
编译时整数序列
 
定义于头文件 <type_traits>
template< class >

class result_of; // not defined

template< class F, class... ArgTypes >

class result_of<F(ArgTypes...)>;
(1) (C++11 起)
(C++17 弃用)
(C++20 移除)
template< class F, class... ArgTypes >
class invoke_result;
(2) (C++17 起)

在编译时推导 INVOKE 表达式 的返回类型。

F 必须是可调用类型、函数引用或可调用类型引用。以 ArgTypes... 调用 F 必须是良构表达式。

(C++11 起)
(C++14 前)

FArgTypes 中的所有类型可以是任何完整类型、未知边界数组或(可能带 cv 限定的)void

(C++14 起)

如果程序为此页描述的任何模板添加特化,则行为未定义。

内容

[edit] 成员类型

成员类型 定义
type 若以实参 ArgTypes... 调用,则为 Callable 类型 F 的返回类型。仅当能在未求值语境中以实参 ArgTypes... 调用 F 时才定义。(C++14 起)

[edit] 辅助类型

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 起)

[edit] 可能的实现

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...> {};

[edit] 注意

如 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 背后的动机是确定调用 Callable 的结果,特别是当该结果类型对于不同的参数集不同时。

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 通常与引用类型一起使用作为 FArgs...。 例如

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 */
    }

[edit] 注意

特性测试 Std 特性
__cpp_lib_result_of_sfinae 201210L (C++14) std::result_ofSFINAE
__cpp_lib_is_invocable 201703L (C++17) std::is_invocable, std::invoke_result

[edit] 示例

#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

[edit] 参见

(C++17)(C++23)
使用给定实参调用任何 Callable 对象并能指定返回类型(C++23 起)
(函数模板) [编辑]
检查是否能以给定的实参类型调用类型(如同通过 std::invoke
(类模板) [编辑]
(C++11)
获取模板类型实参对象的引用,以用于未求值语境
(函数模板) [编辑]