如何获取函数指针的参数类型 [英] How do I get the argument types of a function pointer
本文介绍了如何获取函数指针的参数类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想得到一个函数指针的参数类型如下:(第二行)
I want to get the argument types of a function pointer like this : (second line)
typedef void (*fun)(int);
GetParam1Type<fun>::Result <----Result为int
不一定要使用模板,并且必须支持类成员函数指针。
is not necessarily to use template,and must support class member function pointer.
推荐答案
正如Carlo所指出的那样,无法在运行时检索此信息。但是,您可以通过使用模板在编译时查询和使用类型信息:最简单的方法是使用C ++ 11扩展decltype
或std :: result_of
,或者如果你使用GCC,它有一个名为typeof
的扩展名。 (我对它们都不熟悉,并且没有安装编译器来编写和测试代码以用于您的目的。)
即使没有这些,它应该是可能的,但是在我开始用脑袋打造标准模板解决方案之前,编译时间分辨率是否足以达到您的目的?
PS:这是一个关于函数参数类型的堆栈溢出线程可能会有所帮助:函数参数类型 [ ^ ]
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 extensionsdecltype
orstd::result_of
, or if you use GCC, it has an extension calledtypeof
. (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[^]
简单说明, 你不能。C ++
编程语言不提供反射功能。但是,您可以实现自己的机制(例如,MS
使用IDispatch
执行此操作。)
Simply stated, you cannot.C++
programming language provides no reflection facilities. However you might implement your own mechanism (for instance,MS
did it withIDispatch
).
#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;
}
这篇关于如何获取函数指针的参数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文