首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::result_of

Defined in header <type_traits>

template< class > class result_of; // not defined template< class F, class... ArgTypes > class result_of<F(ArgTypes...)>;

(1)

(since C++11) (deprecated in C++17)

template< class F, class... ArgTypes> class invoke_result;

(2)

(since C++17)

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

F must be a callable type, reference to function, or reference to callable type. Invoking F with ArgTypes... must be a well-formed expression

(since C++11)

F and all types in ArgTypes can be any complete type, array of unknown bound, or (possibly cv-qualified) void

(since C++14)

成员类型

Member type

Definition

type

the return type of the Callable type F if invoked with the arguments ArgTypes.... Only defined if F can be called with the arguments ArgTypes... in unevaluated context. (since C++14)

帮助者类型

template< class T > using result_of_t = typename result_of<T>::type;

(1)

(since C++14) (deprecated in C++17)

template< class F, class... ArgTypes> using invoke_result_t = typename invoke_result<F, ArgTypes...>::type;

(2)

(since C++17)

可能的实施

二次

代码语言:javascript
复制
namespace detail {
template <class F, class... Args>
inline auto INVOKE(F&& f, Args&&... args) ->
    decltype(forward<F>(f)(forward<Args>(args)...)) {
      return forward<F>(f)(forward<Args>(args)...);
}
 
template <class Base, class T, class Derived>
inline auto INVOKE(T Base::*pmd, Derived&& ref) ->
    decltype(forward<Derived>(ref).*pmd) {
      return forward<Derived>(ref).*pmd;
}
 
template <class PMD, class Pointer>
inline auto INVOKE(PMD&& pmd, Pointer&& ptr) ->
    decltype((*forward<Pointer>(ptr)).*forward<PMD>(pmd)) {
      return (*forward<Pointer>(ptr)).*forward<PMD>(pmd);
}
 
template <class Base, class T, class Derived, class... Args>
inline auto INVOKE(T Base::*pmf, Derived&& ref, Args&&... args) ->
    decltype((forward<Derived>(ref).*pmf)(forward<Args>(args)...)) {
      return (forward<Derived>(ref).*pmf)(forward<Args>(args)...);
}
 
template <class PMF, class Pointer, class... Args>
inline auto INVOKE(PMF&& pmf, Pointer&& ptr, Args&&... args) ->
    decltype(((*forward<Pointer>(ptr)).*forward<PMF>(pmf))(forward<Args>(args)...)) {
      return ((*forward<Pointer>(ptr)).*forward<PMF>(pmf))(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中所述,std::result_of时未定义INVOKE(std::declval<F>(), std::declval<ArgTypes>()...)是畸形的%28等。当F根本不是一个可调用类型时,%29。C++14将其更改为SFINAE%28当F不可调用时,std::result_of<F(ArgTypes...)>简单地说,%27T具有type成员%29

背后的动机std::result_of是确定调用Callable,特别是对于不同的参数集,如果结果类型不同。

F(Args...)的函数类型Args...作为参数类型和F作为返回类型。因此,F不能是函数类型%28,但可以是对函数类型%29的引用。

实例

二次

代码语言:javascript
复制
#include <type_traits>
#include <iostream>
 
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, "");
 
    // 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
}

二次

产出:

二次

代码语言:javascript
复制
overload of f for non-callable T

二次

另见

invoke (C++17)

invokes any Callable object with given arguments (function template)

is_invocableis_invocable_ris_nothrow_invocableis_nothrow_invocable_r (C++17)

checks if a type can be invoked (as if by std::invoke) with the given argument types (class template)

declval (C++11)

obtains a reference to its argument for use in unevaluated context (function template)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券