Connecting Tech Pros Worldwide Help | Site Map

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

 
LinkBack Thread Tools Search this Thread
  #1  
Old September 4th, 2006, 08:25 PM
Diego Martins
Guest
 
Posts: n/a
Default One type traits class (or something like that) to get the return type of a pointer to function

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


  #2  
Old September 5th, 2006, 06:05 AM
Kai-Uwe Bux
Guest
 
Posts: n/a
Default 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
  #3  
Old September 5th, 2006, 10:45 AM
Pete Becker
Guest
 
Posts: n/a
Default Re: One type traits class (or something like that) to get the returntype 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.
  #4  
Old September 5th, 2006, 03:35 PM
Noah Roberts
Guest
 
Posts: n/a
Default 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.

  #5  
Old September 5th, 2006, 04:15 PM
Pete Becker
Guest
 
Posts: n/a
Default Re: One type traits class (or something like that) to get the returntype 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.
  #6  
Old September 5th, 2006, 08:25 PM
Diego Martins
Guest
 
Posts: n/a
Default 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

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.