Click here to Skip to main content
15,888,246 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I want to get the argument types of a function pointer like this : (second line)
C#
typedef void (*fun)(int);
GetParam1Type<fun>::Result  <----Result为int

is not necessarily to use template,and must support class member function pointer.
Posted
Comments
Philippe Mori 25-Nov-13 19:49pm    
If you want to do Advanced Template stuff, then you should read books on that topic like C++ Template Metaprogramming (or maybe something more recent) as the language as evolve a lot since then (C++11)

As Carlo pointed out, there is no way to retrieve this information at runtime. However, you can inquire and use type information at compile time through the use of templates: the easiest way would be with the C++ 11 extensions decltype or std::result_of, or if you use GCC, it has an extension called typeof. (I'm not familiar with either, and have no compiler installed to write and test code for your purpose though.)

Even without these, it should be possible, but before I start wringing a knot into my brain to come up with a standard template solution, is compile time resolution even sufficient for your purposes?

P.S.: here is a thread on stack overflow about function argument types which may be of help: Function argument type[^]
 
Share this answer
 
v2
Comments
CPallini 25-Nov-13 7:52am    
5.
Simply stated, you cannot. C++ programming language provides no reflection facilities. However you might implement your own mechanism (for instance, MS did it with IDispatch).
 
Share this answer
 
Comments
Philippe Mori 25-Nov-13 19:45pm    
Using templates, you can get a lot of informations from types...
C++
#include <iostream>
#include <type_traits>
 
struct A{};
  
template< typename T/*, typename U */>
struct GetParam1Type;
  
template< typename T, typename U, typename V >
struct GetParam1Type< T( U, V ) >
{
    typedef U Result;
};
 
template< typename T, typename U, typename V , typename R>
struct GetParam1Type< T( U, V ,R) >
{
    typedef U Result;
};
  
template< typename T >
struct GetParam1Type< T( void ) >
{
    typedef void Result;
};
  
template< typename T, typename U, typename V >
struct GetParam1Type< T( * )( U, V) >
{
    typedef U Result;
};
 
template< typename T, typename U, typename V , typename R>
struct GetParam1Type< T( * )( U, V ,R) >
{
    typedef U Result;
};
  
template< typename T >
struct GetParam1Type< T( * )( void ) >
{
    typedef void Result;
};
  
template< typename T, typename U, typename V, typename R >
struct GetParam1Type< T ( V::* )( U, R ) >
{
    typedef U Result;
};
 
template< typename T, typename U, typename V, typename R , typename A>
struct GetParam1Type< T ( V::* )( U, R ,A) >
{
    typedef U Result;
};
  
template< typename T, typename V >
struct GetParam1Type< T ( V::* )( void ) >
{
    typedef void Result;
};
  
typedef void ( A::*fun1 )( int, long, double );
typedef void ( A::*fun2 )(  );
typedef long fun3( int, long, double );
typedef long fun4(  );
typedef long ( *fun5 )( int, long, double );
typedef long ( *fun6 )(  );
 
int main( void )
{
    std::cout << std::is_same< GetParam1Type< fun1 >::Result, int >::value << std::endl;
    std::cout << std::is_same< GetParam1Type< fun2 >::Result, void >::value << std::endl;
    std::cout << std::is_same< GetParam1Type< fun3 >::Result, int >::value << std::endl;
    std::cout << std::is_same< GetParam1Type< fun4 >::Result, void >::value << std::endl;
    std::cout << std::is_same< GetParam1Type< fun5 >::Result, int >::value << std::endl;
    std::cout << std::is_same< GetParam1Type< fun6 >::Result, void >::value << std::endl;
    getchar();
    return 0;
}
 
Share this answer
 
Comments
Stefan_Lang 27-Nov-13 3:53am    
This looks like it's going the right way, but some of this won't compile: your definitions of fun1 and fun2 are wrong since member function pointers require you to reference an instance of the class or struct that contains the function. I am not familiar with the exact syntax of member function pointers, but I added a link to my solution where you can see decltype being used for that purpose (and the boost library in case you can't use decltype).

P.S.: seems I was wrong - looks like with the right compiler it does compile after all...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900