Namespaces
Variants
Views
Actions

std::result_of

From cppreference.com
< cpp‎ | types
Revision as of 20:24, 15 April 2013 by Cubbi (Talk | contribs)

 
 
 
Type support
Basic types
Fundamental types
Fixed width integer types (C++11)
Numeric limits
C numeric limits interface
Runtime type information
Type traits
Primary type categories
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Type properties
(C++11)
(C++11)
(C++11)
(C++14)
Supported operations
Relationships and property queries
(C++11)
(C++11)
(C++11)
(C++11)
Type modifications
(C++11)(C++11)(C++11)
Type transformations
(C++11)
(C++11)
result_of
(C++11)
Type trait constants
 

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <type_traits>
</td>

<td></td> <td></td> <tr class="t-dcl ">

<td >
template< class >
class result_of; //not defined
</td>

<td > (1) </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">

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

<td > (2) </td> <td > (since C++11) </td> </tr> Template:ddcl list end

Deduces the return type of a function call expression at compile time.

Contents

Member types

Member type Definition
type the return type of the function F if called with the arguments ArgTypes...

Possible implementation

template<class>
struct result_of;
 
template<class F, class... ArgTypes>
struct result_of<F(ArgTypes...)>
{
    typedef decltype(
                     std::declval<F>()(std::declval<ArgTypes>()...)
                    ) type;
};

Examples

std::result_of can be used to determine the result of invoking a functor, in particular if the result type is different for different sets of arguments:

#include <type_traits>
 
struct S {
    double operator()(char, int&);
    float operator()(int);
};
 
struct C {
    double Func(char, int&);
};
 
int main()
{
    // the result of invoking S with char and int& arguments is double
    std::result_of<S(char, int&)>::type f = 3.14; // f has type double
    static_assert(std::is_same<decltype(f), double>::value, "");
 
    // the result of invoking S with int argument is float
    std::result_of<S(int)>::type d = 3.14; // f has type float
    static_assert(std::is_same<decltype(d), float>::value, "");
 
    // result_of can be used with a pointer to member function as follows
    std::result_of<decltype(&C::Func)(C, char, int&)>::type g = 3.14;
    static_assert(std::is_same<decltype(g), double>::value, "");
}


See also

(C++11)
obtains the type of expression in unevaluated context
(function template) [edit]