首页 > 编程知识 正文

函数模板中的类型参数个数不能是,typedef函数指针用法

时间:2023-05-06 21:32:12 阅读:24617 作者:4029

/*以下示例说明了在func中传递不同的参数,但在执行中的代码之前确定所传递参数的大小。 *如果将函数模板放入两个函数中进行重载,部分代码会变得冗长,所以这里考虑使用模板提出通用操作。 */# include iostream # include vector #//函数模板templatetypenametintcompare (const t1,const T t2 ) { return t1t2? 1: 0; //两个同名的函数。 第一个参数是函数指针voidfunc(int(f ) ) const int a,const int b ),inta,intb ) ) if (f ) a,b ) ) cout'int 1'endl }voidfunc(int(*f ) ) const double a,const double b ),doublea,doubleb ) ) if((*f ) (a,b ) ) cout ' double1' ebleb }int KeepAlivems=90; int getKeepAlivems () { cout'get='KeepAlivemsendl; 返回关键点MS; }voidsetkeepalivems(constintv ) { KeepAlivems=v; cout'set='KeepAlivemsendl; 返回; } templatetypenametintcompare1(t (get )、void (set ) const T value () ) ) get ) ); (set ) ) 20; (获取) ); t(*pf ) ); (set ) ) 52; 类型预设(* p ); //编译阶段有效的void*ptr=(void* ) get; pf=(p ) ptr; (pf ) ); 返回0; (}int main ) ) { int a=10,b=20; 双精度d=20.0,d=20.0; func (比较输入,a,b ); 比较双精度(func,c,d ); 比较1 int (getkeepalivems,setKeepAlivems; (/) int 2双精度1 ) /

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。