Diego Martins wrote:
>
I want to build a template class to extract the return type of a
pointer to function
Something like:
template<typename CB>
class ReturnTypeExtractor {
typedef ReturnType .... ??? :-(
};
...
// user code
typedef FooObject * (*CallBackType)();
typedef ReturnTypeExtractor<CallBackTypemyReturnType; //
myReturnType is FooObject *
How can I accomplish this?
#include <functional>
std::tr1::result_of<CallBackType>::type;
That is, if you have TR1. For details, see section 6.4 of my book, "The
Standard C++ Library Extensions: a Tutorial and Reference." You can get
a complete implementation of TR1 from Dinkumware,
www.dinkumware.com.
result_of might also be available from boost. It was developed from a
couple of similar things there, and they've made some adaptations to
support TR1.
The problem in rolling your own is that you can't write a template that
takes an arbitrary number of arguments, so you can't easily extract the
return type unless you know how may arguments the function takes:
template <class Ty>
struct result0;
template <class Ret>
struct result0<Ret(*)()>
{
typedef Ret type;
};
template <class Ty>
struct result1;
template <class Ret, class Arg0>
struct result1<Ret(*)(Arg0)>
{
typedef Ret type;
};
and so on.