| 订阅 | 在线投稿
分享
 
 
当前位置: 王朝网络 >> c/c++ >> shou shen qian hou —— jian tan C++ yu yan jin hua
 

shou shen qian hou —— jian tan C++ yu yan jin hua

2008-06-01 02:10:58 编辑來源:互联网 评论
 
本文为【瘦身前后——兼谈C++语言进化】的拼音翻译版
  qian yi zhen zi xie le yi pian wen zhang , ti dao yu yan jin hua de zhi ze zhi yi , jiu shi qu chu yu yan zhong de tricks( zhi ze zhi er shi qu chu fei ben zhi fu duo xing )。
  chang kan wo blog de peng you ken ding ji de wo zeng xie guo de boost yuan ma pou xi xi lie 。 ben lai zhe ge xi lie shi da suan cheng shu de , dan sui zhe dui C++ de shu xi fa sheng le yi xie zhuan bian , dui yu yan ji ji shu de re zhong zhu jian xiao tui , zai hui guo tou lai kan boost ku zhong de yi xie zu jian , fa xian yuan ben jiao de hen you xie de bi yao de dong xi dun shi xiao shi le 。Scott Meyers de zhu ye shang ye lie you yi ge xie Boost Under The Hood de ji hua , yi zhi ye bu jian cheng wen , xing xu ye you lei si de yuan yin 。
  yi men yu yan ying gai shi “Make simple things simple, make complex things possible” de 。 dang wo men yong yu yan lai biao da si xiang de shi hou , zhe men yu yan ying gai neng gou ti gong zhe yang de neng li : ji rang wo men neng gou zui zhi jie di biao da wo men de yi si , duo yi fen ze tai duo , shao yi fen ze tai shao , hao bi gu ren xing rong mei nv : zeng yi fen ze tai fei , jian yi fen ze tai shou 。
  zhe ge wen ti shang , you yi ge wo ren wei shi guang fan de wu jie , jiu shi “KISS bian yi wei zhe yao jing jian yu yan , bing bi mian zai bian ma zhong shi yong ‘ gao jie ’ yu yan te xing ”。 dui ci you yi ju hua wo jiao de shuo de hao : ni bu neng tong guo cong yi men yu yan zhong qu diao dong xi lai zeng jia biao da li 。 gao jie te xing shi yi mian li ren , yong de bu hao gu ran shang le zi ji , dan zhe bing bu biao ming jiu mei you yong 。 ren he dong xi dou shi zai ta zhen zheng shi yong de di fang shi yong , ba wang ying shang gong de hua gong duan xian beng fan er shang ji zi shen 。 suo yi , jin jin yin wei gao jie te xing qing yi wu yong ( er qie gao jie te xing de que ye qing yi xi yin ren qu yong qie qing yi wu yong , bu guo zhe shi ling yi ge wen ti ), jiu duan ran zai ren he di fang dou bu yong bing xuan cheng zhe yang cai shi KISS de hua , bian yin ye fei shi le 。 ju ge li zi , gao jie han shu shi you yong de , jia ru zai zhen zheng xu yao gao jie han shu de di fang bu yong gao jie han shu , na bu shi KISS, zhi neng rang jie jue fang an ( huo zhe geng que qie di shuo ,workaround) geng fu duo 。lambda han shu shi you yong de , dan jia ru zai zhen zheng xu yao lambda de di fang bu shi yong lambda, ye zhi neng dao zhi geng fu duo geng bu zhi guan de workarounds。OOP shi you yong de , dan jia ru ni de cheng xu ben lai jiu zhi shi jian dan de “ shu ju + cao zuo ” ni pian yao ying shang OOP de hua , bu jin duo le bian ma shi jian , er qie huan jiang di cheng xu de ke jian du he ke wei hu xing , hou zhe jiu yi wei zhe xiang mu de money。 na C++ lai shuo , zhe shi yi ge guang wei gou bing de wen ti 。C++ de pian xiang di ceng de ying yong ling yu jue ding le you bu shao di fang shi yong C++ qi shi jiu shi “ shu ju + cao zuo ”, ran er hen duo ren que yin wei yong de shi C++ bian yi qi , bian ren bu zhu qu shi yong gao ji te xing , jie guo ba ben lai jian dan de shi qing fu duo hua —— wo zi ji jiu you bu shao ci zhe yang de jing li : yong le yi da dui lei zhi hou , zuo wan le hui guo tou lai zai kan , zhe xie lei dou gan ma lai zhe ? xu yao ma ? zui yao hai de jiu shi yao qing chu zi ji zuo de shi shen me shi qing , yi ji shen me gong ju cai shi dui ni suo zuo de shi qing zui shi he de 。
  shuo dao zhe li bu fang shun bian shuo shuo ling yi ge wu jie :“ jia ru wo fan zheng yong bu zhe C++ li mian de gao ji te xing , na huan bu ru yong C ba le ”, jian yu C/C++ de ying yong ling yu , de que you bu shao di fang shi ke yi yong C++ de C bu fen wan cheng de hen hao de , suo yi zhe ge wu jie bei chuan bo de huan shi man guang fan de 。 zhe li de yi ge wei miao de hu shi zai yu : yong C de hua , ni jiu yong bu dao xu duo hen hao de C++ ku le 。 yong C++ de hua , ni wan quan ke yi zai ni zi ji de bian ma zhong bu shi yong gao jie te xing ( shuo shi hua , zhe xu yao qing xing de tou nao he feng fu de jing yan , yi ji ke zhi neng li ), dan ni huan shi ke yi li yong zhong duo de C++ ku lai jian hua ni de gong zuo de : jia ru yi ge transform ming ming ke yi gao ding de ni pian yao xie yi ge for chu lai nan dao neng jiao KISS? jia ru yi ge vector jiu neng bi mian jue da duo shu nei cun zhi li lou dong he jian hua nei cun zhi li gong zuo ni pian pian yao shou dong malloc/free na neng jiao KISS( wo jian guo bu shao yong C++ bian ma que dao chu dou shi malloc/free de )? jia ru zui zhi jie de fang shi shi gc ni pian pian yao rao yi da dui wan zi cai neng bao zheng zheng que shi fang na ye bu jiao KISS( deng C++09 ba )。 jia ru yi ge for_each(readdir_sequence(".", readdir_sequence::files), ::remove); neng gao ding de ni pian yao xie :
  
  // in C
  DIR* dir = opendir(".");
  if(NULL != dir)
  {
  strUCt dirent* de;
  for(; NULL != (de = readdir(dir)); )
  {
  struct stat st;
  if( 0 == stat(de->d_name, &st) &&
  S_IFREG == (st.st_mode & S_IFMT))
  {
  remove(de->d_name);
  }
  }
  closedir(dir);
  }
  na neng jiao KISS?
  zong zhi huan shi na ju hua : ming que zhi dao ni xiang yao biao da de shi shen me bing yong zui jian jie ( zai bu sun hai qing yi li jie xing de qian ti xia ) de fang shi qu biao da ta 。 dan wo ren wei , zui KISS bu dai biao zui yuan shi 。
  jin hua —— liang ge li zi
  xian ju yi ge ping yi jin ren de li zi (Walter Bright——D yu yan fa ming zhe —— zeng zai ta de yi ge presentation zhong shi yong zhe ge li zi ), jia ru wo men xiang yao bian li yi ge shu zu , zai C li mian wo men shi zhe me zuo ( huo zhe yong zhi zhen , bu guo zhi zhen you zhi zhen zi ji de wen ti ):
  
  int arr[10];
  … // initialize arr
  for(int i = 0; i < 10; ++i)
  {
  int value = arr[i];
  …
  printf
  }
  zhe ge mao si jian dan de xun huan qi shi you ji ge zhu yao de wen ti :
  1. xia biao suo yin bu ying gai shi int, er ying gai shi size_t,int wei bi neng zu gou cun fang yi ge shu zu de xia biao 。
  2. value de lei xing yi kao yu arr nei yuan su de lei xing , wei fan DRY, jia ru arr de lei xing gai bian wei long huo unsigned, jiu ke neng fa sheng jie duan 。
  3. zhe zhong for zhi neng dui shu zu gong zuo , jia ru shi ling yi ge zi ding yi rong qi jiu bu xing le 。
  zai xian dai C++ li mian , ze shi zhe me zuo :
  
  for(std::vector<int>::iterator
  iter = v.begin();
  iter != v.end();
  ++iter) {
  …
  }
  qi shi zui da de wen ti jiu shi yi tian san bian de xie , ma fan 。for xun huan de zhe ge wen ti shang pian jiang auto de shi hou ye ti dao 。
  Walter Bright ran hou jiu ba D li mian zhi chi de foreach na chu lai dui bi ( dang ran , zhi chi foreach de yu yan tai duo le , zhe ye shuo ming le zhe ge jie gou de gao xiao xing )。
  
  foreach(i; v) {
  …
  }
  bu duo bu shao , gang hao biao da le yi si : dui v zhong de mei ge yuan su i zuo mou mou shi qing 。
  zhe ge li zi you ren shuo tai Na?ve le , qi shi wo ye zan cheng , de que , tian tian bu zhi dao you duo shao cheng xu yuan xie xia yi ge ge de xun huan jie gou , jiu jing you duo shao chu le shang mian ti dao de san ge wen ti ne ? zui da de wen ti kong pa huan shi shu zu yue jie 。 ci wai da jia ye dou qin shen ti yan guo wei fan DRY yuan ze de hou guo : gai le yi chu di fang de lei xing , bian yi , fa xian dao chu dou shi lei xing cuo wu , jie guo yi tong “ cha zhao —— ti huan ” shi mian bu le de le , shui shuo cheng xu yuan de shi jian shi bao gui de lai zhe ?
  ji ran zhe ge li zi tai Nave, na jiu shuo yi ge bu na me Nave de 。Java wei shen me yao jia ru closure? yi C++STL wei li , jia ru wo men yao :
  transform(v1.begin(), v1.end(), v2.begin(), v3.begin(), _1 + _2);
  ye jiu shi shuo jiang v1 he v2 li mian de yuan su dui ying xiang jia ran hou fang dao v3 dang zhong qu 。 zhe li yong le boost.lambda, dan da jia dou zhi dao boost.lambda you shi yi ge jing dian de ji lei 。_1 + _2 huan suan cou huo , yi dan biao da shi fu duo le , huo zhe qi zhong qian she dao dui qi ta han shu de tiao yong le , jian zhi jiu shi yi chang e meng , bi ru shuo wo men xiang ba v1 he v2 zhong xiang ying yuan su zhe yang xiang jia :f(_1) + f(_2), qi zhong f shi yi ge han shu huo fang han shu , ke yi zuo jia quan huo zhe qi ta chu li , na me wo men ke yi xiang xia mian zhe yang xie ma :
  transform(…, f(_1) + f(_2));
  da an shi bu xing , ni de zhe yang xie :
  
  transform(…,
  boost::bind(std::plus<int>(), boost::bind(f, _1), boost::bind(f, _1))
  );
  Lisper men xiao le ,Haskeller men xiao le , jiu lian Javaer men dou xiao le 。It’s not even funny! zhe xian ran wei fan le “simple things should be simple” yuan ze 。
  jia ru bu xiang juan ru C++ functional de e meng de hua , ni ye ke yi zhe me xie :
  
  struct Op
  {
  int operator()(int a1, int a2) { return f(a1) + f(a2); }
  };
  transform(…, Op());
  shao wei hao yi dian , dan zhe zhong zuo fa ye you hen yan chong de wen ti 。
  wei shen me Java jia ru closure, qi shi huan shi yi ge yu fa wen ti 。 cong yan ge yi yi shang ,Java de anonymous class yi jing ke yi shi xian chu yi yang de gong neng le , zheng ru C++ de functor yi yang 。 ran er , dai ma shi gei ren kan de , yu yan shi gei ren yong lai xie dai ma de , dai ma de zhu yao dai jia zai wei hu , wei hu ze xu yao yue du 、 li jie 。 xie dai ma de ren bu xi wang duo hua bi mo lai xie na xie zi ji ben bu guan xin de dong xi , du dai ma de ren ye xi wang “ suo du ji suo biao ”, bu xiang kan dao dai ma li mian you shen me wan zi , zui hao shi zi ran yu yan zi ran chou xiang cai hao ne 。
  suo yi , jin guan closure shi yi ke yu fa tang , dan que shi yi ke hen tian hen tian de tang , yin wei you le closure ni jiu ke yi xie :
  
  transform(…, <>(a1, a2){ f(a1) + f(a2) });
  Simple things should be simple!
  ci wai ,closure zui qiang da de hao chu huan shi zai yu dui ju bu bian liang de fang bian de yin yong , she xiang wo men xiang yao chuang jian de biao da shi shi :
  
  int weight1 = 0.3, weight2 = 0.6;
  transform(…, f(_1)*weight1 + f(_2)*weight2);
  dang ran , shang mian de yu ju shi fei fa de , bu guo shi yong closure bian ke yi xie cheng :
  
  int weight1 = 0.3, weight2 = 0.6;
  transform(…, <&>(_1, _2){ f(_1)*weight1 + f(_2)*weight2 } );
  yong functor class lai shi xian tong yang de gong neng ze yao ma fan xu duo , yi dan ma fan , jiu hui error-prone, yi dan error-prone, jiu hui xiao hao ren li , er ren li , jiu shi jin qian 。
  C++09 ye you xi wang jia ru lambda, bu guo zhe shi ling yi ge hua ti , xia hui zai shuo 。
  The Real Deal——variadic templates
  C++ de callback lei ,Google yi xia , mei you yi da ye you ban da 。 qi zhong you shu boost.function shi xian de zui wei ling huo zhou dao 。 ran er , jiu zai qi ling huo zhou dao de jie kou xia mian , que shi rang ren bu ren zu du de shi xian ;03 nian de shi hou wo xie de di yi pian boost yuan ma pou xi jiu shi boost.function de , dang shi huan jiao de neng kan dong na yang de dai ma niu de bu xing ... hua shuo hui lai , na pian wen zhang zhu yao pou xi le liang ge fang mian , yi ge shi ta dui bu tong shen shu de han shu lei xing shi ru he chu li de , di er ge shi yi ge type-erase she shi 。 qi zhong di yi ge fang mian jiu zhan qu le da bu fen de pian fu 。
  jian er yan zhi , yao shi xian yi ge fan xing de callback lei , jiu bi xu shi xian yi xia zui chang jian de ying yong chang jing :
  
  function<int(int, int)> caller = f;
  int r = caller(1, 2); // call f
  wei ci function lei mo ban li mian ken ding yao you yi ge operator(), ran er , jie xia lai , ru he ding yi zhe ge operator() jiu cheng le wen ti :
  
  template<Signature>
  class function
  {
  operator()(???);
  };
  ??? chu tian shen me ? fan hui zhi chu de ??? ke yi jie jue , yong yi ge traits:typename result_type<Signature>::type, dan shen shu lie biao chu de ??? ne ?
  boost cai yong de ban fa ye shi C++98 wei yi de ban fa , jiu shi wei bu tong shen shu ge shu de Signature jin xing te hua :
  
  template<typename R, typename T1>
  class function<R(T1)>
  {
  R operator()(T1 a1);
  };
  template<typename R, typename T1, typename T2>
  class function<R(T1, T2)>
  {
  R operator()(T1 a1, T2 a2);
  };
  template<typename R, typename T1, typename T2, typename T3>
  class function<R(T1, T2, T3)>
  {
  R operator()(T1 a1, T2 a2, T3 a3);
  };
  … // zai xie xia qu ye kuan bu gou le , da zhu …
  ru ci yi gong N(N you yi ge hong kong zhi ) ge ban ben 。
  zhe zhong zuo fa you liang ge wen ti : yi , han shu de shen shu ge shu shi zhong huan shi shou xian de , ni zuo chu N ge te hua ban ben , na me dui N+1 ge shen shu de han shu jiu mei zhe le 。boost::tuple ye shi zhe ge wen ti 。 er , dai ma chong fu 。 mei ge te hua ban ben li mian chu le shen shu ge shu bu tong zhi wai ji ben qi ta dou shi xiang tong de ;boost jie jue zhe ge wen ti de ban fa shi li yong hong , hong ben shen de yi da dui wen ti jiu bu shuo le , ni zhi yao da kai boost.function de zhu ti shi xian dai ma jiu zhi dao you duo zao gao le , jin yi qian xing dai ma , qi zhong she ji yuan bian cheng he hong ji qiao wu shu , ke du xing ke yi shuo ji ben wei 0。 hao zai zhe shi ge biao zhun ku (boost.function jiang jia ru tr1) bu yong ni wei hu , jia ru shi ni zi ji xie le yong de ku , kong pa chu le ni shui ye bie xiang dong le 。 suo yi di er ge wen ti qi shi jiu shi ke du xing ke wei hu xing wen ti , yong Matthew Wilson de shuo fa jiu shi ke fa xian xing he tou ming xing de wen ti , zhe shi yi ge hen yan chong de wen ti , xu duo C++ xian dai ku yin wei zhe ge wen ti er zao dao gou bing 。
  xian zai , rang wo men lai kan yi kan jia ru le variadic templates zhi hou de C++09 shi xian :
  
  template<typename R, typename... Args>
  struct invoker_base {
  virtual R invoke(Args...) = 0;
  virtual ~invoker_base() { }
  };
  template<typename F, typename R, typename... Args>
  struct functor_invoker : public invoker_base<R, Args...>
  {
  eXPlicit functor_invoker(F f) : f(f) { }
  R invoke(Args... args) { return f(args...); }
  private:
  F f;
  };
  template<typename Signature>
  class function;
  template<typename R, typename... Args>
  class function<R (Args...)>
  {
  public:
  template<typename F>
  function(F f) : invoker(0)
  {
  invoker = new functor_invoker<F, R, Args...>(f);
  }
  R operator()(Args... args) const
  {
  return invoker->invoke(args...);
  }
  private:
  invoker_base<R, Args...>* invoker;
  };
  zheng ge gai xin shi xian jiu zhe xie ! yi gong cai 36 xing ! jia shang xi gou han shu kao bei gou zao han shu deng bian jiao liao yi gong ye jiu 70 xing ! geng chong yao de shi , zheng ge dai ma qing chu wu bi , suo you she ji dao ke bian shu mu ge mo ban shen shu de di fang dou you variadic templates dai ti 。“Args…” qia ru qi fen de biao da le wo men xiang yao biao da de yi si —— duo ge shen shu ( shu mu bu guan )。 yu C++98 de boost.function shi xian zhen shi tian rang zhi bie !
  zhe li function_invoker shi yong de type-erase shou fa , ju ti ke shen jian wo yi qian xie de boost.any yuan ma pou xi , huo shang pian jiang auto de , huo 《C++ Template Metaprogramming》( nei you yuan bian cheng shen ru !)。type-erase shou fa shi xiang C++ zhe yang de ruo RTTI zhi chi de yu yan zhong shao shu zhen zheng shi yong de shou fa , mou zhong cheng du shang she ji mo shi li mian de adapter mo shi ye shi type-erase de yi ge bian zhong 。
  jia ru huan jiao de bu gou de hua , ke yi shen kao variadic-templates de zhu ye , shang mian de variadic templates proposal zhong dai le san ge tr1 shi xian , fen bie shi tuple,bind,function, dang ran ,variadic-templates de hao chu yuan yuan bu jin jin zhi yu zhe san ge shi xian , cong ben zhi shang ta ti gong le yi zhong zhen zheng zhi jie de biao da yi tu de gong ju , wan quan bi kai le xiang xia mian zhe zhong horrible de workaround:
  
  template<class T1>
  cons(T1& t1, const null_type&, const null_type&, const null_type&,
  const null_type&, const null_type&, const null_type&,
  const null_type&, const null_type&, const null_type&)
  : head (t1) {}
  tuple de C++98 shi xian , dai ma jin qian xing 。 li yong variadic-templates shi xian , dai ma jin bai xing 。
  he zhe zhong geng horrible de workaround:
  
  template<class R, class F, class A1, class A2, class A3, class A4, class A5, class A6>
  _bi::bind_t<R, F, typename _bi::list_av_6<A1, A2, A3, A4, A5, A6>::type>
  BOOST_BIND(boost::type<R>, F f, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6)
  {
  typedef typename _bi::list_av_6<A1, A2, A3, A4, A5, A6>::type list_type;
  return _bi::bind_t<R, F, list_type>(f, list_type(a1, a2, a3, a4, a5, a6));
  }
  xiao xiao de boost.bind, shi xian dai ma yu liang qian xing , qi jian chong fu dai ma wu shu 。 yong le variadic-templates, shi xian bu guo bai xing 。
  BTW. variadic templates zai C++ da hui shang yi ci xing ji hu quan shu tou piao tong guo 。lambda neng bu neng jin biao zhun ze yao kan ji ge ti an zhe de gong zuo 。 mu qian huan mei you Wording chu lai 。 bu guo zhi yao chu le wording xiang bi ye hui xiang variadic templates na yang ya dao xing tong guo de 。【原文】【汉音对照
 
 
 
 94.*.*.* 发表于2012-07-20 22:15:43
  At last, someone comes up with the "right" asnwer!
  回复
  
  
 
 
 
 
 
 
日版宠物情人插曲《Winding Road》歌词

日版宠物情人2017的插曲,很带节奏感,日语的,女生唱的。 最后听见是在第8集的时候女主手割伤了,然后男主用嘴帮她吸了一下,插曲就出来了。 歌手:Def...

兄弟共妻,我成了他们夜里的美食

老钟家的两个儿子很特别,就是跟其他的人不太一样,魔一般的执着。兄弟俩都到了要结婚的年龄了,不管自家老爹怎么磨破嘴皮子,兄弟俩说不娶就不娶,老父母为兄弟两操碎了心...

如何磨出破洞牛仔裤?牛仔裤怎么剪破洞?

把牛仔裤磨出有线的破洞 1、具体工具就是磨脚石,下面垫一个硬物,然后用磨脚石一直磨一直磨,到把那块磨薄了,用手撕开就好了。出来的洞啊很自然的。需要猫须的话调几...

我就是扫描下图得到了敬业福和爱国福

先来看下敬业福和爱国福 今年春节,支付宝再次推出了“五福红包”活动,表示要“把欠大家的敬业福都还给大家”。 今天该活动正式启动,和去年一样,需要收集“五福”...

冰箱异味产生的原因和臭味去除的方法

有时候我们打开冰箱就会闻到一股异味,冰箱里的这种异味是因为一些物质发出的气味的混合体,闻起来让人恶心。 产生这些异味的主要原因有以下几点。 1、很多人有这种习...

 
 
   qian yi zhen zi xie le yi pian wen zhang , ti dao yu yan jin hua de zhi ze zhi yi , jiu shi qu chu yu yan zhong de tricks( zhi ze zhi er shi qu chu fei ben zhi fu duo xing )。    chang kan wo blog de peng you ken ding ji de wo zeng xie guo de boost yuan ma pou xi xi lie 。 ben lai zhe ge xi lie shi da suan cheng shu de , dan sui zhe dui C++ de shu xi fa sheng le yi xie zhuan bian , dui yu yan ji ji shu de re zhong zhu jian xiao tui , zai hui guo tou lai kan boost ku zhong de yi xie zu jian , fa xian yuan ben jiao de hen you xie de bi yao de dong xi dun shi xiao shi le 。Scott Meyers de zhu ye shang ye lie you yi ge xie Boost Under The Hood de ji hua , yi zhi ye bu jian cheng wen , xing xu ye you lei si de yuan yin 。    yi men yu yan ying gai shi “Make simple things simple, make complex things possible” de 。 dang wo men yong yu yan lai biao da si xiang de shi hou , zhe men yu yan ying gai neng gou ti gong zhe yang de neng li : ji rang wo men neng gou zui zhi jie di biao da wo men de yi si , duo yi fen ze tai duo , shao yi fen ze tai shao , hao bi gu ren xing rong mei nv : zeng yi fen ze tai fei , jian yi fen ze tai shou 。    zhe ge wen ti shang , you yi ge wo ren wei shi guang fan de wu jie , jiu shi “KISS bian yi wei zhe yao jing jian yu yan , bing bi mian zai bian ma zhong shi yong ‘ gao jie ’ yu yan te xing ”。 dui ci you yi ju hua wo jiao de shuo de hao : ni bu neng tong guo cong yi men yu yan zhong qu diao dong xi lai zeng jia biao da li 。 gao jie te xing shi yi mian li ren , yong de bu hao gu ran shang le zi ji , dan zhe bing bu biao ming jiu mei you yong 。 ren he dong xi dou shi zai ta zhen zheng shi yong de di fang shi yong , ba wang ying shang gong de hua gong duan xian beng fan er shang ji zi shen 。 suo yi , jin jin yin wei gao jie te xing qing yi wu yong ( er qie gao jie te xing de que ye qing yi xi yin ren qu yong qie qing yi wu yong , bu guo zhe shi ling yi ge wen ti ), jiu duan ran zai ren he di fang dou bu yong bing xuan cheng zhe yang cai shi KISS de hua , bian yin ye fei shi le 。 ju ge li zi , gao jie han shu shi you yong de , jia ru zai zhen zheng xu yao gao jie han shu de di fang bu yong gao jie han shu , na bu shi KISS, zhi neng rang jie jue fang an ( huo zhe geng que qie di shuo ,workaround) geng fu duo 。lambda han shu shi you yong de , dan jia ru zai zhen zheng xu yao lambda de di fang bu shi yong lambda, ye zhi neng dao zhi geng fu duo geng bu zhi guan de workarounds。OOP shi you yong de , dan jia ru ni de cheng xu ben lai jiu zhi shi jian dan de “ shu ju + cao zuo ” ni pian yao ying shang OOP de hua , bu jin duo le bian ma shi jian , er qie huan jiang di cheng xu de ke jian du he ke wei hu xing , hou zhe jiu yi wei zhe xiang mu de money。 na C++ lai shuo , zhe shi yi ge guang wei gou bing de wen ti 。C++ de pian xiang di ceng de ying yong ling yu jue ding le you bu shao di fang shi yong C++ qi shi jiu shi “ shu ju + cao zuo ”, ran er hen duo ren que yin wei yong de shi C++ bian yi qi , bian ren bu zhu qu shi yong gao ji te xing , jie guo ba ben lai jian dan de shi qing fu duo hua —— wo zi ji jiu you bu shao ci zhe yang de jing li : yong le yi da dui lei zhi hou , zuo wan le hui guo tou lai zai kan , zhe xie lei dou gan ma lai zhe ? xu yao ma ? zui yao hai de jiu shi yao qing chu zi ji zuo de shi shen me shi qing , yi ji shen me gong ju cai shi dui ni suo zuo de shi qing zui shi he de 。    shuo dao zhe li bu fang shun bian shuo shuo ling yi ge wu jie :“ jia ru wo fan zheng yong bu zhe C++ li mian de gao ji te xing , na huan bu ru yong C ba le ”, jian yu C/C++ de ying yong ling yu , de que you bu shao di fang shi ke yi yong C++ de C bu fen wan cheng de hen hao de , suo yi zhe ge wu jie bei chuan bo de huan shi man guang fan de 。 zhe li de yi ge wei miao de hu shi zai yu : yong C de hua , ni jiu yong bu dao xu duo hen hao de C++ ku le 。 yong C++ de hua , ni wan quan ke yi zai ni zi ji de bian ma zhong bu shi yong gao jie te xing ( shuo shi hua , zhe xu yao qing xing de tou nao he feng fu de jing yan , yi ji ke zhi neng li ), dan ni huan shi ke yi li yong zhong duo de C++ ku lai jian hua ni de gong zuo de : jia ru yi ge transform ming ming ke yi gao ding de ni pian yao xie yi ge for chu lai nan dao neng jiao KISS? jia ru yi ge vector jiu neng bi mian jue da duo shu nei cun zhi li lou dong he jian hua nei cun zhi li gong zuo ni pian pian yao shou dong malloc/free na neng jiao KISS( wo jian guo bu shao yong C++ bian ma que dao chu dou shi malloc/free de )? jia ru zui zhi jie de fang shi shi gc ni pian pian yao rao yi da dui wan zi cai neng bao zheng zheng que shi fang na ye bu jiao KISS( deng C++09 ba )。 jia ru yi ge for_each(readdir_sequence(".", readdir_sequence::files), ::remove); neng gao ding de ni pian yao xie : // in C DIR* dir = opendir("."); if(NULL != dir) { strUCt dirent* de; for(; NULL != (de = readdir(dir)); ) { struct stat st; if( 0 == stat(de->d_name, &st) && S_IFREG == (st.st_mode & S_IFMT)) { remove(de->d_name); } }  closedir(dir); }    na neng jiao KISS?    zong zhi huan shi na ju hua : ming que zhi dao ni xiang yao biao da de shi shen me bing yong zui jian jie ( zai bu sun hai qing yi li jie xing de qian ti xia ) de fang shi qu biao da ta 。 dan wo ren wei , zui KISS bu dai biao zui yuan shi 。    jin hua —— liang ge li zi    xian ju yi ge ping yi jin ren de li zi (Walter Bright——D yu yan fa ming zhe —— zeng zai ta de yi ge presentation zhong shi yong zhe ge li zi ), jia ru wo men xiang yao bian li yi ge shu zu , zai C li mian wo men shi zhe me zuo ( huo zhe yong zhi zhen , bu guo zhi zhen you zhi zhen zi ji de wen ti ): int arr[10]; … // initialize arr for(int i = 0; i < 10; ++i) { int value = arr[i]; … printf }    zhe ge mao si jian dan de xun huan qi shi you ji ge zhu yao de wen ti :   1. xia biao suo yin bu ying gai shi int, er ying gai shi size_t,int wei bi neng zu gou cun fang yi ge shu zu de xia biao 。   2. value de lei xing yi kao yu arr nei yuan su de lei xing , wei fan DRY, jia ru arr de lei xing gai bian wei long huo unsigned, jiu ke neng fa sheng jie duan 。   3. zhe zhong for zhi neng dui shu zu gong zuo , jia ru shi ling yi ge zi ding yi rong qi jiu bu xing le 。    zai xian dai C++ li mian , ze shi zhe me zuo : for(std::vector<int>::iterator iter = v.begin(); iter != v.end(); ++iter) { … }    qi shi zui da de wen ti jiu shi yi tian san bian de xie , ma fan 。for xun huan de zhe ge wen ti shang pian jiang auto de shi hou ye ti dao 。   Walter Bright ran hou jiu ba D li mian zhi chi de foreach na chu lai dui bi ( dang ran , zhi chi foreach de yu yan tai duo le , zhe ye shuo ming le zhe ge jie gou de gao xiao xing )。 foreach(i; v) { … }    bu duo bu shao , gang hao biao da le yi si : dui v zhong de mei ge yuan su i zuo mou mou shi qing 。    zhe ge li zi you ren shuo tai Na?ve le , qi shi wo ye zan cheng , de que , tian tian bu zhi dao you duo shao cheng xu yuan xie xia yi ge ge de xun huan jie gou , jiu jing you duo shao chu le shang mian ti dao de san ge wen ti ne ? zui da de wen ti kong pa huan shi shu zu yue jie 。 ci wai da jia ye dou qin shen ti yan guo wei fan DRY yuan ze de hou guo : gai le yi chu di fang de lei xing , bian yi , fa xian dao chu dou shi lei xing cuo wu , jie guo yi tong “ cha zhao —— ti huan ” shi mian bu le de le , shui shuo cheng xu yuan de shi jian shi bao gui de lai zhe ?    ji ran zhe ge li zi tai Nave, na jiu shuo yi ge bu na me Nave de 。Java wei shen me yao jia ru closure? yi C++STL wei li , jia ru wo men yao : transform(v1.begin(), v1.end(), v2.begin(), v3.begin(), _1 + _2);    ye jiu shi shuo jiang v1 he v2 li mian de yuan su dui ying xiang jia ran hou fang dao v3 dang zhong qu 。 zhe li yong le boost.lambda, dan da jia dou zhi dao boost.lambda you shi yi ge jing dian de ji lei 。_1 + _2 huan suan cou huo , yi dan biao da shi fu duo le , huo zhe qi zhong qian she dao dui qi ta han shu de tiao yong le , jian zhi jiu shi yi chang e meng , bi ru shuo wo men xiang ba v1 he v2 zhong xiang ying yuan su zhe yang xiang jia :f(_1) + f(_2), qi zhong f shi yi ge han shu huo fang han shu , ke yi zuo jia quan huo zhe qi ta chu li , na me wo men ke yi xiang xia mian zhe yang xie ma : transform(…, f(_1) + f(_2));    da an shi bu xing , ni de zhe yang xie : transform(…, boost::bind(std::plus<int>(), boost::bind(f, _1), boost::bind(f, _1)) );   Lisper men xiao le ,Haskeller men xiao le , jiu lian Javaer men dou xiao le 。It’s not even funny! zhe xian ran wei fan le “simple things should be simple” yuan ze 。    jia ru bu xiang juan ru C++ functional de e meng de hua , ni ye ke yi zhe me xie : struct Op { int operator()(int a1, int a2) { return f(a1) + f(a2); } }; transform(…, Op());    shao wei hao yi dian , dan zhe zhong zuo fa ye you hen yan chong de wen ti 。    wei shen me Java jia ru closure, qi shi huan shi yi ge yu fa wen ti 。 cong yan ge yi yi shang ,Java de anonymous class yi jing ke yi shi xian chu yi yang de gong neng le , zheng ru C++ de functor yi yang 。 ran er , dai ma shi gei ren kan de , yu yan shi gei ren yong lai xie dai ma de , dai ma de zhu yao dai jia zai wei hu , wei hu ze xu yao yue du 、 li jie 。 xie dai ma de ren bu xi wang duo hua bi mo lai xie na xie zi ji ben bu guan xin de dong xi , du dai ma de ren ye xi wang “ suo du ji suo biao ”, bu xiang kan dao dai ma li mian you shen me wan zi , zui hao shi zi ran yu yan zi ran chou xiang cai hao ne 。    suo yi , jin guan closure shi yi ke yu fa tang , dan que shi yi ke hen tian hen tian de tang , yin wei you le closure ni jiu ke yi xie : transform(…, <>(a1, a2){ f(a1) + f(a2) }); Simple things should be simple!    ci wai ,closure zui qiang da de hao chu huan shi zai yu dui ju bu bian liang de fang bian de yin yong , she xiang wo men xiang yao chuang jian de biao da shi shi : int weight1 = 0.3, weight2 = 0.6; transform(…, f(_1)*weight1 + f(_2)*weight2);    dang ran , shang mian de yu ju shi fei fa de , bu guo shi yong closure bian ke yi xie cheng : int weight1 = 0.3, weight2 = 0.6; transform(…, <&>(_1, _2){ f(_1)*weight1 + f(_2)*weight2 } );    yong functor class lai shi xian tong yang de gong neng ze yao ma fan xu duo , yi dan ma fan , jiu hui error-prone, yi dan error-prone, jiu hui xiao hao ren li , er ren li , jiu shi jin qian 。   C++09 ye you xi wang jia ru lambda, bu guo zhe shi ling yi ge hua ti , xia hui zai shuo 。 The Real Deal——variadic templates   C++ de callback lei ,Google yi xia , mei you yi da ye you ban da 。 qi zhong you shu boost.function shi xian de zui wei ling huo zhou dao 。 ran er , jiu zai qi ling huo zhou dao de jie kou xia mian , que shi rang ren bu ren zu du de shi xian ;03 nian de shi hou wo xie de di yi pian boost yuan ma pou xi jiu shi boost.function de , dang shi huan jiao de neng kan dong na yang de dai ma niu de bu xing ... hua shuo hui lai , na pian wen zhang zhu yao pou xi le liang ge fang mian , yi ge shi ta dui bu tong shen shu de han shu lei xing shi ru he chu li de , di er ge shi yi ge type-erase she shi 。 qi zhong di yi ge fang mian jiu zhan qu le da bu fen de pian fu 。    jian er yan zhi , yao shi xian yi ge fan xing de callback lei , jiu bi xu shi xian yi xia zui chang jian de ying yong chang jing : function<int(int, int)> caller = f; int r = caller(1, 2); // call f    wei ci function lei mo ban li mian ken ding yao you yi ge operator(), ran er , jie xia lai , ru he ding yi zhe ge operator() jiu cheng le wen ti : template<Signature> class function { operator()(???); };   ??? chu tian shen me ? fan hui zhi chu de ??? ke yi jie jue , yong yi ge traits:typename result_type<Signature>::type, dan shen shu lie biao chu de ??? ne ?   boost cai yong de ban fa ye shi C++98 wei yi de ban fa , jiu shi wei bu tong shen shu ge shu de Signature jin xing te hua : template<typename R, typename T1> class function<R(T1)> { R operator()(T1 a1); }; template<typename R, typename T1, typename T2> class function<R(T1, T2)> { R operator()(T1 a1, T2 a2); }; template<typename R, typename T1, typename T2, typename T3> class function<R(T1, T2, T3)> { R operator()(T1 a1, T2 a2, T3 a3); }; … // zai xie xia qu ye kuan bu gou le , da zhu …    ru ci yi gong N(N you yi ge hong kong zhi ) ge ban ben 。    zhe zhong zuo fa you liang ge wen ti : yi , han shu de shen shu ge shu shi zhong huan shi shou xian de , ni zuo chu N ge te hua ban ben , na me dui N+1 ge shen shu de han shu jiu mei zhe le 。boost::tuple ye shi zhe ge wen ti 。 er , dai ma chong fu 。 mei ge te hua ban ben li mian chu le shen shu ge shu bu tong zhi wai ji ben qi ta dou shi xiang tong de ;boost jie jue zhe ge wen ti de ban fa shi li yong hong , hong ben shen de yi da dui wen ti jiu bu shuo le , ni zhi yao da kai boost.function de zhu ti shi xian dai ma jiu zhi dao you duo zao gao le , jin yi qian xing dai ma , qi zhong she ji yuan bian cheng he hong ji qiao wu shu , ke du xing ke yi shuo ji ben wei 0。 hao zai zhe shi ge biao zhun ku (boost.function jiang jia ru tr1) bu yong ni wei hu , jia ru shi ni zi ji xie le yong de ku , kong pa chu le ni shui ye bie xiang dong le 。 suo yi di er ge wen ti qi shi jiu shi ke du xing ke wei hu xing wen ti , yong Matthew Wilson de shuo fa jiu shi ke fa xian xing he tou ming xing de wen ti , zhe shi yi ge hen yan chong de wen ti , xu duo C++ xian dai ku yin wei zhe ge wen ti er zao dao gou bing 。    xian zai , rang wo men lai kan yi kan jia ru le variadic templates zhi hou de C++09 shi xian : template<typename R, typename... Args> struct invoker_base { virtual R invoke(Args...) = 0; virtual ~invoker_base() { } }; template<typename F, typename R, typename... Args> struct functor_invoker : public invoker_base<R, Args...> { eXPlicit functor_invoker(F f) : f(f) { } R invoke(Args... args) { return f(args...); } private: F f; }; template<typename Signature> class function; template<typename R, typename... Args> class function<R (Args...)> { public: template<typename F> function(F f) : invoker(0) { invoker = new functor_invoker<F, R, Args...>(f); } R operator()(Args... args) const { return invoker->invoke(args...); } private: invoker_base<R, Args...>* invoker; };    zheng ge gai xin shi xian jiu zhe xie ! yi gong cai 36 xing ! jia shang xi gou han shu kao bei gou zao han shu deng bian jiao liao yi gong ye jiu 70 xing ! geng chong yao de shi , zheng ge dai ma qing chu wu bi , suo you she ji dao ke bian shu mu ge mo ban shen shu de di fang dou you variadic templates dai ti 。“Args…” qia ru qi fen de biao da le wo men xiang yao biao da de yi si —— duo ge shen shu ( shu mu bu guan )。 yu C++98 de boost.function shi xian zhen shi tian rang zhi bie !    zhe li function_invoker shi yong de type-erase shou fa , ju ti ke shen jian wo yi qian xie de boost.any yuan ma pou xi , huo shang pian jiang auto de , huo 《C++ Template Metaprogramming》( nei you yuan bian cheng shen ru !)。type-erase shou fa shi xiang C++ zhe yang de ruo RTTI zhi chi de yu yan zhong shao shu zhen zheng shi yong de shou fa , mou zhong cheng du shang she ji mo shi li mian de adapter mo shi ye shi type-erase de yi ge bian zhong 。    jia ru huan jiao de bu gou de hua , ke yi shen kao variadic-templates de zhu ye , shang mian de variadic templates proposal zhong dai le san ge tr1 shi xian , fen bie shi tuple,bind,function, dang ran ,variadic-templates de hao chu yuan yuan bu jin jin zhi yu zhe san ge shi xian , cong ben zhi shang ta ti gong le yi zhong zhen zheng zhi jie de biao da yi tu de gong ju , wan quan bi kai le xiang xia mian zhe zhong horrible de workaround: template<class T1> cons(T1& t1, const null_type&, const null_type&, const null_type&, const null_type&, const null_type&, const null_type&, const null_type&, const null_type&, const null_type&) : head (t1) {}   tuple de C++98 shi xian , dai ma jin qian xing 。 li yong variadic-templates shi xian , dai ma jin bai xing 。    he zhe zhong geng horrible de workaround: template<class R, class F, class A1, class A2, class A3, class A4, class A5, class A6> _bi::bind_t<R, F, typename _bi::list_av_6<A1, A2, A3, A4, A5, A6>::type> BOOST_BIND(boost::type<R>, F f, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6) { typedef typename _bi::list_av_6<A1, A2, A3, A4, A5, A6>::type list_type; return _bi::bind_t<R, F, list_type>(f, list_type(a1, a2, a3, a4, a5, a6)); }    xiao xiao de boost.bind, shi xian dai ma yu liang qian xing , qi jian chong fu dai ma wu shu 。 yong le variadic-templates, shi xian bu guo bai xing 。   BTW. variadic templates zai C++ da hui shang yi ci xing ji hu quan shu tou piao tong guo 。lambda neng bu neng jin biao zhun ze yao kan ji ge ti an zhe de gong zuo 。 mu qian huan mei you Wording chu lai 。 bu guo zhi yao chu le wording xiang bi ye hui xiang variadic templates na yang ya dao xing tong guo de 。
󰈣󰈤
  免责声明:本文仅代表作者个人观点,与王朝网络无关。王朝网络登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
上海柏雅居酒店模拍
清晨美丽的风景
梦清园的靓丽女生
妖艳的食人花(7)
且行且攝------"色"影大連
【做股票的朋友看过来-华尔街的铜牛】
端午节后闲逛
捐款赈灾吴川行 花絮
 
>>返回首页<<
 为你推荐
 
 
 转载本文
 UBB代码 HTML代码
复制到剪贴板...
 
 热帖排行
 
 
 
 
©2005- 王朝网络 版权所有