How to divide typenames from a function declaration?

B

Blace Ice

HI,
Pls. see the following function declaration:
int foo(const char *);

How to get the return typename and parameter typenames from the declaration?
For example, I need some codes like the following pseudocode:

typedef typename ReturnType(&foo)::Type AType;

Regards,
Leo Liu
 
G

Gianni Mariani

Blace said:
HI,
Pls. see the following function declaration:
int foo(const char *);

How to get the return typename and parameter typenames from the declaration?
For example, I need some codes like the following pseudocode:

typedef typename ReturnType(&foo)::Type AType;

Check out this example. I think it will do what you're asking.

This compiled with gcc 3.3.1 and did what I expected.


#include <iostream>

template <typename T>
class FuncParam
{
public:

enum { is_func = 0 };

typedef int t_ret;
typedef int t_val;
};


// only works for single parameter functions.

//
// specialize for a single parameter function.
// w_ret FUNC( w_val )
//
template <typename w_ret, typename w_val>
class FuncParam<w_ret ( w_val )>
{
public:
enum { is_func = 1 };

typedef w_ret t_ret;
typedef w_val t_val;
};


typedef char * TYPE( short & );

typedef char OTYPE;


std::eek:stream & PrintMe( std::string s )
{
return std::cout << s << "\n";
}

template <typename T>
typename FuncParam<T>::t_ret Somthing(
T * func,
typename FuncParam<T>::t_val v
) {
return func( v );
}


int main()
{

std::cout << FuncParam<TYPE>::is_func << "\n";

Somthing( PrintMe, "TRY THIS" ) << " AND THIS\n";
}
 

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,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top