2014-11-07 星期五 22:52:07
接1,先说下typename和class的差异。
● 修饰template形参时,两者无差异
● 重定义(typedef)或要使用template内部的类型时,在此前面只能用typename来表明template::inherent_type,例如(typename ParentFunctor::ResultType)
1、代码流程
Functor< void, TYPELIST_2(int , double) > cmd(f);
● 先看构造函数
1
2
3
|
template
<
typename
Fun>
Functor(
const
Fun& fun)
:spImpl_(
new
FunctorHandler<Functor, Fun>(fun))
|
-
FunctorImpl()->FunctorHandler(const Fun& fun):fun_(fun)
把fun传给了FunctorHandler::fun_。
-
:spImpl_(
new
FunctorHandler<Functor, Fun>(fun))
spImpl_初始化了,Functor构造也完成了。
-
cmd(f)
根据< void, TYPELIST_2(int , double) >自动调用仿函数Functor::operator()->FunctorImpl::operator()->FunctorHandler::operator()->FunctorHandler::fun_
-
总结
只要Functor< void, TYPELIST_2(int , double) > cmd(f)中<>的类型具现了,编译器就会自动匹配最优的Functor::operator()仿函数。其他没有用到的仿函数就不会编译。举个浅显的例子。鞋店并不知道你的脚的大小等,但它提供了足够多的型号来供你选择。你选中了合适的鞋子后,其他的鞋子都与你无关了,无论它们是一步登天的仙鞋,还是步履维艰的妖鞋子,都与你无关了。