现在的位置: 首页 > 综合 > 正文

C++利用不完全实例化来获得函数模板参数的返回值和参数 C++利用不完全实例化来获得函数模板参数的返回值和参数

2017年12月12日 ⁄ 综合 ⁄ 共 4928字 ⁄ 字号 评论关闭
 

C++利用不完全实例化来获得函数模板参数的返回值和参数

分类: 开发模式探索 linux 嵌入式 boost 107人阅读 评论(0) 收藏 举报

有一些模板会以函数为模板参数,有时候这些模板要获得函数的返回值和参数。如在boost中的signal和slot机制,就存在这样情况。

那么,我们如何得到这些信息呢?

我们使用C++不完全实例化来实现。

比如,有这个代码

  1. typedef function_traits<void (int,const char*)> Signal;  

能够得到Signal::result_type == void, Signal::arg1_type == int, Signal::arg2_type == const char*?

要想获得这种效果,必须通过Function的指针来实现,我们借助一个function_traits_help模板来实现。

  1. template<typename Function>  
  2. struct function_traits : public function_traits_help<Function*>  
  3. {  
  4. };  

function_traits_help类接受Function的指针为参数。

函数类型和函数指针是不一样的,如

  1. void (int,int)//定义一个函数类型  
  2. void (*)(intint//定义了一个函数指针  
  3.   
  4. template<typename Func>  
  5. struct FuncType {  
  6.    typedef FuncPtr funcPtr;  
  7. };  
  8. FuncType<void(int,int)>::funcPtr 等同于 void(*)(int,int)  

function_traits_help就是利用不完全实例化来实现

首先,定义主模板

  1. template<typename FunctionPtr> struct function_traits_help;  

主模板不做任何事情,作为一个核心模板存在,但从不使用,因此无需具体定义。

定义无参数的实现

  1. template<typename R>  
  2. struct function_traits_help<R(*)(void)>  
  3. {  
  4.     enum {arty = 0 };  
  5.     typedef R result_type;  
  6. };  

function_traits_help<R(*)(void)>就是function_traits_help<FunctionPtr>的一种具体实例化,C++编译器当遇到 R (*)(void)这样类型的函数指针类型的时候,就会匹配到这个定义上。

定义包含一个参数的模板

  1. template<typename R, typename T1>  
  2. struct function_traits_help<R(*)(T1)>  
  3. {  
  4.     enum {arty = 1 };  
  5.     typedef R result_type;  
  6.     typedef T1 arg1_type;  
  7. };  

同样的,C++编译器遇到 R (*)(T1)类型的函数指针时,就会匹配到这个定义。

同理,我们可以定义2,3,4,...

  1. template<typename R, typename T1, typename T2>  
  2. struct function_traits_help<R(*)(T1, T2)>  
  3. {  
  4.     enum {arty = 2 };  
  5.     typedef R result_type;  
  6.     typedef T1 arg1_type;  
  7.     typedef T2 arg2_type;  
  8. };  
  9.   
  10. template<typename R, typename T1, typename T2, typename T3>  
  11. struct function_traits_help<R(*)(T1, T2, T3)>  
  12. {  
  13.     enum {arty = 3 };  
  14.     typedef R result_type;  
  15.     typedef T1 arg1_type;  
  16.     typedef T2 arg2_type;  
  17.     typedef T3 arg3_type;  
  18. };  
  19.   
  20. template<typename R, typename T1, typename T2, typename T3, typename T4>  
  21. struct function_traits_help<R(*)(T1, T2, T3, T4)>  
  22. {  
  23.     enum {arty = 4 };  
  24.     typedef R result_type;  
  25.     typedef T1 arg1_type;  
  26.     typedef T2 arg2_type;  
  27.     typedef T3 arg3_type;  
  28.     typedef T4 arg4_type;  
  29. };  
  30.   
  31. ..............  

这样,我们就可以得到正确的信息:

[html] view
plain
copy

  1. typedef function_traits<int (int,char*)> Function;  

于是:

Function::arty  : 2;

Function::result_type : int

Function::arg1_type : int

Function::arg2_type : char* 

我们还可以用typeid(x).name()来看真正的效果。

PS: 要使用typeid(x).name()要首先  #include <typeinfo>

  1. template<typename T>  
  2. void show_function_info(const char* name)  
  3. {  
  4.     printf("%s: arg count=%d, result_type:%s\n", name, T::arty, typeid(typename T::result_type).name());  
  5.     show_args<T::arty, T> x;  
  6.     x();  
  7. }  

show_args<T::arty, T>也是一个不完全实例化的对象,用于匹配正确数目参数的对象。

show_args的定义如下:

  1. template<int N, typename Func>  
  2. struct show_args; //主模板,从不使用  
  3.   
  4. template<typename Func>  
  5. struct show_args<0, Func> { //0个参数  
  6.     void operator()(){ }  
  7. };  
  8.   
  9. template<typename Func>  
  10. struct show_args<1, Func> {//1个参数  
  11.     void operator()(){   
  12.         printf("\targ1 = %s\n"typeid(typename Func::arg1_type).name());  
  13.     }  
  14. };  
  15.   
  16. template<typename Func>  
  17. struct show_args<2, Func> {//2个参数  
  18.     void operator()(){  
  19.         printf("\targ1 = %s\n"typeid(typename Func::arg1_type).name());  
  20.         printf("\targ2 = %s\n"typeid(typename Func::arg2_type).name());  
  21.     }  
  22. };  
  23.   
  24. template<typename Func>  
  25. struct show_args<3, Func> {  
  26.     void operator()(){  
  27.         printf("\targ1 = %s\n"typeid(typename Func::arg1_type).name());  
  28.         printf("\targ2 = %s\n"typeid(typename Func::arg2_type).name());  
  29.         printf("\targ3 = %s\n"typeid(typename Func::arg3_type).name());  
  30.     }  
  31. };  
  32.   
  33. template<typename Func>  
  34. struct show_args<4, Func> {  
  35.     void operator()(){  
  36.         printf("\targ1 = %s\n"typeid(typename Func::arg1_type).name());  
  37.         printf("\targ2 = %s\n"typeid(typename Func::arg2_type).name());  
  38.         printf("\targ3 = %s\n"typeid(typename Func::arg3_type).name());  
  39.         printf("\targ4 = %s\n"typeid(typename Func::arg4_type).name());  
  40.     }  
  41. };  
  42.   
  43. .................................  

最后,用宏来简化实现

  1. #define SHOW_FUNC(T) \  
  2.     show_function_info<T  > (#T)  

最后的测试代码

  1. int main()  
  2. {  
  3.     SHOW_FUNC(function_traits<int ()>);  
  4.     SHOW_FUNC(function_traits<void ()>);  
  5.     SHOW_FUNC(function_traits<void (int)>);  
  6.     SHOW_FUNC(function_traits<int (int,char*)>);  
  7.     SHOW_FUNC(function_traits<int (int,char*, double)>);  
  8.   
  9. };  

该程序的运行结果是

[plain] view
plain
copy

  1. function_traits<int ()>: arg count=0, result_type:i  
  2. function_traits<void ()>: arg count=0, result_type:v  
  3. function_traits<void (int)>: arg count=1, result_type:v  
  4.     arg1 = i  
  5. function_traits<int (int,char*)>: arg count=2, result_type:i  
  6.     arg1 = i  
  7.     arg2 = Pc  
  8. function_traits<int (int,char*, double)>: arg count=3, result_type:i  
  9.     arg1 = i  
  10.     arg2 = Pc  
  11.     arg3 = d  

抱歉!评论已关闭.