ãããã°ã©ãã³ã°èšèªã§æãéèŠãªã®ã¯ãã®ååã§ãã èšèªã¯è¯ãååããªããã°æåããŸããã ç§ã¯æè¿éåžžã«è¯ãååãæãã€ããŸããã ä»ã§ã¯é©åãªèšèªãçºæããããšãæ®ã£ãŠããŸããã D. E.ã¯ããã
C ++ 09 EvolutionïŒãã©ãã·ã¥ããã¯
1998幎ã«æ¹åãããæåã®C ++ä»æ§ã®åŸã5幎éã®äŒæ©ãåãããã³ã³ãã€ã©éçºè ãæšæºã«é©å¿ã§ããããã«ãªããŸããã ãŸããä»åã®ãç¡ç·æ²é»ãã«ãããå§å¡äŒã¯ææžã«é¢ãããã£ãŒãããã¯ãåãåãããšãã§ããŸããã ãã®æéã®çµããã«ãANSIæšæºå§å¡äŒã¯ãä¿®æ£ãšããŸããŸãªæ¹åãå«ãæŽæ°ãããä»æ§ããªãªãŒã¹ããŸããã ãããã®ä¿®æ£ã¯ã2003幎ã®æåã®æè¡çãšã©ãŒãªã¹ãã«èšèŒãããŠããŸãã
ããã«ãå§å¡äŒã®ã¡ã³ããŒã¯C ++ã®å€æŽã«é¢ããææ¡ãåãå ¥ãå§ããŸããã ãã®ã€ãã·ã¢ããã¯C ++ 0xãšåŒã°ãã21äžçŽã®æåã®10幎ã§æ°ããããŒãžã§ã³ã®èšèªãæ¿èªãããããšãäºæ³ãããŠããŸããã ããããæéãçµã€ã«ã€ããŠæããã«ãªããŸãããæ°ããããŒãžã§ã³ã®èšèªã¯2009幎ãããæ©ãæ¹åã§ããªãã£ãããããã®ã€ãã·ã¢ããã¯ãã®ååãC ++ 09ã«å€æŽããŸããã
ã©ã€ãã©ãªæ¡åŒµã«é¢ããããã¥ã¡ã³ãã®éçºã¯2004幎ã«éå§ããã2005幎1æã«å®äºããŸããïŒTR1ãšåŒã°ããŠããŸããïŒã æšæºC ++ã©ã€ãã©ãªã«ããã€ãã®æ¡åŒµæ©èœãå°å ¥ããããšãæšå¥šããŸãããããã®å€ãã¯Boostãã¬ãŒã ã¯ãŒã¯ããã®ãã®ã§ãã 2006幎4æãå§å¡äŒã¯ãã¹ãŠã®TR1å§åãæ¡çšããŸããïŒã³ã³ãã€ã©éçºè ãå®è£ ããã®ãå°é£ãªé«ã¬ãã«ã®æ°åŠã©ã€ãã©ãªãé€ããŸãïŒã GCC 4.0ã¯ãã»ãšãã©ã®TR1ãstd :: tr1 ::åå空éã«æ¢ã«å®è£ ããŠããŸãã GCCã«å ããŠãMetrowerks CodeWarrior 9ããã³10ããããå®è¡ããMicrosoftã¯Visual C ++ 2008 Feature PackããªãªãŒã¹ããTR1ãå®è£ ããŠããŸãïŒ waskerã®ãããã§ã ïŒã
æšæºåéçºå§å¡äŒã¯ã2007幎æ«ãŸã§ã«C ++ 09ãå®äºããäºå®ã§ãã åã幎ã«ã4æãš10æã«2ã€ã®äŒè°ãèšç»ãããŠããŸãã é害ããªããã°ãããã¥ã¡ã³ãã®æçµçã¯2008幎ã«å©çšå¯èœã«ãªãã2009幎ã®ãã€ãã§æ¹åãè¡ãããŸãã
å²åŠC ++ 09
çŽ10幎åã«C ++ 98ãæ¹åããåŸãæšæºåå§å¡äŒã¯èšèªã«å€§ããªå€æŽãå ããããšã«é¢å¿ããããŸããã§ããããåå¿è ãåŠç¿ãããããããã¢ã¯ã»ã¹ãããããã®ã«ããå€æŽããµããŒãããŸããã å€ãã®ããã°ã©ããŒã¯ãç¹å®ã®ããã°ã©ãã³ã°èšèªã®å°é家ã«ãªããããããŸããã ããã©ãããã圌ãã¯åœŒãã®åéã®å°é家ã«ãªããããšæã£ãŠãããåã«ã¿ã¹ã¯ãå®çŸããæ段ãšããŠC ++ã䜿çšããŠããŸãã 匷åãªæ°ããããŒã«ãèšèªã«è¿œå ãããŸãããããã®äž»ãªç®æšã¯äŸç¶ãšããŠèšèªãç°¡çŽ åããããšã§ãã
å¥ã®ç®æšã¯ãèšèªèªäœã®ã³ã¢ãå€æŽããåã«æšæºã©ã€ãã©ãªãç°¡åã«æŽæ°ããããšã§ãã ã«ãŒãã«ã®å€æŽã¯éåžžã«å±éºã§ããã倧ããªäºææ§ã®åé¡ã«ã€ãªããå¯èœæ§ããããŸãã ããã©ããããã©ã€ãã©ãªã®æ¹åã«ãããããå°ãªããªã¹ã¯ã§åªããæè»æ§ãå®çŸã§ããŸãã ããšãã°ãã¬ããŒãžã³ã¬ã¯ã¿ãŒã®å®è£ ãèããŠã¿ãŸãããïŒã»ã«ãã¯ãªãŒãã³ã°çšã®èšèªã®ã«ãŒãã«ãå€æŽãããšïŒJavaããã³CïŒã§è¡ãããããã«ïŒãããæ·±å»ãªå€æŽãçºçããäžäœäºææ§ãããã«å¿ èŠã«ãªããŸãããŸããæšæºã©ã€ãã©ãªã®ã¹ããŒããã€ã³ã¿ãŒã¯ã©ã¹ã®ãµããŒãã«ãããäœã³ã¹ãã§ã®æ©äŒã
æåŸã«ãå§å¡äŒã¯å¯èœãªéãçã®çç£æ§ãåäžãããããåªããŸããã C ++ã®åŒ·ã¿ã®1ã€ã¯ããã®ããã©ãŒãã³ã¹ïŒæ¯èŒçæ°ããCïŒãšJavaïŒã§ãã ãã®ãããå€ãã®ããã°ã©ããŒãC ++ãäž»èŠãªããã°ã©ãã³ã°èšèªãšããŠéžæããŠããŸãã IDCã«ãããšã2003幎ã«ã¯çŽ300äžäººã®C ++ããã°ã©ããŒããããããèšèªãããã§ãªããã®ã«å€ããããšããã®ã§ã¯ãªããå©äŸ¿æ§ã®ããã«èšèªãæ¹åããããšã¯çã«ããªã£ãŠããŸãã
EC ++ã®æçãªç©èª
1999幎ãæ¥æ¬ã®çµã¿èŸŒã¿éçºè ã®ã³ââã³ãœãŒã·ã¢ã ïŒNECãæ¥ç«ãå¯å£«éãæ±èãå«ãïŒããC ++ã®ãµãã»ããã匷調ããææ¡ãæåºããŸããã ãã®ãµãã»ããã¯ãã³ã³ãœãŒã·ã¢ã ã®ã¡ã³ããŒã«ãããšéåžžã«è€éã§ããã©ãŒãã³ã¹ã«å€§ããªãã¡ãŒãžãäžããç¹å®ã®æ°ã®èšèªæ©èœãåé€ããããšãé€ããŠãC ++ãã»ãŒç¹°ãè¿ããŸãã åé€ããå¿ èŠãããèšèªã®äž»èŠã³ã³ããŒãã³ãïŒå€éç¶æ¿ããã¿ãŒã³ãäŸå€ãRTTIãæ°ãããã£ã¹ãæŒç®åãåå空éã ãã®ãããªèšèªã®ãµãã»ããã¯ãEmbedded C ++ïŒãŸãã¯åã«EC ++ïŒãšåŒã°ããŸãã
ã³ã³ãœãŒã·ã¢ã ã®é©ããããšã«ãEC ++ã³ã³ãã€ã©ã¯C ++ã³ã³ãã€ã©ãããé«éã§ã¯ãããŸããã§ããã ããã©ããããå Žåã«ãã£ãŠã¯ã¯ããã«é ããªããŸããã C ++ã®åµèšè ã§ããBjarn Straustrupã¯ããã³ãã¬ãŒããã»ãšãã©ã®æšæºã©ã€ãã©ãªã§äœ¿çšãããŠããããããã®åé€ã¯å®å šã«éå®çšçã§ãããšèª¬æããŸããã åæã«ãã³ã³ãœãŒã·ã¢ã ã¯ããã³ãã¬ãŒãããµããŒãããæ¡åŒµïŒæ¡åŒµïŒEC ++ã®åºçŸãå¯èœã§ãããšçºè¡šããŸããã
æ¡åŒµEC ++ã³ã³ãã€ã©ãŒãå©çšå¯èœã«ãªã£ããšãããããã¯åã³ãã倧ããªåçç©ãšäžèŽããŸããã ã³ã³ãœãŒã·ã¢ã ã®é©ããããšã«ãC ++ãšæ¯èŒããããã©ãŒãã³ã¹ã®åäžã¯ãããããã§ããã åé¡ã®äžéšã¯ãã³ã³ãœãŒã·ã¢ã ãC ++ã®ååã䜿çšããŠããªããã®ã«ãéãæãå¿ èŠããªãããç¡èŠããããšã§ãã ãã®åŸãISOã¯EC ++ã«é¢ããææ¡ãåãå ¥ããããšãæåŠããŸããã
2004幎ãEC ++ã®å€§å€±æã«è§ŠçºãããC ++ 0xå§å¡äŒã¯ãã©ã®C ++æ©èœã«å®éã«å€§ããªããã©ãŒãã³ã¹ã®åé¡ãããããå€æããããšããŸããã çµå±ã®ãšãããçç£æ§ãæ¬åœã«é«ããããšãã§ããã®ã¯æ¬¡ã®3ã€ã®é åã ãã§ãã
æ°èŠããã³åé€ RTTIïŒtypeidïŒïŒããã³dynamic_cast <>ïŒ äŸå€ïŒã¹ããŒããã³ãã£ããïŒ
ã¡ã¢ãªå²ãåœãŠã¯ããã©ãŒãã³ã¹ã«æã倧ããªåœ±é¿ãäžããããšãå€æããŸããããããŒãããã¡ã¢ãªãå²ãåœãŠãªãèšèªã䜿çšããããšã¯ã»ãšãã©ãããŸããã RTTIãšäŸå€åŠçã«é¢ããŠãå€ãã®ã³ã³ãã€ã©ã«ã¯ãããããªãã«ããã¹ã€ããããããŸãã ææ°ã®ã³ã³ãã€ã©ã§ã¯ãäŸå€åŠçã¯ããªãé«ãã¬ãã«ã§å®è£ ãããŠãããåé¡ã¯RTTIã®ã¿ã«ãããŸãã ãããã«ãããC ++ã®ååã«åŸãå Žåãèšèªã®äžéšã®æ©èœãåé€ããããšã¯ãããããç¡å¹ã«ããããšã«å¹æµããŸãã
EC ++ã«é¢ããŠãStraustrupã¯æ¬¡ã®ããã«è¿°ã¹ãŠããŸãã ãç§ã®æèŠã§ã¯ãEC ++ã¯æ»ãã§ããŸãããããã§ãªããã°æ»ãã§ããã¯ãã§ãã ã
ä¿®æ£ãšæ¹å
1998幎ã®C ++æšæºã¯ããèªäœãç®èŠããææã§ãã£ãã«ãããããããããã€ãã®åé¡ããããŸããã ãã£ãããå°é£ãªãã®ãããã°ãæ¢ç¥ã®åé¡ããããŸããããå€ãã®å Žåãæ°ãã解決çãäœæããã«ã¯äžååã§ããã Bjarn Straustrupããããã®ããã€ãã説æããŸãããäŸãã°ïŒ
ãã¯ãã«<ãã¯ãã«<int >> xv; //å¯èœã§ãïŒ vector <double> xv {1.2ã2.3ã3.4}; // STLã³ã³ãããåæåããŸã åæåã®ãã匷åãªå//åæåã¯ã¹ã³ãŒãå ã«æ®ããŸã ãã³ãã¬ãŒãã®å€éšå//翻蚳ãŠãããéã§ã®éè€ãªã
äžèšã®ãšã©ãŒãçºçããçç±ãããããªãå Žåã¯ãçºçã®çç±ãç解ããããšããªãããšããå§ãããŸãã æåã®ãšã©ãŒã®ã¿ãæ€èšããŸãã æ¬ ç¹ã¯ãC ++ 98ããã¯ãã«ã®ã>>ãéšåãå³ã·ããæŒç®åãšããŠè§£æãããšã©ãŒãçæããããšã§ãã C ++ 09ã§ã¯ããã®ãšã©ãŒã¯ä¿®æ£ãããŸãã 以äžã«å°ããªäŸã瀺ããŸãã
ãã³ãã¬ãŒã<int I> struct myX { static int const x = 2; } ãã³ãã¬ãŒã<> struct myX <0> { typedef int x; } ãã³ãã¬ãŒã<typename T> struct myY { static int const x = 3; } static int const x = 4; cout <<ïŒmyY <myX <1 >> :: x> :: x> :: xïŒ<< endl; // C ++ 98ã¯ã3ããåºåããC ++ 09-ã2ããåºåããŸã
ANSI / ISO C99ãšã®åæ
C ++ã®æ¹åãã1幎åŸãANSI / ISO Cä»æ§ã¯Cèšèªã«ããã€ãã®å€æŽãå ããŠæŽæ°ããããããã®å€æŽã®å€ãã¯æ¢ã«C ++ã§è¡ãããŠããŸãããCã§ãæå³ããããŸãã å¯Ÿç §çã«ãä»ã®ãã®ã¯C ++ã®äžéšã§ã¯ãããŸããã§ããããå§å¡äŒã¯ãããã䟡å€ããããšèŠãªããC ++ 09ä»æ§ã«åæ ããããšããŸããã ãããã«ã¯ä»¥äžãå«ãŸããŸãã
__func__ //é 眮ãããŠããé¢æ°ã®ååãè¿ããŸã long long // 64ãããã®æ°å€ã«äžè¬çã«äœ¿çšãããæ¡åŒµçµã¿èŸŒã¿å int16_tãint32_tãinttpr_tãªã© //ç¹å®ã®çš®é¡ã®æ°å€ double x = 0x1.F0 // 16é²æµ®åå°æ°ç¹æ° ããã€ãã®æ°åŠé¢æ°ã®è€éãªããŒãžã§ã³ åºå®æ°ã®åŒæ°ãåããã¯ã
C ++æšæºã©ã€ãã©ãªã®æ©èœåŒ·å
æšæºC ++ã©ã€ãã©ãªïŒSTLãå«ãïŒã¯ãå€æ°ã®äŸ¿å©ãªã³ã³ãããšãŠãŒãã£ãªãã£ã§ãã ãã®æ©èœã¯å å®ããŠããã«ããããããããŠãŒã¶ãŒã«ãšã£ãŠéåžžã«å¿ èŠãªå€ãã®ã³ã³ããŒãã³ãããããŸããã C ++ 09ã¯ããããã®ã¹ããŒã¹ã次ã®æ°ããã©ã€ãã©ãªã¯ã©ã¹ã§åããŸãã
regexïŒãã¹ãŠãæåŸ ããæ£èŠè¡šçŸã¯ã©ã¹ é å<>ïŒç¬èªã®ãµã€ãºãå«ã1次å é åïŒ0ã®å ŽåããããŸãïŒ tuple <>ïŒtuple-tupleãã³ãã¬ãŒãã¯ã©ã¹ STLããã·ã¥ã³ã³ãããŒã¯ã©ã¹ïŒunordered_set <>ãunordered_map <>
GCC 4ïŒXcode 2.xïŒã䜿çšããéçºè ã¯ãstd :: tr1 ::ãä»ããŠåæ§ã®æ¡åŒµæ©èœã䜿çšã§ãããããæšæºã©ã€ãã©ãªã®ãã®ãããªå€æŽã2009幎ãŸã§åŸ ã€ããšã¯ã§ããŸããã
ã¹ããªãŒã ã®æ¹å
ããŒã«ã«ã¹ãã¬ãŒãžïŒ
ã¹ã¬ããint x = 1; //ã¹ã¬ããå ã§ã°ããŒãã«ã«
ååæäœïŒ
ã¢ããã㯠{ //å®è¡æã«ä»ã®ã¹ã¬ãããäžæããŸã }
䞊åå®è¡ïŒ
ã¢ã¯ãã£ã { {...} //æåã®äžŠåããã㯠{...} // 2çªç®ã®äžŠåããã㯠{...} // 3çªç®ã®äžŠåããã㯠}
䞊åå®è¡ã®å Žåãã³ã³ãã€ã©ããã®å Žæã§äžŠåãããã¯ã¯åçæ§ããªããšèããå Žåãã³ã³ãã€ã©ã¯ããããåã«é£ç¶ããŠå®è¡ããå¯èœæ§ããããŸãã
ãã®ãããªèšèªã®æ©èœã¯ãpthreadãmutexãªã©ã䜿çšããŠå®è¡ãããéçºãå€§å¹ ã«ç°¡çŽ åããããšã¯æããã§ãã åè¿°ã®ç¹æ§ã¯ãC ++ 09å§å¡äŒã®ã¡ã³ããŒã«ãã£ãŠãŸã è°è«ãããŠãããããè¥å¹²ã®å€æŽãè¡ãããå¯èœæ§ãããããšã«æ³šæããŠãã ããã
ãã®ããã¥ã¡ã³ãã§ãã®ãããªæ¹è¯ã«é¢ãã詳现ãèŠã€ããããšãã§ããŸã ã
ããŸããŸãªæ°ã®åŒæ°ãæã€ãã³ãã¬ãŒã
é·å¹Žã®éãCèšèªã§ã¯ãé¢æ°ãåºå®ãããŠããªãæ°ã®ãã©ã¡ãŒã¿ãŒãæã€ããšãã§ããŸããã æ®å¿µãªãããããã¯C ++ 98ã§ã¯äžå¯èœã§ããã C ++ 09ã§ã¯ããã³ãã¬ãŒãã«ããŸããŸãªã¿ã€ãã®åãå«ããããšãã§ããŸãã æãç°¡åãªäŸã次ã«ç€ºããŸãã
// DEBUGãã©ã°ãèšå®ãããŠããå Žåã«ã®ã¿stderrã«åºåããŸã ãã³ãã¬ãŒã<typename TypeArgs> void DebugMessageïŒTypeArgs ... argsïŒ { #ifdef DEBUG // stderrã®ãšã³ããªã®å®è£ #else //äœãããŸãã #endif } //ããã«ã³ãŒã㧠DebugMessageïŒ "n is"ãnïŒ; DebugMessageïŒ "x is"ãxã "y is"ãyã "z is"ãzïŒ; DebugMessageïŒ "ããã¯ç§ã®ãã¬ãŒã¹ïŒ"ã "time ="ãã¯ããã¯ïŒïŒã ããã¡ã€ã«å=ãã__ FILE__ã ãè¡çªå·=ããã__ LINE__ãã ãå éšé¢æ°ïŒãã__ func__ïŒ;
ã³ã³ã¹ãã©ã¯ã¿ãŒã®å§ä»»
CïŒãªã©ã®ä»ã®èšèªã§ã¯ã1ã€ã®ã¯ã©ã¹ã³ã³ã¹ãã©ã¯ã¿ãŒãå¥ã®ã¯ã©ã¹ã³ã³ã¹ãã©ã¯ã¿ãŒãåŒã³åºãããšãã§ããŸãã C ++ 98ã§ã¯ããã¯äžå¯èœã§ãã£ããããã¯ã©ã¹éçºè ã¯å¥ã®åæåé¢æ°ãäœæããå¿ èŠããããŸããã C ++ 09ã§ã¯ã次ã®ã³ãŒãã«ç€ºãããã«ããããå¯èœã«ãªããŸãã
ã¯ã©ã¹MyClass { å ¬éïŒ MyClassïŒïŒ; //ããã©ã«ãã®ã³ã³ã¹ãã©ã¯ã¿ãŒ MyClassïŒvoid * myptrïŒ; //ãã€ã³ã¿ãååŸããŸã MyClassïŒint myvalueïŒ; //æ°å€ãååŸããŸã }; MyClass :: MyClassïŒïŒïŒMyClassïŒNULLïŒ// XãåŒã³åºãïŒvoid *ïŒ { ... //ã³ãŒã } MyClass :: MyClassïŒvoid * myptrïŒïŒMyClassïŒ0ïŒ//åŒã³åºãXïŒintïŒ { ... //ã³ãŒã } MyClass :: MyClassïŒint myvalueïŒ//å§ä»»ãããŠããŸãã { ... //ã³ãŒã }
ãã«ãã€ã³ã¿ãŒ
ANSIã§ã¯ãC NULLã¯ïŒvoid *ïŒ0ãšããŠå®çŸ©ãããŸããC++ã§ã¯ãNULLã¯æšå¥šãããŸããã ãªãã§ïŒ Cãšã¯ç°ãªããC ++ã§ã¯ãä»ã®åã®ãã€ã³ã¿ãŒã«voidãã€ã³ã¿ãŒãå²ãåœãŠãã®ã¯ééã£ãŠããŸãã
void * vPtr = NULL; // CãšC ++ã®äž¡æ¹ã§ä¿®æ£ int * viPtr = NULL; // Cã§ã¯æ£ããããC ++ã§ã¯ééã£ãŠãã // C ++ã§ã¯int *ã«void *ãæ·»ä»ã§ããŸããïŒ int * viPtr = 0; //å³C ++
ãã ããC ++ã³ãŒãã§ã®NULLã®ååžã¯éåžžã«å€§ãããããå€ãã®ã³ã³ãã€ã©ã¯ããã®ãããªå²ãåœãŠãçºçãããšèŠåïŒãšã©ãŒã§ã¯ãªãïŒãçæããã ãã§ãã C ++ã§NULLã0ãšããŠåå®çŸ©ãããã£ã¹ããšã©ãŒã®çºçãé²ããã®ããããŸãã ã³ã³ãã€ã©ã®ãã¹ãŠã®ã瀌åãã«ãããããããããã¯ãã¹ãŠåå¿è ã®C ++ããã°ã©ãã倧ããæ··ä¹±ãããŸãã
ãã€ãããŒïŒintïŒ; //æŽæ°å€ãååŸããŸã ãã€ãããŒïŒchar *ïŒ; // charãååŸããŸã* ããŒïŒ0ïŒ; //ãã€ã³ã¿ã§ããããããšãåãªãæ°åã§ããïŒ ããŒïŒNULLïŒ; //察å¿ãããããã¿ã€ãã¯ãããŸãã
ãããã£ãŠãnullãã€ã³ã¿ãŒã®äœ¿çšãç°¡çŽ åããããã«ãnullptrãC ++ 09ã§å°å ¥ãããŸããã ä»»æã®åã®ãã€ã³ã¿ãŒã§äœ¿çšã§ããŸãããçµã¿èŸŒã¿åã«ã¯é©çšã§ããŸããã
char * cPtr1 = nullptr; // C ++ã®NULLãã€ã³ã¿ãŒ char * vcPtr2 = 0; // Trueããã ãæšå¥šãããŸãã int n = nullptr; //ç¡å¹ã§ã myX * xPtr = nullptr; //ä»»æã®ã¿ã€ãã®ãã€ã³ã¿ãŒã§äœ¿çšã§ããŸã ãã€ãããŒïŒintïŒ; //æŽæ°å€ãååŸããŸã ãã€ãããŒïŒchar *ïŒ; // charãååŸããŸã* ããŒïŒ0ïŒ; // fooïŒintïŒãåŒã³åºããŸã ããŒïŒnullptrïŒ; // fooãåŒã³åºããŸãïŒchar *ïŒ
ã©ãã«è¡ã£ãïŒ
Cãã¡ããã©éçºäžã§ãã£ããšããautoããŒã¯ãŒãã䜿çšããŠãã³ã³ãã€ã©ãŒã«ã¹ã¿ãã¯äžã®å€æ°ã®å Žæãéç¥ããŸãããäŸãã°ïŒ
auto x; / * xïŒæŽæ°ïŒãšããååã®å€æ°ãã¹ã¿ãã¯ã«ãããŸã* /
ANSI Cã1989幎ã«æ¹åããããšããåå®çŸ©ã¯åé€ãããŸããã
auto x; / * ANSI Cã§ã¯ç¡å¹* / int x; / * True * / auto int x; / *確ãã«ãçå®ã¯äžå¿ èŠã§ã* /
ãã以æ¥ã1970幎代ããautoã䜿çšãã人ã¯ã»ãšãã©ããŸããã§ããããautoã¯CïŒããã³C ++ã§ïŒã®ããŒã¯ãŒãã«ãªããŸããã 30幎åŸãC ++ 09æšæºã¯autoããŒã¯ãŒããåå°å ¥ããŸãã ãã®ããŒã¯ãŒãã§å®çŸ©ãããå€æ°ã¯ãåæåæã«åãèªåçã«ååŸããŸãã
auto y = 10.0; // yã¯æµ®åå°æ°ç¹æ°ã§ã auto z = 10LL; // zã¯long long const auto * p =ïŒy; // pã¯const double *
åçæ§ã¯ã次ã®ãããªè€éãªãã¥ãŒãšçµã¿åãããŠäœ¿çšââãããšãããæ確ã«ãªããŸãã
void * barïŒconst int doubleArray [64] [16]ïŒ; auto myFcnPtr = bar; // myFcnPtrã®ã¿ã€ãããvoid *ïŒconst intïŒ*ïŒ[16]ïŒãã«ãªããŸãã
ãã®äžãautoã¯ãåãããã»ã©éèŠã§ã¯ãªãäžæå€æ°ã«å¯ŸããŠéåžžã«äŸ¿å©ã«ãªããŸãã STLã³ã³ããã®èŠçŽ ãééãã次ã®é¢æ°ãæ€èšããŠãã ããã
ãã€ãããŒïŒãã¯ã¿ãŒ<MySpace :: MyClass *> xïŒ { forïŒauto ptr = x.beginïŒïŒ; ptrïŒ= x.endïŒïŒ; ptr ++ïŒ { ... //ããŸããŸãªçš®é¡ã®ã³ãŒã } }
autoããŒã¯ãŒãããªãå Žåãptrå€æ°ã®åã¯vector <MySpace :: MyClass *> :: iteratorã«ãªããŸãã ããã«ããã®ã³ã³ãããŒã®å€æŽïŒããšãã°ããã¯ã¿ãŒ<>ãããªã¹ã<>ãžã®å€æŽãã¯ã©ã¹åãåå空éåã®å€æŽãªã©ïŒã¯ãã«ãŒãå ã§ãã®ã¿ã€ãã絶察ã«éèŠã§ã¯ãªããšããäºå®ã«ãããããããããã°ã©ããŒã«ç¢ºå®ã«ptrå€æ°ã®å®çŸ©ãå€æŽãããŸãã
èå³æ·±ãããšã«ãCïŒã§ã¯ãvarããŒã¯ãŒãã䜿çšããŠåæ§ã®åäœãäœæãããŸãã
C ++ 09ã§autoã䜿çšããã«ã¯ããŸã åæåãå¿ èŠã§ããããšã«æ³šæããŠãã ããã
auto x; // C ++ 09ã§ç¡å¹
ããããïŒå¥ã®å€æ°ã«åºã¥ããŠïŒå¿ èŠãªåãç¥ã£ãŠããããåæåããããªãã£ããšããŸããïŒ æ°ããdecltypeããŒã¯ãŒãã¯ã次ã®ã³ãŒãã¹ãããããªã©ã®ç®çã§äœ¿çšã§ããŸãã
bool SelectionSortïŒäºéããŒã¿[256]ãäºé蚱容å€ïŒ; bool BubbleSortïŒäºéããŒã¿[256]ãäºé蚱容å€ïŒ; bool QuikSortïŒäºéããŒã¿[256]ãäºé蚱容å€ïŒ; decltypeïŒSelectionSortïŒmySortFcn; ifïŒbUseSelectionSortïŒ mySortFcn = SelectionSort; else ifïŒbUseBubbleSortïŒ mySortFcn = BubbleSort; ä»ã« mySortFcn = QuikSort;
ã¹ããŒããã€ã³ã¿ãŒ
ã¹ããŒããã€ã³ã¿ãŒã¯ãããã°ã©ããŒã«äŸåããã«ãã¡ã¢ãªããèªåèªèº«ãåé€ããå¿ èŠãããæéãèªèº«ã§ç解ã§ãããªããžã§ã¯ãã§ãã JavaãCïŒãªã©ã®ã»ãšãã©ãã¹ãŠã®ææ°èšèªã¯ãäžå¿ èŠãªã¡ã¢ãªãªãŒã¯ãåé¿ããåæ§ã®ã¢ãã«ã«åŸã£ãŠã¡ã¢ãªã管çããŸãã C ++ 98ã§ã¯ãåæ§ã®å°ããªãªããžã§ã¯ãauto_ptr <>ããããŸããã æ®å¿µãªãããauto_ptr <>ã«ã¯ããã€ãã®å¶éããããŸãããæãé¡èãªã®ã¯ãç¬èªã®ã¢ã¯ã»ã¹åæ£ã¢ãã«ã®äœ¿çšã§ãã ã€ãŸããæåŸã®auto_ptr <>ãã¡ã¢ãªã®å¯äžã®ææè ã§ããã
auto_ptr <int> ptr1ïŒæ°ããint [1024]ïŒ; auto_ptr <int> ptr2 = ptr1;
ãã®ãããC ++ã³ãã¥ããã£ã§ã¯ãauto_ptr <>ã®äœ¿çšåæ°ã¯ãŒãã«ãªãåŸåããããŸãã
C ++ 09æšæºã©ã€ãã©ãªã«ã¯ãããã¹ããŒããªãã€ã³ã¿ãã¥ãŒshared_ptr <>ãå°å ¥ãããŠããŸãã auto_ptr <>ãšã®äž»ãªéãã¯ãåæ£æš©å©ã¢ãã«ãšåç §ã«ãŠã³ã¿ãŒã䜿çšããŠããã€ã¡ã¢ãªã解æŸãããã決å®ããããšã§ãã äŸïŒ
ã¡ã€ã³ïŒïŒ { shared_ptr <int> ptr1; //ã¹ããŒãNULLãã€ã³ã¿ãŒ ... { shared_ptr <int> ptr2ïŒæ°ããint [1024]ïŒ; ptr1 = ptr2; //ææç©ã®ååžïŒå°å»ºïŒ } // ptr2ãåé€ãããptr1ã®ã¿ãæ®ããŸã //ã¡ã¢ãªã¯ãŸã 解æŸãããŠããŸãã } // ptr1ãåé€ãããã¡ã¢ãªã解æŸãããŸãã
shared_ptr <>ã¯ãã€ã³ã¿ãŒãšèŠãªãããšãã§ããããã* ptrãšããŠäœ¿çšã§ããptr1-> fooïŒïŒãªã©ã®æ§é ã§äœ¿çšã§ããŸãã
æ瀺çãªshared_ptr <T>ïŒT * ptrïŒ; //ã¡ã¢ãªã«ã¢ã¿ããããŸã shared_ptr <T>ïŒT * ptrãFcn delFcnïŒ; //ã¡ã¢ãªãšãŠãŒã¶ãŒå®çŸ©ã®ã¯ãªãŒã³ã¢ããé¢æ°ã«ã¢ã¿ããããŸã shared_ptr <T>ïŒshared_ptr <T> ptrïŒ; //ã³ã³ã¹ãã©ã¯ã¿ãã³ããŒããŸã shared_ptr <T>ïŒauto_ptr <T> ptrïŒ; // auto_ptrã§å€æ<>
æåŸã®ã³ã³ã¹ãã©ã¯ã¿ãauto_ptr <>ããshared_ptr <>ã«ããŒã¿ãå€æããããšã«æ³šæããŠãã ãããããã«ããã以åã®ããŒãžã§ã³ã®ã³ãŒãããã®ç§»è¡ãå€§å¹ ã«å®¹æã«ãªããäžäœäºææ§ãæäŸãããŸãã swapïŒïŒãstatic_pointer_castïŒïŒãdynamic_pointer_castïŒïŒãªã©ãããã€ãã®è¿œå æ©èœãæäŸãããŠããŸãã
shared_ptr <>ã¯ãã§ã«std :: tr1 ::åå空éã®äžéšã§ãããMacããã°ã©ããŒã¯Xcode 2.x以éã§äœ¿çšã§ããŸãã
å³èŸºå€ãªã³ã¯
Cã§ã¯ãé¢æ°ãã©ã¡ãŒã¿ãŒã¯åžžã«å€ã§æž¡ãããŸããã€ãŸãããã©ã¡ãŒã¿ãŒã®ã³ããŒãæž¡ãããŸããããã®å€ã¯é¢ä¿ãããŸããã Cã®å€æ°ãå€æŽããã«ã¯ãäŸã®ããã«ãé¢æ°ã¯ãã€ã³ã¿ãŒãæž¡ãå¿ èŠããããŸãã
void fooïŒint valueParameterãint * pointerParameterïŒ { ++ valueParameter; //ãã©ã¡ãŒã¿ã¯å€ã§æž¡ãããããŒã«ã«ã³ããŒãå€æŽãããŸã ++ pointerParameter; //ãã€ã³ã¿ã¯å€ã§æž¡ãããŸãããããŒã«ã«ã³ããŒã¯ãšã«ããå€æŽãããŸã ++ * pointerParameter; //ãã®å€æŽã¯äžå®ã®ãŸãŸ }
C ++ã®æã匷åãªæ©èœã®1ã€ã¯ã ïŒæŒç®åã䜿çšããŠåç §ã«ãã£ãŠãã©ã¡ãŒã¿ãŒãæž¡ãããšã§ããã ããã«ããããã€ã³ã¿ãŒã䜿çšããã«ãã©ã¡ãŒã¿ãŒããŒã¿ãçŽæ¥å€æŽã§ããŸããã
void fooïŒint valueParameterãintïŒreferenceParameterïŒ { ++ valueParameter; //ãã©ã¡ãŒã¿ã¯å€ã§æž¡ãããããŒã«ã«ã³ããŒãå€æŽãããŸã ++ referenceParameter; //åç §æž¡ãã§ãå€æŽã¯äžå®ã®ãŸãŸ }
ãªã³ã¯ã¯ãå³èŸºå€ïŒèªã¿åãå°çšïŒã§ã¯ãªãã巊蟺å€ïŒãããã¯å€æŽå¯èœãªå€æ°ã§ããããïŒã«é©çšããå¿ èŠããããŸãã
int myIntA = 10; int myIntB = 20; fooïŒmyIntAãmyIntBïŒ; // myIntA = 10ãmyIntB = 21 fooïŒ1ãmyIntAïŒ; //å€ã§æž¡ããã1ãmyIntA = 11 fooïŒmyIntAã1ïŒ; //ãšã©ãŒïŒ1ã¯å³èŸºå€ã§ãããæž¡ãããšã¯ã§ããŸãã fooïŒ0ãmyIntB + 1ïŒ; //ãšã©ãŒïŒmyIntB + 1ã¯å³èŸºå€ã§ãããæž¡ãããšã¯ã§ããŸãã
å 容ãå€æŽããå¿ èŠããªãå Žåã§ããåç §ã«ãã£ãŠãã©ã¡ãŒã¿ãŒãæž¡ããšäŸ¿å©ãªå ŽåããããŸãã ããã¯ã倧ããªã¯ã©ã¹ãŸãã¯æ§é ãé¢æ°ã«æž¡ããããã®ãããªå·šå€§ãªãªããžã§ã¯ãã®ã³ããŒãé¿ããå¿ èŠãããå Žåã«ç¹ã«åªãããœãªã¥ãŒã·ã§ã³ã§ãã
void fooïŒBigClass valueParameterãconst BigClassããã³constRefParameterïŒ { ++ valueParameter; //å€æž¡ããå€æŽã¯äžæç ++ constRefParameter; //ãšã©ãŒïŒå®æ°ãã©ã¡ãŒã¿ãå€æŽã§ããŸãã }
C ++ 09ã§ã¯ãå³èŸºå€ãªã³ã¯ãšåŒã°ããæ°ããã¿ã€ãã®ãªã³ã¯ãå°å ¥ãããŸããïŒãããã£ãŠãC ++ 98ã§ããªãã¿ã®ãªã³ã¯ã¿ã€ãã¯å·ŠèŸºå€ãªã³ã¯ãšåŒã°ããããã«ãªããŸãïŒã å³èŸºå€ãªã³ã¯ã¯äžæããŒã¿ã«æ·»ä»ã§ããŸãããã³ããŒããã«çŽæ¥å€æŽã§ããŸãã &&æŒç®åã¯ããªã³ã¯ãå³èŸºå€ãªã³ã¯ã§ãããšèšããŸãã
void fooïŒint valueParameterãintïŒlvalRefParameterãint && rvalRefParameterïŒ { ++ valueParameter; //ãã©ã¡ãŒã¿ã¯å€ã§æž¡ããããã¹ãŠã®å€æŽã¯ããŒã«ã«ã§ã ++ lvalRefParameter; //巊蟺å€ãªã³ã¯ããã¹ãŠã®å€æŽã¯æ°žç¶ç ++ rvalRefParameter; //å³èŸºå€ãªã³ã¯ãã³ããŒãå¿ èŠãšããªãããŒã«ã«å€æŽ } fooïŒ0ãmyIntAãmyIntB + 1ïŒ; //äžæçãªå€myIntB + 1ã¯ã³ããŒãããŸããããæž¡ãããšãã§ããŸã
å³èŸºå€ãªã³ã¯ã®äž»ãªå©ç¹ã®1ã€ã¯ã移åã®ã»ãã³ãã£ã¯ã¹ãå©çšã§ããããšã§ããã€ãŸããã³ããŒããã«1ã€ã®å€æ°ããå¥ã®å€æ°ã«ããŒã¿ã移åã§ããŸãã ã¯ã©ã¹ã¯ãã³ããŒã³ã³ã¹ãã©ã¯ã¿ãŒã®ä»£ããã«ããŸãã¯ã³ããŒã³ã³ã¹ãã©ã¯ã¿ãŒãšå ±ã«ç§»åã³ã³ã¹ãã©ã¯ã¿ãŒãå®çŸ©ã§ããŸãã
//ã¯ã©ã¹å®çŸ© ã¯ã©ã¹X { å ¬éïŒ XïŒïŒ; //ããã©ã«ãã®ã³ã³ã¹ãã©ã¯ã¿ãŒ XïŒå®æ°XïŒxïŒ; //ã³ã³ã¹ãã©ã¯ã¿ãã³ããŒããŸãïŒå·ŠèŸºå€åç §ïŒ XïŒX && xïŒ; //ã³ã³ã¹ãã©ã¯ã¿ãŒã移åããŸãïŒå³èŸºå€åç §ïŒ }; // Xãè¿ãããŸããŸãªé¢æ° XããŒïŒïŒ; X x1; //ããã©ã«ãã®ã³ã³ã¹ãã©ã¯ã¿ã䜿çšããŠx1ãªããžã§ã¯ããäœæããŸã X x2ïŒx1ïŒ; // x2ã¯x1ã®ã³ããŒã«ãªããŸã X x3ïŒããŒïŒïŒïŒ; // barïŒïŒã¯äžæçãªXãè¿ããã¡ã¢ãªã¯çŽæ¥x3ã«ç§»åããŸã
åäœã®ã»ãã³ãã£ã¯ã¹ã®äž»ãªåæ©ã¯ãçç£æ§ã®åäžã§ãã 2ã€ã®è¡ãã¯ãã«ãããããããã®éã§ããŒã¿ã亀æãããšããŸãã æšæºã®ã³ããŒããžãã¯ã䜿çšããŠã次ã®ã³ãŒããååŸããŸãã
void SwapDataïŒãã¯ã¿ãŒ<string>ïŒv1ããã¯ã¿ãŒ<string>ïŒv2ïŒ { vector <string> temp = v1; //æ°ããã³ããŒv1 v1 = v2; //æ°ããã³ããŒv2 v2 = temp; //äžæã®Noviaã³ã㌠};
移åã®ããžãã¯ã䜿çšãããšã次ã®ãããªãã®ãåŸãããŸãã
void SwapDataïŒãã¯ã¿ãŒ<string>ïŒv1ããã¯ã¿ãŒ<string>ïŒv2ïŒ { ãã¯ã¿ãŒ<string> temp =ïŒãã¯ã¿ãŒ<string> &&ïŒv1; // temp-v1ãšåãããŒã¿ v1 =ïŒãã¯ã¿ãŒ<string> &&ïŒv2; // v1ã«ã¯v2ãå«ãŸããŸã v2 =ïŒãã¯ã¿ãŒ<string> &&ïŒtemp; // v2ã¯äžæããŒã¿ãæããŸã } //åäžã®ã³ããŒã§ã¯ãªãã移åã®ã¿ãäœæãããŸããïŒ
C ++ 09ã®ãã®ä»ã®è¿œå
説æããæ©èœã«å ããŠãä»ã®å€æŽç¹ã®ãªã¹ãã以äžã«ç€ºããŸãã
æ°ããæåã¿ã€ã ïŒchart16_tãchar32_t
éçã¹ããŒãã¡ã³ã ïŒã¢ãµãŒããBoost ::ããïŒ
ãšã€ãªã¢ã¹ãã³ãã¬ãŒã
åãã§ã㯠ïŒis_pointerïŒïŒãis_sameïŒïŒ
Foreachã®çŽ¹ä»
æ°ããä¹±æ°ãžã§ãã¬ãŒã¿ãŒ
çµè«
C ++ã®æ¬¡ã®ããŒãžã§ã³ãžã®å€ãã®å€æŽã¯ãæšæºã©ã€ãã©ãªã«é¢é£ããŠãããšããäºå®ã«ãããããã°ã©ããŒãå©çšã§ããããã«ãªããŸããã ããã«ãããããããèšèªã®ä»åŸã®æŽæ°ã«åããããšã¯ä»äŸ¡å€ããããŸãã ããããã¹ãŠã®å€æŽã«ã€ããŠèªããšãC ++ã«ã¯ããªãèå³æ·±ãæªæ¥ãããããšãæããã«ãªããŸãã