æ»èªã¨ãããä»äº
ã¨ãã©ãç§ã¯æ¸ç±ã®ç£ä¿®ãããã¦ããããã¨ããããæ»èªãããã¦ããããã¨ãçµæ§ããã
æ»èªã®ã¨ãã¯ãç§ã¯ãéã¯ããããªããã¨ã«ãã¦ãããããã®æã®å稿ãæ¸ãæ £ãã¦ããªã人ã®å ´åã1ãã¼ã¸ã«5ç®æãããããããã¨ãããããã®ãæ®éã§ãã£ã¦ã300ãã¼ã¸ããã°ãå°ãªãã¨ã1000ç®æãããã¯èª¤åããè±åããã説æã®ä»æ¹ãã¾ããã¨ããããä½ãããããã
ä¸æããã¨èªåã§æ¬ãæ¸ãä½åãã®æéããããã®ãæ®éãªã®ã§ãããããã¼ã®ã£ã©ã¨ããã®ã¯ãæ¬å½ã«å²ã«åããªãããã¾ããããã§ããæ¬ãä¸ã«åºãã°ç¤¾ä¼è²¢ç®ã«ã¤ãªããã¨æãããèªåã®åå¼·ã«ããªãã®ã§é²ãã§ãããã¦ããã ããã¨ã«ãã¦ããã
ç§ãã©ãããæãã§æ»èªããããç¥ã£ã¦ãããããã«ãç§ãå ¨ãé¢ãã£ã¦ãããªãæ¬ã§ã¯ããããC++ãã³ãã¬ã¼ããã¯ããã¯ãä¾ã«ã¨ã£ã¦ã¿ããã
ä½æ ãã®æ¬ãé¸ãã ãã¨è¨ãã¨ãèè ã®æ¹ã¯ãäºäººã¨ãC++æ¨æºåå§å¡ä¼ã®ã¡ã³ãã¼ãªã®ã ãããããç¨åº¦C++çã«è¦ã¦æ£ç¢ºãæãã¦ãã¦ç¶ãã¹ãã§ããã¨ããã®ãããã
ããã¦ãεÏιÏÏημηã®ãã£ã¡ããã¯ãããä½åãæ¬ãæ¸ãã¦ããã®ã ããããã£ã¨ãèªåã®å稿ã«å¯¾ãã¦ã·ãã¢ãªç®ã§è¦ã¦æ¬²ããã¨æãããããã²ã¨ãã®èè ã§ããé«æ©ãã(id:faith_and_braveãã)ã®ã»ãã¯ãåéææãããªéå¹´ãªã®ã§ããã¶ãå°æ¥ãã¾ã ä½åãæ¬ãåºãã ããããããããæ©ã«ããããèãã¦æ¬²ããã¨æãã®ã§ããã
ç§ãä»åé¸ãã ã®ã¯ãpp.34-35ãããã§ç§ãæ°ã«ãªã£ãã®ã¯ã以ä¸ã®é¨åãC++ã®templateã®ä»æ§ã®ãªãã§ãtwo phase lookupã¯é¬¼éã§ãããç§ãªãããC++æ¨æºåå§å¡ä¼ã®äººã«ä½ãè¨ãã®ã¯ãã¡ããé迦ã«èª¬æ³ã ããç§ã¯ä»¥ä¸ã§çµæ§ééã£ããã¨ãè¨ã£ã¦ãããç¥ããªããã§ãããããééããæãã¦ãã¦ã¯æ»èªã¯åºæ¥ãªãã®ã§ãç§ã¯æ»èªã®ã¨ãã¯ãç§ã¯ããæããã¨ããã®ãç©æ¥µçã«çºè¨ããããã«ãã¦ããã以ä¸ã§ãããããã¹ã¿ã³ã¹ã§æ¸ãã¦ã¿ãã
以ä¸ã§ã¯ããä¸ç¥ãã®ä»£ããã«"â¦"ãç¨ããã
0. ã解é(åå解決)ãã®ããã«æ¬å¼§ãã¤ãã¦ä½ããè£è¶³ããå ´åããã®æ¬å¼§ãæ¶å»ãã¦ãæå³ãéããªããã°ãªããªãããã®æ¬å¼§ãæ¶å»ããã¨ãããã³ãã¬ã¼ãã®è§£éã¯ãâ¦ãã¨ãªããããã®å¾ãã§ååã®è§£æ±ºã¨ãã説æã2度åºã¦ãã¦ããã®ã§ãããã ã¨ããã³ãã¬ã¼ãã®è§£éã«ã¤ãã¦ã®èª¬æãéä¸ã§ãã³ãã¬ã¼ãã®åå解決ã®èª¬æã«ããæ¿ãã£ã¦ãããã¨ã«ãªã£ã¦ãã¾ãã
ãã£ã¦ãæåã®æã¯ãããã³ãã¬ã¼ãã®åå解決(ãã³ãã¬ã¼ãã®è§£é)ã¯ãã§å§ã¾ãã»ããé©åã ã¨æãã
1.ããã³ãã¬ã¼ããå®ç¾©ãããæç¹ã§ã¯ãã³ãã¬ã¼ããã©ã¡ã¼ã¿ã«ä½ãä¸ããããã決å®ããã¦ããªãã®ã§ãã¨ããããããã³ãã¬ã¼ããã©ã¡ã¼ã¿ãã¨ããç¨èªã¯ãC++ARMã«è¼ã£ã¦ããªãããã¾èª¿ã¹ãããC++ Templates: The Complete Guideã«ã¯è¼ã£ã¦ãã¦ããããããC++çã«ã¯å³å¯ã«å®ç¾©ããã¦ããç¨èªã ã¨ã¯æãã®ã ãã©ã詳ããç¥ããªã人ãããvoid f(int x)ã®ãããªé¢æ°ã«ãã¦ããxã«ã©ããªå¤ãä¸ãããããã¯fãå®ç¾©ãããæç¹ã§ã¯æ±ºå®ãã¦ããªããããªãã¦çªã£è¾¼ã¿ãå ¥ããªãããã«ããããã«ããããã¯ããã³ãã¬ã¼ããã©ã¡ã¼ã¿ã¨ãã¦ã©ããªåãä¸ããããã決å®ããã¦ããªãã®ã§ãã¨æ¸ããã»ãã親åã ã¨æãã
2.ããã³ãã¬ã¼ãã«ä¾åããªãååãã¨ããã³ãã¬ã¼ããã©ã¡ã¼ã¿ã«ä¾åããååãã¨ãªã£ã¦ããããããã§ã¯2ã¤ã対çãª(å¯¾ç §çãª)é¢ä¿ã«ãªã£ã¦ããªããåè ãããã³ãã¬ã¼ããã©ã¡ã¼ã¿ã«ä¾åããªãååãã¨æ¸ãã¹ãã ã¨æããã¾ããä¾ååã®èª¬æãããã ããããªãã¨ããã®ã¯ããããä¸è¦ªåãå ·ä½çãªä¾ãå°ãã§ãæ¸ãã°ãã£ã¨ããããããã¨æããä¾ååã¨ãã¦T::resultã¨ããããã¤ãã®ä¾ç¤ºãããã°å ¨ç¶èªã¿ããããéãã¨æãã®ã ãã
3.ãã³ã¼ãã®è§£éãããã¾ãã«ãªããã®æå³ãä¸æãã³ã³ãã¤ã«ã¨ã©ã¼ã«ãªãã®ããã³ã³ãã¤ã©ã«ãã£ã¦è§£éãåãããã®ããã©ããããã¨ãè¨ãããã®ãããã§ã¯ããããªãã
4. typenameãã¼ã¯ã¼ãã®ä½¿ãæ¹ãããã§èª¬æãã¦ããã®ã«ãtypenameã¨classã¨ã®ãã¼ã¯ã¼ãã®éãããã®èª¬æã§ã¯ããããªããtypenameãã¼ã¯ã¼ãã®èª¬æããã®æ¬ã§åãã¦èª¬æãã¦ããã®ã«ããclassã¾ãã¯typenameããã©ã¡ã¼ã¿ã®åã«ä»ãã¾ãããã¨æç« ãç¶ããã®ã¯çåããä»ãã¾ããã§ãã£ããæç« ãçµããããã®ä¸ã§ãtypenameãã¼ã¯ã¼ãã®å ´åã«ã¯ãclassãã¼ã¯ã¼ãã«ã¯ãªãç¨æ³ãããã¾ãããã¨èª¬æãç¶ããã¹ãã
ããã§ãªãã¨ããtypenameã«ã¯ããã²ã¨ã¤éè¦ãªç¨éãããã¾ãããã¨æ¸ãã¦ãã£ã¦ãããããtypenameã«ãããªãç¨éãªã®ããclassãã¼ã¯ã¼ãã§ãããç¨éãªã®ããããããªããããããããã®ç´åã§æ¸ããã¦ããã®ã¯ãclassã§ãtypenameã§ãã©ã¡ãã§ãè¯ãä¾ã ãããtypenameãã¼ã¯ã¼ãã¯classãã¼ã¯ã¼ãã§ä»£æ¿å¯è½ãªç¨éã§ãã£ã¦ããã®èª¬æãè¦ãéããéè¦ãªç¨éãã§ã¯ãªããã ããããtypenameã«ã¯ããã²ã¨ã¤éè¦ãªç¨éãããã¾ããã®ãéè¦ãªç¨éãã¨ãã表ç¾ã«ã¯(ãã®èª¬æã®æµãã ã¨)éåæãããã
4ã®ä¸ç·é¨ã¯ããã®ä¾ã ããè¦ãã°typenameãã¼ã¯ã¼ãã¯classãã¼ã¯ã¼ãã§ä»£æ¿å¯è½ãªããã«è¦ãã¾ãããtypenameãã¼ã¯ã¼ãã«ã¯typenameãã¼ã¯ã¼ãã«ããåºæ¥ãªãéè¦ãªç¨æ³ãããã¾ããããããã®ã»ããé©åã
5.ãint pãã®é¨åããµã³ãã«ã®æ¸ãæ¹ããããããªãããã®ãint pãã¯ãã£ã¨å¾ã®èª¬æãèªã¾ãªãã¨æå³ä¸æã
6.ãpã¯ã°ãã¼ãã«å¤æ°ã§ããã¨ãããããã®ãµã³ãã«ãªã¹ãã§ã¯pã¯ãint pãã¨ãT::result* p;ãã®2ç®æããããã ãããpã¯ã°ãã¼ãã«å¤æ°ã§ããã¨ãã説æã¯èª¤ããã¾ããpã¯ã°ãã¼ãã«å¤æ°ã§ããã¨ãã説æã¯åå¾ã¨ä½ã®ã¤ãªãããç¡ããçªçºçã«åºã¦ãã¦ãèªè ã¯æ··ä¹±ãããä½ã説æãããã®ããããããããããªãã
7.ãå¼æ°ã«struct Xãå¼ã渡ãããã¨ãã¯æå³ã¯ä¼ãããããã表ç¾ãããããæ°ãããããXåã®å¤æ°xã«å¯¾ãã¦ãg(x)ã®ããã«ãã®templateãå®ä½åããã¨ããããã§ã¯ã©ãã ãããã
8.ãpã¯X::result(=int)ã¸ã®ãã¤ã³ã¿ã¨ãªãã¾ããã¨ãããã6.ã§ãpã¯ã°ãã¼ãã«å¤æ°ã§ããã¨æ¸ããç´å¾ãªã®ã«ãçªç¶ãã¼ã«ã«å¤æ°ã®ã»ãã®pã«ã¤ãã¦è¨åãã¦ããããã®ããã«ä¸ã¤ã®ã·ã³ãã«ãè¨èãç°ãªãæå³ã§ä½¿ãã®ã¯ãªãã¹ãé¿ããã»ããè¯ãã¨æãã
9. ãstruct Y {â¦};ãã¯Yã®å®ç¾©ã§ãã£ã¦ãããã10.ã«ããããã«é¢æ°gã«å¼ã渡ããã¨ã¯åºæ¥ãªãã6.ã®ã¨ããã§ã表ç¾ã«éåæããã£ãããããã¯æããã«ãããããããã¦ã6.ã®è¡¨ç¾ã«å£ãããstruct Y {â¦};ã®ããã«å®ç¾©ãããYãé¢æ°g()ã«å¼ã渡ãããã¨ããã¨æ¸ãã¹ãã§ããã
11.ãã¾ã£ããç°ãªãå®ç¾©ã¨ãªããã¨ããããä½ãå ¨ãç°ãªããå®ç¾©ãã«ãªãã®ãï¼é¢æ°gã®å®ç¾©ã¯ãå°ãªãã¨ãX,Yã®ã©ã¡ããä¸ããããããå¤ãããã¨ã¯ç¡ãããããå¤ãã£ããå®ç¾©ã§ã¯ãªããããã¯ãã¾ã£ããç°ãªãæå³ã«ãªããã¨æ¸ãã»ããé©åããã ãããã§ããtemplateã§æ¸ããã¦ããã®ã ããä¸ããããåã«å¿ãã¦å®ä½åãããããããã®æå³(å®éã«çæãããã³ã¼ã)ãç°ãªãã®ã¯å½ããåã ããããã¯ãä½ã表ç¾ãããã®ããã¾ãæ確ã«ã¯ãªããªãããã®é¨åã¯å ¨ä½çã«æ¸ãç´ããã»ããè¯ãã¨æãã
12.ãããããã³ã³ãã¤ã«ã¨ã©ã¼ã¨ã¯ãªãã¾ãããã®ã¨ããã«ãã¦ãã³ã³ãã¤ã«ã¨ã©ã¼ã¨ãªããªãã®ã¯ãpãã°ãã¼ãã«å¤æ°ã¨ãã¦å®£è¨ãã¦ããããã§ãã5*pãã®pãã°ãã¼ãã«å¤æ°ã®pã¨è§£éãããããã§ããã¨ãããã®ã ãããã®èª¬æã§ã¯ããããã¡ãã¨ä¼ãããªããã6.ã®ãpã¯ã°ãã¼ãã«å¤æ°ã§ããã¯ãããã«å ¥ããã¹ã説æã ã¨æãã
ã¾ãããã®ãããªã°ãã¼ãã«å¤æ°ãã³ã³ãã¤ã«ã¨ã©ã¼ã¨ãªããªãããã«ããããã ãã«ãµã³ãã«ã³ã¼ãä¸ã§æå³ãªãå®ç¾©ãã¦ãããã§ãã³ã³ãã¤ã«ã¨ã©ã¼ã¨ã¯ãªãã¾ãããã¨èª¬æãããããã¯ããããªã°ãã¼ãã«å¤æ°ã¯æåãã宣è¨ããã«ãã5*pã¨è§£éããããã®çµæãpã¨ããå¤æ°ã宣è¨ããã¦ããªãã®ã§ãã³ã³ãã¤ã«ã¨ã©ã¼ã«ãªãã¾ãã(ãã ããpã¨ããå¤æ°ãã°ãã¼ãã«å¤æ°ã¨ãã¦å®£è¨ããã¦ããå ´åãªã©ã¯ãã³ã³ãã¤ã«ã¨ã©ã¼ã«ãªãã¾ããã)ãã¨èª¬æããã»ããèªç¶ã ã¨æãã
ãã¨ããY::result* p;ãã¨ãã宣è¨ãã³ã³ãã¤ã«ã¨ã©ã¼ã¨ãªããªãã®ã¯ãç§ã¯ããã¯ç¥ããªããVC++ã®ç¬èªæ¡å¼µã§ã¯ãªããããããtypename Y::result* p;ãã§ããã¹ããªã®ã§ã¯ãªããããããã®ã¸ãã説æãããã«ãã³ã³ãã¤ã«ã¨ã©ã¼ã¨ã¯ãªãã¾ãããã¯ãã¾ãããªãã ãããã
13.ãT::resultã¯Tã®ä¾ååã§ããããã³ãã¬ã¼ãã®å®ç¾©æç¹ã§ã¯ããã¾ããªã®ã§ããã¨ããããä½ããããã¾ããã ã¨è¨ãããã®ãããããªããããã³ãã¬ã¼ãã®å®ç¾©æç¹ã§ã¯T::resultãåãªã®ãæ°å¤ãªã®ããææ§ãã¨æ¸ããã»ããç´ããå°ãªãã
14.ãä¾ååããã³ãã¬ã¼ãã®å®ç¾©æç¹ã§è§£æ±ºããããã«ãã¨ãããã2.ã®èª¬æã§ãä¾ååã®è§£æ±ºã¯ã¤ã³ã¹ã¿ã³ã¹åæã¾ã§é 延ããã¾ããã¨æ¸ãã¦ããã®ã ãããå®ç¾©æç¹ã§ä¾ååã解決ãããã®ãæ¬å½ã ã¨ãããããã®2.ã®èª¬æã¨è«ççã«çç¾ããã
ããããtemplateã®å®ç¾©æç¹ã§ã¯ãtemplateã®é¢æ°ã®ä¸èº«ã¯ã³ã³ãã¤ã©çã«ã¯ãã¼ãã§ãã¯ã§ãã£ã¦ãC++ã®è¦ç´ä¸ã¯è¨±ãããã¯ãã§ãtypenameãã¼ã¯ã¼ããã¤ããããã¨è¨ã£ã¦ãä¾ååããã³ãã¬ã¼ãã®å®ç¾©æç¹ã§è§£æ±ºããã¦ããã¨è¨ãã®ã¯èª¤ãã ãããC++ã®ç¨èªã§ãååã®è§£æ±ºãã¨ããã®ãã©ããããå³å¯ã«å®ãããã¦ããã®ãã¯ç§ã¯ç¥ããªããã©ãããä¾ååã®è§£æ±ºãã¨ããã®ããtwo phase lookupã®æ ¸ã¨ãªãæ¦å¿µãªã®ã ããã2.ã®ããã«èª¬æãªãã«æ¸ãã®ã¯ã¾ããããã¾ãã¦ã2.ã¨14.ã¨ã§èª¬æãçç¾ããã®ã¯è«å¤ã ã¨æãã
ã¨ã¾ãããã£ã2ãã¼ã¸ã§ã¯ãããã©ããç§ãè¦ã¦æ°ã«ãªãé¨åã¯çµæ§ããããã ããç¹°ãè¿ãã«ãªãããä¸ã®ç§ã®æè¦ããã¹ã¦æ£ããã¨ã¯éããªãããããã風ã«ã¯æããªãã§ããã ããããæ»èª(ããã¯ã¬ãã¥ã¼ã¨è¨ãã¹ãã)ã¨ã¯ãã®ããã«èªåã®æè¦ãèè ã«ã¶ã¤ãã¦ããä½æ¥ã§ããããã®çµæãèªã¿ããããããããããæ¬ãåºæ¥ãã®ã§ããã
ç§ãã³ã³ãã¥ã¼ã¿æ¸ç±ã§ããã®äººã®æ¬ãªãééããªãå®å¿ãã¦èªããã¨æãã®ã¯ãçµå浩ããã®æ¬ãããã§ããã®äººã®æ¸ãæ¬ã®èªã¿ãããã¯ç°å¸¸ã ã¨æããã©ãã»ã©å³ããèªåã®å稿ãè¦ã¤ãããããã ãããããããæ¬ãåºæ¥ãã®ããç§ã«ã¯ãã¾ã æ³åããã¤ããªãã
ã¨ããã§ãåé ã«ãæ¸ããããã«æ»èªã¯ååçã«ãã¼ã®ã£ã©ã§ããã¦ãããã¾ããä»äºãå¿ããææã¯å¼ãåãããã¾ããããç§ã«è¦ã¦æ¬²ããã¨ãã人ã¯ã¡ã¼ã«ãã ããã(ãã¾ãã«ãééããå¤ãããªå ´åã¯ãå®éã«ç§ã®äºåæã¾ã§æ¥ã¦ããã ãã¦ãã®å ´ã§ãªããã¦ããã ãã¨ããå½¢ã«ãªãã¾ããããã®è¦æãããã°æè¿ãã¾ãã)
ãã¨ç£ä¿®ãããã¦ãããã¾ããç£ä¿®ã¯ã®ã£ã©ãããã ããã¨ã«ãã¦ã¾ãã翻訳æ¬ã®ç£ä¿®ãªããããããã¦ãããã¾ãããããç§ã«é ¼ã¿ãã人ã¯ã¡ã¼ã«ãã ãããããã¾ããç§ã®ä»äºãå¿ããææã¯å¼ãåãããã¾ãããã