[ result_of ]

Callable Object and Callable Type

A callable type is:
  • pointer to function
  • pointer to a member function
  • pointer to a member data [ see mem_fn ]
  • object that can stand on the left of the function call operator
    • object implementing operator() 
    • object implementing conversion operator returning a pointer to function

A callable object is an object of a callable type.

result_of class template:

The result_of meta-function (class template) provides a uniform way to get the type of the return value of a callable type.

The metafunction takes the type of the callable object followed by the list of its argument types. Example:

#include <tr1/functional>

template <class Ct, class Arg>
void result_of_example(Ct fun, Arg arg)
{
    // the object can be called fun(arg)

    typedef typename std::tr1::result_of< Ct(Arg)>::type ret;
    ...
}

struct Callable
{
    char get() const 
    {...}

    std::string n;

    int operator()(int i) const
    {...}

    typedef int result_type;
};

void function(int n) 
{...}

int main()
{
    Callable object;
        
    result_of_example(function,1);
    result_of_example(&Callable::get, object);
    result_of_example(&Callable::n, object);
    result_of_example(object, 0);
}

note: for the result_of to work properly on user-defined callable objects, the member typedef result_type must be defined as the return type of the function call operator().

Comments