Until the auto keyword makes it really trivial, one can rely on the convention that function objects provide certain typedef's, including result_type. One just needs to wrap a plain function into a function object. The standard "functional" header provides some (pointer_to_unary_function/pointer_to_binary_function in addition to a couple of member function wrappers). When these are not enough, boost library provides more powerful wrappers.
#include <iostream>
#include <boost/function.hpp>
#include <boost/bind.hpp>
int answer()
{
return 42;
}
template <class T>
T unknown()
{
return T();
}
template <class Function>
void foobar(Function unknown)
{
typename Function::result_type x = unknown();
std::cout << x << '\n';
}
int main()
{
foobar(boost::function<int()>(answer));
foobar(boost::bind(unknown<int>));
}
And below's an example, how you might add a pointer_to_zeronary_function. (I suppose the helper function that helps you create one, ptr_fun, might be added to the standard namespace as well as an overload(?)
template <class T>
class pointer_to_zeronary_function
{
typedef T(*zeronary_func)();
zeronary_func func;
public:
typedef T result_type;
pointer_to_zeronary_function(zeronary_func f): func(f) {}
T operator()() const
{
return func();
}
};
template <class T>
pointer_to_zeronary_function<T> ptr_fun(T(*f)())
{
return pointer_to_zeronary_function<T>(f);
}
...
//usage:
foobar(ptr_fun(answer));