c – 函数签名作为模板参数
发布时间:2020-12-16 07:51:09 所属栏目:百科 来源:网络整理
导读:有可能实现这样的事情: templatetypename Signatureclass Test{ public: //here I want operator () to respect the signature};Testvoid(int) t1; //void operator()(int)Testvoid(int,float) t2; //void operator()(int,float) 返回类型总是无效的. 我想
有可能实现这样的事情:
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个参数.这通常使用相当复杂的宏来实现,以简化编码,但可以通过强力来完成. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |