473,385 Members | 1,347 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

Sep 4 '06 #1
5 1977
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<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
Sep 5 '06 #2
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.
Sep 5 '06 #3

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.

Sep 5 '06 #4
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.
Sep 5 '06 #5

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

Sep 5 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Teis Draiby | last post by:
I want to write a base class that includes a member function that creates an instance of a derrived class and returns a pointer to it. Problem: The derived class definition has to follow the base...
12
by: Mark | last post by:
I always use the word "class" to describe a class that can be instantiated. However, I don't get the concept of "Type" or at least I don't know how to use the word properly. If I have a class...
59
by: Michael C | last post by:
eg void DoIt() { int i = FromString("1"); double d = FromString("1.1"); } int FromString(string SomeValue) {
2
by: Tony Cox | last post by:
Hi. I'm new to VB.NET, so please be gentle ;-) I'm trying to transfer VB.NET structures to a shared memory area at a fixed location in process memory space. The structures themselves consist of...
9
by: Bit Byte | last post by:
Can't seem to get my head around the point of a trait class - no matter how many times I read up on it - why not simply use functors or function pointers ? Anyone care to explain this in a...
5
by: Hong Ye | last post by:
Traits is a useful template technique to simplfy the implementation of some classes. however, I met some questions when I tried to understand and implement it. Following is an example of traits...
2
by: hg | last post by:
Hi, I have the following code: ****************************************** from ctypes import * g_lib = cdll.LoadLibrary("libc.so.6") class Struct_Password(Structure): """ """
6
by: John | last post by:
Hi I have a function that needs to return value from some column in a table. The column type is not known in advance. Is it possible for function to return a value whose type would be determined...
1
by: s0suk3 | last post by:
On May 6, 12:14 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar> wrote: But at least put an underscore between the words (raw_data), or, if you like the OO world, make the subsequent words...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.