ã¯ããã«
ãCè¨èªããæ´¾çãããªãã¸ã§ã¯ãæåããã°ã©ãã³ã°è¨èªã§ããC++ã¯ã21ä¸ç´ã«å ¥ã£ã¦ã¾ã£ããå¥ç©ã¨ãè¨ããããã°ã©ãã³ã°è¨èªã«æé·ãã¦ããã¾ãããããã¯ãModern C++ã¨ç§°ããã¦ãã¾ãã1990年代ã«C++ã触ã£ã¦ããããã°ã©ããç¾å¨ã®ä»æ§ãç¥ãã°ãéä¸æã«èã¾ããã®ã§ã¯ãªãã§ãããããæ¬é£è¼ã§ã¯ããã¤ã¦ã¯C++ããããªãã§ããã¨ããæ¹ãä»ããC++è¨èªãå§ããã¨ããæ¹ã«åãã¦ãModern C++ãããè¨èªä»æ§ãããã¯ã¢ãããç´¹ä»ãã¦ãããã¨ã§ãä»ã®C++è¨èªã®å§¿ãç解ãã¦ããã ãã¾ãã
対象èªè
- ãã¤ã¦ã¯C++ããããªãã§ããã¨ããæ¹
- ä»ããC++è¨èªãå§ããã¨ããæ¹
- ã¢ãã³ãªããã°ã©ãã³ã°è¨èªã®ãã©ãã¤ã ã«èå³ã®ããæ¹
å¿ è¦ãªç°å¢
ãæ¬è¨äºã®ãµã³ãã«ã³ã¼ãã¯ã以ä¸ã®ç°å¢ã§åä½ã確èªãã¦ãã¾ãã
-
macOS Sonomaï¼Windows 11
- Xcode Command Line Tools 2395
- MinGW GCC 9.2.0
constexprã¨nullptr
ã20ä¸ç´ã®C++ããã°ã©ãï¼çè ï¼ããä»ï¼21ä¸ç´ï¼ã®C++ãç¥ã£ã¦ãã£ã¨é©ãé£è¼ã®ç¬¬6åã§ããä»åã¯ãæè¿ã®ããã°ã©ãã³ã°è¨èªã§ã¯å½ããåã®ãã¢ãã³ãªè¨èªä»æ§ãããã¯ã¢ãããã¦ç´¹ä»ãã¾ããã¾ãã¯ãã·ã³ãã«ãªè©±ã¨ãã¦constexprã¨nullptrãããããå§ãã¾ãããã
ï¼»NOTEï¼½C++ããã°ã©ã ã®ã³ã³ãã¤ã«
ã以éã®ãµã³ãã«ã¯ãåºæ¬çã«ãgcc -o åºåå ãã¡ã¤ã«å ã½ã¼ã¹ãã¡ã¤ã«åãã§ã³ã³ãã¤ã«ã§ãã¾ãããç°å¢ã«ãã£ã¦ã¯ã©ã¤ãã©ãªãC++ãã¼ã¸ã§ã³ã®æå®ãå¿ è¦ã«ãªããã¨ãããã¾ããã³ã³ãã¤ã«ããªã³ã¯ã§ã¨ã©ã¼ãçºçããã¨ãã«ã¯ã以ä¸ã®ããã«ã©ã¤ãã©ãªã®æå®ï¼-lstdc++ï¼ãC++ãã¼ã¸ã§ã³ã®æå®ï¼-std=c++11ï¼ã®è¿½å ãæ¤è¨ãã¦ãã ããã
% gcc -o sample sample.cpp -lstdc++ -std=c++11 // c++14, c++17, c++20ãªã©
å®æ°å®ç¾©ã¯constexprã§ï¼»C++11ï¼½
ãC/C++è¨èªã«ã¯const修飾åã¨ãããã®ãããã¾ãã¦ãå¤æ°å®£è¨ã«é©ç¨ããå ´åã«ã¯ãã®å¤æ°ãä¸å¤ï¼immutableï¼ã¨ãããã¨ãã§ãã¾ããä¾ãã°ã以ä¸ã®ãããªæãã§ãï¼ãã¯ããå®æ°ä»£ããã«ä½¿ãã¨ãããã¨ãããè¡ããã¦ãã¾ããããå ¨ãå®å ¨ã§ãªãã®ã§ããã§ã¯å²æãã¾ãï¼ã
const int a = 100; a = 200; // ã³ã³ãã¤ã«ã¨ã©ã¼ // cannot assign to variable 'a' with const-qualified type 'const int' const int b = rand(); b = 300; // ã³ã³ãã¤ã«ã¨ã©ã¼ // cannot assign to variable 'b' with const-qualified type 'const int'
ãconst修飾ãããaï¼ãããã¯bï¼ã«ã¯åä»£å ¥ã§ããªããã¨ããã¨ã©ã¼ã«ãªãã¾ããaã¨bã§ç°ãªãã®ã¯ãaããªãã©ã«ã«ããåæåãbãé¢æ°ã«ããåæåã¨ãããã¨ã§ããconst修飾åã§ã¯ã両è ãç¹ã«åºå¥ãããã¨ã«ããåä»£å ¥ä¸å¯ã¨ãã¾ãã
ãC++ 11ã§ã¯ãåè ã®ãããªã±ã¼ã¹ã«ã¯constexpræå®åã使ã£ã¦æ確ã«åºå¥ãããã¨ã«ãªãã¾ããã
constexpr int c = 200; c = 400; // ã³ã³ãã¤ã«ã¨ã©ã¼ constexpr int d = rand(); // ã³ã³ãã¤ã«ã¨ã©ã¼ // constexpr variable 'd' must be initialized by a constant expression
ãåè ã¯ãconst修飾åã®ä¾ã¨åãã§åä»£å ¥æã«ã¨ã©ã¼ã¨ãªãã¾ãããå¾è ã¯å®£è¨æã«å®æ°ã§ã®åæåãå¿ è¦ã¨ããã¨ã©ã¼ã¨ãªãã¾ããã¤ã¾ããconst修飾åã¨constexpræå®åã«ã¯ä»¥ä¸ã®éããããã¨ãããã¨ã§ãã
- constï¼åæååãå®æ°ãå¤æ°ãã¯åããªããã¨ã«ããå¤æ´ã§ããªãå¤æ°ã¨ãã
- constexprï¼åæååã¯å®æ°ã§ãªããã°ãªããªã
ãconstexpræå®åã«ãã£ã¦ä¸å¤ã¨ãããå¤æ°ã¯ãã³ã³ãã¤ã«æã«å¤ã決ã¾ãã®ã§ãã³ã³ãã¤ã«å¾ã®ãã¤ããªã«ç´å¤ã¨ãã¦åãè¾¼ã¾ãã¾ãï¼ã¤ã¾ãå®è¡å¹çã®åä¸ã«ãªãã¾ãï¼ãconst修飾åã«ãã£ã¦ä¸å¤ã¨ãããå¤æ°ã¯ãåæååãæé©åã«ãã£ã¦ã±ã¼ã¹ãã¤ã±ã¼ã¹ï¼ã¡ã¢ãªã«ç½®ãããããç´å¤ã¨ãªã£ããï¼ã¨ãªãã¾ãããã£ã¦ãæãããªå®æ°ã«ã¯constexpræå®åã使ãã®ããC++ 11以éã§ã¯æ£ãã使ãåãã¨è¨ãã¾ãã
ããªããconstexpré¢æ°ã¨ãããã®ããããããã¯å ¥åãæ»ãå¤ãconstexprã¨ãªãé¢æ°ã§ãããã®é¢æ°ã«ããåæåã¯OKã§ãããã ãããã¾ã§ãã³ã³ãã¤ã©ã追跡ã§ãã¦ãæçµçãªçµæãå®æ°ã§ããã¨å¤æã§ããå ´åã®ã¿ã§ãã以ä¸ã®ãªã¹ãã§ã¯ãæ£ããconstexpré¢æ°ã¨ãå¼ã³åºããªãconstexpré¢æ°ã®ä¾ã示ãã¦ãã¾ãã
// å ¥åãconstexprã§è¨ç®çµæãconstexprã¨ã§ããã®ã§å¼ã³åºãå¯è½ constexpr bool is_greater(int a, int b) { a += 3; return (a > b)? true : false; } // å ¥åãconstexprã§ãé¢æ°å ã§constexprã§ãªãé¢æ°å¼ã³åºããããã®ã§å¼ã³åºãä¸å¯ constexpr bool is_lower(int a, int b) { a += rand(); // ã³ã³ãã¤ã«ã¨ã©ã¼ return (a < b)? true : false; } constexpr bool greater = is_greater(5, 4); // OK constexpr bool lower = is_lower(8, 2); // é¢æ°ã¯å¼ã³åºãä¸å¯
NULLãã¤ã³ã¿ã¯nullptrã§ï¼»C++11ï¼½
ãC/C++è¨èªã§ã¯ãé·ãéNULLãã¤ã³ã¿ã®æ確ãªå®ç¾©ãããã¾ããã§ãããç¹ã«Cè¨èªã§ã¯ãããã°ã©ãã«ãã£ã¦ä»¥ä¸ã®ããã«åæã«å®ç¾©ãã¦ä½¿ã£ã¦ããã®ã§ã¯ãªããã¨æãã¾ãã
#define NULL ((void *)0) // ãããã¯åã«ã0ãã¨ããã¨ã⦠void *ptr = NULL;
ãNULLãã¤ã³ã¿ã便å®ä¸ã¢ãã¬ã¹0çªå°ã¸ã®ãã¤ã³ã¿ã¨ããããã§ãããã®ãããNULLãã¤ã³ã¿ã§ã¡ã¢ãªã«æ¸ãè¾¼ãã§ãã¾ã£ããã¨ãããã°ã©ã çµäºæã«æ¤åºããNull pointer assignmentã¨ãã£ãã¨ã©ã¼ãåãåºãå¦çç³»ãããã¾ãããC++ 03ã§ã¯å°ãæ¹åããã¦ãNULLãã¯ããå®ç¾©æ¸ã¿ã¨ãªãã¾ããããªã®ã§ãä¸ã®ãªã¹ãã®ããã«#defineãã¦ã³ã³ãã¤ã«ããã¨åå®ç¾©ã¨ã©ã¼ã«ãªãã¾ããã¡ãªã¿ã«ããã®NULLãã¯ãã¯ãgccã§ã¯__nullã¨ããå¤ã§å®ç¾©ããã¦ãã¾ãã__nullã«ã¯ç§»æ¤æ§ã¯ãªããããªã®ã§ããã®ã¾ã¾ä½¿ãã®ã¯é¿ããæ¹ãããã§ãããã
ããã ããã¤ã¾ã§ããã®ãããªãã¯ãã使ãã®ã¯ã©ããã¨ãããã¨ã§ãC++ 11ã«ããã¦è¨èªä»æ§ã¨ãã¦nullptrãå®ãããã¾ãããnullptrã¯å½ããåã§ããæ´æ°åãªã©ã«å¤æãããªãã®ã§ãå®å ¨ã«ä½¿ããã¨ãã§ãã¾ãããã¤ã¦ã\0ããã«æåã¨è¡¨ç¾ãã¦ããããããã£ã¦ããã¤ã³ã¿ã®NULLã¨ãã«æåã®NULLããã£ã¡ãã«ãªã£ã¦ããã±ã¼ã¹ãããã¾ãããNULLã0ã«å®ç¾©ããã¦ããã¨ãã©ã¡ãã«ã§ãé©ç¨ã§ãã¦ãã¾ãã®ã§ãããã¬ãã°ã®å ã§ããã
ãnullptrã使ãã¨ããã¤ã³ã¿å¤æ°ãæ示çã«NULLãã¤ã³ã¿ã¨ãã¦åæåã§ãã¾ããã¾ããé£è¼ç¬¬3åã§ç´¹ä»ããã¹ãã¼ããã¤ã³ã¿ããã³ã³ã¹ãã©ã¯ã¿ã«ãã£ã¦æé»çã«nullptrã«åæåããã¾ãã
int *nptr = nullptr; unique_ptr<int> int_ptr; shared_ptr<int> str_ptr; *nptr = 1; // å®è¡æsegmentation fault
ããªãããã®ãªã¹ãã®æçµè¡ã¯nullptrã«ããæ¸ãè¾¼ã¿ã¨ãªãã¾ãããã³ã³ãã¤ã«æã«ã¯ææãã¦ãããã«å®è¡æã«ã¨ã©ã¼ã¨ãªãã¾ãããã®ããã«ãã³ã³ãã¤ã«æã«nullptrã追跡ãã¦ãããããã§ã¯ãªãããã§ãã