您的位置:程序门 -> c/c++ ->



函数对象适配器问题


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


函数对象适配器问题[已结贴,结贴人:alwaysslh]
发表于:2007-10-29 12:54:16 楼主
如下:
string   str   =   "poor";

template <   typename   funcobject   ,   typename   type   >
type   unaryfunc(   funcobject   fob   ,   const   type   &val   )
{
        return   fob(   val   );
}

用内置关系函数对象equal_to <type> 判断str是否为"pooh"时,如下:
equal_to <string>   se;
bool   s   =   se(   string("pooh")   ,   str   );
能通过编译,但用绑定器bind2nd时,报错,如下:
bool   sr   =   unaryfunc(   bind2nd(   equal_to <string> ()   ,   string(   "pooh"   )   )   ,   str   );
报错如下:
cannot   convert   from   'std::string'   to   'bool'
为什么unaryfunc返回string类型呢?到底哪儿错了?高手帮忙,谢谢
发表于:2007-10-29 13:09:131楼 得分:7
c/c++ code
#include<iostream> #include<string> #include<functional> using namespace std; template < typename funcobject , typename type > bool unaryfunc( funcobject fob , const type &val )//返回值不对 { return fob( val ); } void main() { string str = "poor"; bool sr = unaryfunc( bind2nd( equal_to <string >() , string"pooh" ) ) , str ); }
发表于:2007-10-29 13:40:342楼 得分:0
不知道绑定器怎么用,up一下!
发表于:2007-10-29 14:54:133楼 得分:7
这是类型的问题,不是绑定器的问题
发表于:2007-10-29 21:29:494楼 得分:0
嗯,唉,又是粗心问题,看来真的得好好午休了,谢谢ljf982713了
发表于:2007-10-29 21:33:085楼 得分:0
谢谢akirya了
发表于:2007-10-29 21:35:086楼 得分:0
谢谢wanfustudio了
发表于:2007-10-30 00:06:387楼 得分:6
template   <   typename   funcobject   ,   typename   type     >  
type   unaryfunc(   funcobject   fob   ,   const   type   &val   )  
{  
        return   fob(   val   );  
}  
  不用写这个函数,用绑定器就搞定了。


快速检索

最新资讯
热门点击