pointer_protector(傻())); 所以实际上pointer_protector将是一个重载的函数模板, 知道检查是否或不是它得到了一个 std :: unary_function<>或者std :: binary_function<>,然后它会发出相应的一元指针保护器或二进制指针 protector。对于本讨论的其余部分,我将继续讨论关于一元指针保护器的并留下二进制指针 protector作为练习。这是我的一元指针保护模板: 模板< typename Arg,typename Res> class unary:public std :: unary_function< Arg *,Res> { public: 一元(const std :: unary_function< Arg,Res>& f):f( f) {} Res operator()(Arg * a)// 27 {// 28 返回f(* a); // 29 } // 30 // 31 私人: std :: unary_function< Arg ,Res> f; }; 这不起作用 - 当我创建一个函数对象并封装在 a指针保护器中这样: struct print_it:public std :: unary_function< int,int> { ... } int main() { for_each(foo。 begin(),foo.end(), 一元< print_it :: argument_type, print_it :: result_type>(print_it())); } i得到如下错误: unary.cpp:29错误:无法匹配调用`(unary_function< int,int>) (int&)'' 在FreeBSD 5.4-STABLE上使用g ++ via gfilt。那么什么是正确的方式来获得写一个引用封装翻译器的指针?它是否已经存在?b / b 解决方案 jsnX写道:使用在FreeBSD 5.4-STABLE上通过gfilt g ++。那么为参考封装翻译器编写指针的正确方法是什么?它已经存在吗? 是的,例如你可以使用boost :: lambda(检查 www.boost.org)。它 实现了lambda演算,能够自动创建这些仿函数 。检查他们的例子。 另一种选择是使用 部分特化为你的仿函数创建两个模板:一个在纯对象上工作,另一个在 指向对象的指针。 您可以使用boost :: type_traits来检查它是指针类型还是 不然后让编译器实例化正确的模板 自动。 - Matthias Kaeppler 说我不能使用boost,那么什么? 我真的很想知道为什么我的代码不起作用。 say i have a function object silly that takes a const ref to clowns class silly : public std::unary_function<const clown&, bool>{...} and then i decide to feed it a bunch of pointers to clowns: for_each(clown_pointers.begin(), clown_pointers.end(), silly()); of course this doesn''t work. i would like some magic template thatmakes it work like this: for_each(clown_pointers.begin(), clown_pointers.end(),pointer_protector(silly())); so actually pointer_protector would be an overloaded function template,which knows to check for whether or not it''s getting astd::unary_function<> or a std::binary_function<>, and then it wouldspit out a corresponding unary pointer protector or binary pointerprotector. for the rest of this discussion, i will stick t talkingabout the unary pointer protector and leave the binary pointerprotector as an exercise. here is my unary pointer protector template: template<typename Arg, typename Res>class unary : public std::unary_function<Arg*, Res>{public: unary(const std::unary_function<Arg, Res>& f) : f(f){} Resoperator() (Arg* a) //27{ //28return f(*a); //29} //30//31private:std::unary_function<Arg, Res> f;}; this does not work - when i create a function object and encapsulate ina pointer protector like this: struct print_it : public std::unary_function<int, int>{...} intmain(){for_each( foo.begin(), foo.end(),unary<print_it::argument_type,print_it::result_type>(print_it()) );} i get an error like this: unary.cpp:29 error: no match for call to `(unary_function<int, int>)(int &)'' using g++ via gfilt on FreeBSD 5.4-STABLE. so what is the right way towrite a pointer to reference encapsulating translator? does it alreadyexist? 解决方案 jsnX wrote: using g++ via gfilt on FreeBSD 5.4-STABLE. so what is the right way to write a pointer to reference encapsulating translator? does it already exist? Yes, for example you can use boost::lambda (check www.boost.org). Itimplements the lambda calculus and is capable of creating these functorsfor you automatically. Check their examples. Another option would be to create two templates for your functor usingpartial specialization: One working on pure objects and one working onpointers to objects.You can use boost::type_traits to check whether it''s a pointer type ornot and then let the compiler instantiate the correct templateautomagically. --Matthias Kaepplersay i can''t use boost, then what? I would really like to know why my code does not work. 这篇关于pointer_protector模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 02:26