模板参数 函数指针

http://stackoverflow.com/questions/5507377/template-type-deduction-for-a-pointer-to-member-function 写道


For a.connect<double> (&GApp::foo), both the foo(double) and foo(double, double) will match the overload of connect with one, and with two template parameters respectively (in the case of the two parameter version, the second template argument will be deduced, the first argument was provided by you explicitly).

If you want to disambiguate ambiguous cases, I recommend that you pass the exact type, so no surprises can happen. Instead of those overloads, why not have a single overload

template<typename MType, typename T>
void connect(MType T::*f)
{
//Do some stuff
}

a.connect<void()> (&GApp::foo);
a.connect<void(double)> (&GApp::foo);
a.connect<void(double, double)> (&GApp::foo);

 

猜测MType为函数类型,而f是T内的一个函数指针。并写了这个函数来测试

 

template<typename FunctionType,typename T,typename Type>
MethodWrapper<FunctionType T::*,Type>
methodwrap(FunctionType T::* fp,Type *pObj)
{
	return MethodWrapper<FunctionType T::*,Type>(fp,pObj);
}

 

template<typename MType,typename T>
void memfn(MType (T::*f))
{
	cout << typeid(f).name() << endl;
}

 

写的这个memfn可以从&Test::display中同时推导出函数类型 MType和所属类型T

vs2010,

gcc 4.5.2 (没有启用 -std=c++0x)

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值