C++利用不完全实例化来获得函数模板参数的返回值和参数
2013-07-16 14:31
417 查看
C++利用不完全实例化来获得函数模板参数的返回值和参数
分类: 开发模式探索 linux 嵌入式 boost2013-07-1318:19 107人阅读 评论(0) 收藏 举报
有一些模板会以函数为模板参数,有时候这些模板要获得函数的返回值和参数。如在boost中的signal和slot机制,就存在这样情况。
那么,我们如何得到这些信息呢?
我们使用C++不完全实例化来实现。
比如,有这个代码
[cpp] view
plaincopy
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模板来实现。
[cpp] view
plaincopy
template<typename Function>
struct function_traits : public function_traits_help<Function*>
{
};
function_traits_help类接受Function的指针为参数。
函数类型和函数指针是不一样的,如
[cpp] view
plaincopy
void (int,int)//定义一个函数类型
void (*)(int, int) //定义了一个函数指针
template<typename Func>
struct FuncType {
typedef FuncPtr funcPtr;
};
FuncType<void(int,int)>::funcPtr 等同于 void(*)(int,int)
function_traits_help就是利用不完全实例化来实现
首先,定义主模板
[cpp] view
plaincopy
template<typename FunctionPtr> struct function_traits_help;
主模板不做任何事情,作为一个核心模板存在,但从不使用,因此无需具体定义。
定义无参数的实现
[cpp] view
plaincopy
template<typename R>
struct function_traits_help<R(*)(void)>
{
enum {arty = 0 };
typedef R result_type;
};
function_traits_help<R(*)(void)>就是function_traits_help<FunctionPtr>的一种具体实例化,C++编译器当遇到 R (*)(void)这样类型的函数指针类型的时候,就会匹配到这个定义上。
定义包含一个参数的模板
[cpp] view
plaincopy
template<typename R, typename T1>
struct function_traits_help<R(*)(T1)>
{
enum {arty = 1 };
typedef R result_type;
typedef T1 arg1_type;
};
同样的,C++编译器遇到 R (*)(T1)类型的函数指针时,就会匹配到这个定义。
同理,我们可以定义2,3,4,...
[cpp] view
plaincopy
template<typename R, typename T1, typename T2>
struct function_traits_help<R(*)(T1, T2)>
{
enum {arty = 2 };
typedef R result_type;
typedef T1 arg1_type;
typedef T2 arg2_type;
};
template<typename R, typename T1, typename T2, typename T3>
struct function_traits_help<R(*)(T1, T2, T3)>
{
enum {arty = 3 };
typedef R result_type;
typedef T1 arg1_type;
typedef T2 arg2_type;
typedef T3 arg3_type;
};
template<typename R, typename T1, typename T2, typename T3, typename T4>
struct function_traits_help<R(*)(T1, T2, T3, T4)>
{
enum {arty = 4 };
typedef R result_type;
typedef T1 arg1_type;
typedef T2 arg2_type;
typedef T3 arg3_type;
typedef T4 arg4_type;
};
..............
这样,我们就可以得到正确的信息:
[html] view
plaincopy
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>
[cpp] view
plaincopy
template<typename T>
void show_function_info(const char* name)
{
printf("%s: arg count=%d, result_type:%s\n", name, T::arty, typeid(typename T::result_type).name());
show_args<T::arty, T> x;
x();
}
show_args<T::arty, T>也是一个不完全实例化的对象,用于匹配正确数目参数的对象。
show_args的定义如下:
[cpp] view
plaincopy
template<int N, typename Func>
struct show_args; //主模板,从不使用
template<typename Func>
struct show_args<0, Func> { //0个参数
void operator()(){ }
};
template<typename Func>
struct show_args<1, Func> {//1个参数
void operator()(){
printf("\targ1 = %s\n", typeid(typename Func::arg1_type).name());
}
};
template<typename Func>
struct show_args<2, Func> {//2个参数
void operator()(){
printf("\targ1 = %s\n", typeid(typename Func::arg1_type).name());
printf("\targ2 = %s\n", typeid(typename Func::arg2_type).name());
}
};
template<typename Func>
struct show_args<3, Func> {
void operator()(){
printf("\targ1 = %s\n", typeid(typename Func::arg1_type).name());
printf("\targ2 = %s\n", typeid(typename Func::arg2_type).name());
printf("\targ3 = %s\n", typeid(typename Func::arg3_type).name());
}
};
template<typename Func>
struct show_args<4, Func> {
void operator()(){
printf("\targ1 = %s\n", typeid(typename Func::arg1_type).name());
printf("\targ2 = %s\n", typeid(typename Func::arg2_type).name());
printf("\targ3 = %s\n", typeid(typename Func::arg3_type).name());
printf("\targ4 = %s\n", typeid(typename Func::arg4_type).name());
}
};
.................................
最后,用宏来简化实现
[cpp] view
plaincopy
#define SHOW_FUNC(T) \
show_function_info<T > (#T)
最后的测试代码
[cpp] view
plaincopy
int main()
{
SHOW_FUNC(function_traits<int ()>);
SHOW_FUNC(function_traits<void ()>);
SHOW_FUNC(function_traits<void (int)>);
SHOW_FUNC(function_traits<int (int,char*)>);
SHOW_FUNC(function_traits<int (int,char*, double)>);
};
该程序的运行结果是
[plain] view
plaincopy
function_traits<int ()>: arg count=0, result_type:i
function_traits<void ()>: arg count=0, result_type:v
function_traits<void (int)>: arg count=1, result_type:v
arg1 = i
function_traits<int (int,char*)>: arg count=2, result_type:i
arg1 = i
arg2 = Pc
function_traits<int (int,char*, double)>: arg count=3, result_type:i
arg1 = i
arg2 = Pc
arg3 = d
相关文章推荐
- C++利用不完全实例化来获得函数模板参数的返回值和参数
- C++利用不完全实例化来获得函数模板参数的返回值和参数
- C++可变参数的函数与模板实例分析
- c/c++使用指针做函数返回值和指针作函数参数问题
- C/C++利用位运算来进行函数参数检查
- C++ 类模板概念与实例,模板参数问题
- C++函数返回多个参数
- C++中函数的返回值作为函数的参数学习笔记
- C++中函数的参数是传值还是传址?return返回的变量是传值还是传址?
- 不可或缺 Windows Native (16) - C++: 函数重载, 缺省参数, 内联函数, 函数模板
- C++ 多维数组作为函数的返回值和参数
- C++学习之可变参数的函数与模板
- 对于qt5的信号机制的一点参考,然后利用模板实现:在新线程执行可变函数参数的调用
- C++获取Lua全局变量和执行Lua多参数多返回值函数
- VB6 用数组作函数参数并返回一个数组的实例
- 实例讲解在C++的函数中变量参数及默认参数的使用
- C++获取Lua全局变量和执行Lua多参数多返回值函数
- C++学习 【4.1】 利用函数实现指定的功能---函数,函数的形式,函数参数
- 利用C++模板,代替虚函数实现类的静态多态性及动态继承
- [C++] 利用模板的模板参数实现单链表