c++ 函数指针模板

C++  /  houtizong 发布于 2年前   193
直接按如下定义是不行的,会报错!!!
typedef  template<class T> bool (*templateFunc)(const T& t1,const T& t2); //编译不通过!!!


但是不意味着不可以以模板函数作为函数的参数!!!回顾函数指针时http://cherishlc.iteye.com/blog/1274062
发现了返回函数指针的函数可以如下声明:
int (*fff(int a))(int ,int){//从函数名开始向外读      return sub;  }  


经过测试,以下函数中的可以传递函数指针的模板了~~~~~

#include <iostream>using namespace std;template<class T> inline bool isEqual(const T& t1,const T& t2){return t1==t2;}//typedef  template<class T> bool (*templateFunc)(const T& t1,const T& t2); //编译不通过!!!//第三个参数为以T为参数的函数,不可按上述方法定义!!!但直接写就没问题template<class T> bool isEqual2(T t1,T t2,bool (*isCompatible)(const T&,const T&)=isEqual<T>){return isCompatible(t1,t2);}void main(){bool (*isIntEqual)(int,int);//isIntEqual=isEqual2<int>; //不行,真悲催bool not=isEqual2<int>(1,2);bool yes=isEqual2<double>(1.0,1.0);cout<<"1==2? "<<not<<"\n1.0==1.0? "<<yes<<endl;}




另外转载两个其他的方法:
1、
http://topic.csdn.net/u/20120418/00/64c2d59b-151b-450e-a7aa-39ff6bf941a5.html?seed=380167928&r=78289909
template <typename PktType>struct Wrapper{typedef int (* CallbackFunPtrType) (const PktType& CurPkt);};这样不就行了用的时候就Wrapper<int>::CallbackFunPtrType callback=....


2、C++中用模板函数指针实现委托http://tangfeng.iteye.com/blog/602680

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客