Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > One type traits class (or something like that) to get the return type of a pointer to function

Reply
Thread Tools

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

 
 
Diego Martins
Guest
Posts: n/a
 
      09-04-2006
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

 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      09-05-2006
Diego Martins wrote:

> 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
 
Reply With Quote
 
 
 
 
Pete Becker
Guest
Posts: n/a
 
      09-05-2006
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<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.
 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      09-05-2006

Pete Becker wrote:

> 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.

 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      09-05-2006
Noah Roberts wrote:
> Pete Becker wrote:
>
>
>>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.
 
Reply With Quote
 
Diego Martins
Guest
Posts: n/a
 
      09-05-2006

Kai-Uwe Bux wrote:
> Diego Martins wrote:
>
> > 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


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Type traits with non-template variadic function Qi C++ 1 10-25-2012 08:39 AM
How to generate execute file that include enthought.traits.api ,enthought.traits.ui.api ? ray Python 1 06-04-2010 03:49 PM
User defined class meta data (type traits?) greek_bill C++ 3 11-02-2008 07:06 PM
object-like macro used like function-like macro Patrick Kowalzick C++ 5 03-14-2006 03:30 PM
umm... something... template(s)... something else... pointer(s)... and such... 0.o yah, I'm hopeless and clueless o.0 C++ 4 10-13-2004 10:34 PM



Advertisments