有可能实现这样的事情:
- template<typename Signature>
- class Test
- {
- public:
- //here I want operator () to respect the signature
- };
- Test<void(int)> t1; //void operator()(int)
- Test<void(int,float)> t2; //void operator()(int,float)
返回类型总是无效的.
解决方法
- template <class Ty>
- class Test; /* not defined */
- template <class Ret,class Arg0>
- class Test<Ret(Arg0)> { /* whatever */ }
- template <class Ret,class Arg0,class Arg1>
- class Test<Ret(Arg0,Arg1)> { /* whatever */ }
- template <class Ret,class Arg1,class Arg2>
- class Test<Ret(Arg0,Arg1,Arg2)> { /* whatever */ }
继续繁琐的重复,直到你有足够的参数为您的需要.在TR1中,建议各种功能对象模板能够处理10个参数.这通常使用相当复杂的宏来实现,以简化编码,但可以通过强力来完成.