ITeye博客

模板参数 函数指针

chenqi210 · 2011-12-22 13:12 · c++ · 编程语言 · Markdown

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)

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。