Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Diego Martins
Guest
 
Posts: n/a
#1: Sep 4 '06
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<CallBackTypemyReturnType; //
myReturnType is FooObject *

How can I accomplish this?

Thanks In Advance

Diego
HP


Kai-Uwe Bux
Guest
 
Posts: n/a
#2: Sep 5 '06

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


Diego Martins wrote:
Quote:
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<CallBackTypemyReturnType; //
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
Pete Becker
Guest
 
Posts: n/a
#3: Sep 5 '06

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


Diego Martins wrote:
Quote:
>
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.
Noah Roberts
Guest
 
Posts: n/a
#4: Sep 5 '06

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



Pete Becker wrote:
Quote:
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.

Pete Becker
Guest
 
Posts: n/a
#5: Sep 5 '06

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


Noah Roberts wrote:
Quote:
Pete Becker wrote:
>
>
Quote:
>>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.
>
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.

--
-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and Reference."
For more information about this book, see www.petebecker.com/tr1book.
Diego Martins
Guest
 
Posts: n/a
#6: Sep 5 '06

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



Kai-Uwe Bux wrote:
Quote:
Diego Martins wrote:
>
Quote:
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<CallBackTypemyReturnType; //
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
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

Closed Thread