One type traits class (or something like that) to get the return type of a pointer to function

D

Diego Martins

Hi all.

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<CallBackType> myReturnType; //
myReturnType is FooObject *

How can I accomplish this?

Thanks In Advance

Diego
HP
 
K

Kai-Uwe Bux

Diego said:
Hi all.

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<CallBackType> myReturnType; //
myReturnType is FooObject *

How can I accomplish this?

Here is some code I use:

template < typename F >
struct nullary_function_traits {

typedef typename F::result_type result_type;

};

template < typename R >
struct nullary_function_traits< R (*)( void ) > {

typedef R result_type;

};


Best

Kai-Uwe Bux
 
P

Pete Becker

Diego said:
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<CallBackType> myReturnType; //
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.
 
N

Noah Roberts

Pete said:
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.

It certainly is. That's where it came from. Most of TR1 was first
created in boost...you should know that.
 
P

Pete Becker

Noah said:
Pete Becker wrote:




It certainly is. That's where it came from. Most of TR1 was first
created in boost...you should know that.

I certainly do know that much of TR1 came from Boost. That doesn't mean
that Boost provides everything that's in TR1.

As I said, result_of came from a couple of things in Boost. My
recollection is that those things were somewhat different from what
eventually turned into result_of. I haven't looked to see whether the
final version of std::tr1::result_of is available from Boost. I'm not
going to say that it is, just because it's based on things from there.
 
D

Diego Martins

Kai-Uwe Bux said:
Here is some code I use:

template < typename F >
struct nullary_function_traits {

typedef typename F::result_type result_type;

};

template < typename R >
struct nullary_function_traits< R (*)( void ) > {

typedef R result_type;

};


Best

Kai-Uwe Bux

thank you! your solution is great for dealing with callbacks without
parameters

thanks all for the tr1 and boost hints, but I don't want to install any
addicional libraries now (due project policies and my personal taste
for making stuff manually . it is a good way to learn things)

by the way, there is a good link I've found somewhere in another thread
http://erdani.org/publications/traits.html

Cheers
Diego
HP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top