Blace Ice wrote:
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::ostream & 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";
}