ä½ã ããã ã§C++ãæ¸ãç¶ãã¦ãã¾ããä»ãC++20ã§ããC++0xã§é¨ãã§ããé ãæãããã§ãã
ä»åã¯æè¿èªåãC++ãæ¸ãæã«æ°ã«ãã¦ãããã¨ã«ã¤ãã¦ç°¡åã«ç´¹ä»ãã¦ãããã¨æãã¾ãã
åºæ¬æ¹é
åãã³ã¼ãã触ãéçºè
ãå¤ããããå®è£
è
ã«ã¨ã£ã¦ã®å©ä¾¿æ§ãããå
±åéçºè
ã«ã¨ã£ã¦ã®å¯èªæ§ãä¿å
¨æ§ãéè¦ãã¦ãã¾ãããã®ãããå¯èªæ§ãä¸ããæ©è½ãæ±ããé£ããæ©è½ã¯æ±ããªãäºãå¤ãã§ãã
auto
autoã¯æ¸ãæã«ã¨ã£ã¦ã¯ä¾¿å©ã§ãããå¤ç¨ããã¨ï¼ç¹ã«å
±åéçºè
ã«ã¨ã£ã¦ã®ï¼ã³ã¼ãã®å¯èªæ§ãä¸ãããããã¾ã使ãã¾ããã
std::optional<std::vector<CrawledArticle>> articles = CrawlArticles(...);
auto articles = CrawlArticles();
ãã ããã³ã³ããã®ã¤ãã¬ã¼ã¿ã¼ã¯ååãç¡é§ã«é·ããã¾ãå¤æ°ã®åãå¨è¾ºã®ã³ã¼ãããæãããªãã¨ãå¤ãããautoã使ã£ã¦ãã¾ãã
åæ§ã®çç±ã§ãstd::make_uniqueã®æ»ãå¤ãåããå¤æ°ã«å¯¾ãã¦ãautoã使ã£ã¦ãã¾ãã
auto crawler = std::make_unique<ArticleCrawler>(...);
std::unique_ptr<ArticleCrawler> crawler = std::make_unique<ArticleCrawler>(...);
std::pairã¨std::tuple
operator<ãªã©ãå®ç¾©ããã¦ãããã便å©ã§ãããfirstãsecondãget<3>ã¯descriptiveãªååã§ã¯ãªããå¯èªæ§ãä¸ãããã極å使ããªãããã«ãã¦ãã¾ããå¯è½ã§ããã°structãå®ç¾©ããæ¹ãå¯èªæ§ãé«ãã³ã¼ãã«ãªãã¾ãã
if (ProcessArticle(article_info.second)) {...}
if (ProcessArticle(std::get<2>(article_info))) {...}
ç¶æ¿
ããªã¢ã¼ãã£ãºã ã®ããã®ç¶æ¿ã¯ã»ã¼ä½¿ã£ã¦ãã¾ããããã¾ã«Strategyãã¿ã¼ã³ã®ããã«ä½¿ããã¨ãããç¨åº¦ã§ãããå¤ç¨ããã¨ä»ã®éçºè
ã«ã¨ã£ã¦ã®å¯èªæ§ãä¸ããï¼ãªãã¸ã§ã¯ããã¤ã³ã¹ã¿ã³ã¹åããã¦ããç®æã¾ã§è¾¿ããªãã¨ä»®æ³é¢æ°å¼ã³åºããã©ã®å®è£
ãå¼ãã§ããã®ãåãããªããªãï¼ããæ°ãã¤ãã¦ãã¾ããTemplate Methodãã¿ã¼ã³çãå®è£
ãå
±æããç®çã§ã®ç¶æ¿ã¯ä½¿ã£ã¦ãã¾ããï¼å
±åéçºè
ã§ä½¿ã£ã¦ãã人ã¯ãã¾ãï¼ã
Dependency Injectionã§ã¦ããããã¹ããããããããç®çã§ã®ç¶æ¿ã¯ä½¿ã£ã¦ãã¾ããAbstract Factoryãç¨ã«ä½¿ãã¾ãããã ããå
·è±¡ã¯ã©ã¹ã使ã£ããã¹ãã§æ¸ãå ´åï¼ãããã¯ã¼ã¯ããã£ã¹ã¯ã¢ã¯ã»ã¹ãèµ·ããããåä½ãååéãã¯ã©ã¹ã«ä¾åããã³ã¼ãã®å ´åï¼ã¯ãªãã¹ãDependency Injectionã使ããã«ãã¹ããã¦ãã¾ãã
ãã³ãã¬ã¼ã
ãã³ãã¬ã¼ãã¯å¼·åã§ãããå
±åéçºè
ãã³ã¼ããèªãæã«é¢æ°ãã³ãã¬ã¼ããã¯ã©ã¹ãã³ãã¬ã¼ããã¤ã³ã¹ã¿ã³ã¹åããã¦ããç®æã¾ã§è¾¿ããªãã¨åä½ã®ç¢ºèªãã§ããªãå ´åãããããå¤ç¨ããªãããã«ãã¦ãã¾ãããã ããä»ã®äººãè²ã
ãªåã®å¤æ°ã§ä½¿ããããªããããªã©ã¤ãã©ãªãè¨è¨ããå ´åã¯è©±ãå¤ãã£ã¦ããã¨æãã¾ãã
std::unique_ptr
é常ã«ãã使ãã¾ããçã®ãã¤ã³ã¿ãnewãããã¨ã¯ã»ã¼ããã¾ãããstd::unique_ptrãã¤ã³ã¹ã¿ã³ã¹åããæã¯std::make_uniqueã§è¡ãã¾ããstd::unique_ptrã®ã³ã³ã¹ãã©ã¯ã¿ã¯ã»ã¼ä½¿ãã¾ããã
std::unique_ptrã¯é¢æ°ã®ã·ã°ããã£ã¼ä¸ã§ãªãã¸ã§ã¯ãã®ææ権ã®ç§»åãæ確åããããã«ã使ãã¾ããå¯èªæ§ãä¿å
¨æ§ã®åä¸ãæå¾
ã§ãã¾ãã
class Notifier {
public:
explicit Notifier(std::unique_ptr<MailSender> mail_sender)
: mail_sender_(std::move(mail_sender)) {}
...
private:
std::unique_ptr<MailSender> mail_sender_;
};
...
auto mail_sender = std::make_unique<MailSender>(...);
...
auto notifier = std::make_unique<Notifier>(std:move(mail_sender));
std::unique_ptr<ArticleCrawler> CreateArticleCrawler();
ã¡ãªã¿ã«std::shared_ptrã¯ä»ã®ã¨ããæ»
å¤ã«ä½¿ã£ã¦ãã¾ããã
Designated initializers
structã®åæåã§ä¾¿å©ãã¤å¯èªæ§ãåä¸ããã®ã§ãã使ãã¾ããæ¥æ¬èªã§ã¯æ示ä»ãåæåã¨è¨ããããã§ãã
struct Person {
std::string name;
int age = 0;
};
Person person = {.name = "John Doe", .age = 42};
æ示ä»ãåæåã¯å
ã
C99ã§å°å
¥ãããæ©è½ã§ãããC++20ã§ãå©ç¨å¯è½ã«ãªãã¾ããããã ãããã¤ãC++20ç¹æã®å¶ç´ãããã¾ãï¼ãã¡ãã®ãã¼ã¸ã®"ä»æ§"ãåç
§ãã¦ãã ããï¼ã
ä»ã«ããæ示ä»ãåæåã¯å¼æ°ãå¤ãorããã©ã«ãå¼æ°ã®ä¸é¨ã ããå¤ãã¦å¼ã³åºããã¨ãå¤ãé¢æ°ã«ä½¿ãã¨ä¾¿å©ã§ãã
abseil / Tip of the Week #173: Wrapping Arguments in Option Structsã«è¼ã£ã¦ããä¾ãå°ãç°¡ç¥åãããã®ã以ä¸ã«ç´¹ä»ãã¾ãã
struct PrintDoubleOptions {
std::string_view prefix = "";
int precision = 8;
char thousands_separator = ',';
char decimal_separator = '.';
bool scientific = false;
};
void PrintDouble(double value,
const PrintDoubleOptions& options = {});
PrintDouble(5.0, {.prefix = "value=", .scientific = true});
structã¨æ示ä»ãåæåãªãã ã¨ä»¥ä¸ã®ããã«æ±ãã¥ããã³ã¼ãã«ãªãã¾ãã
void PrintDouble(double value, std::string_view prefix = "", int precision = 8,
char thousands_separator = ',', char decimal_separator = '.',
bool scientific = false);
PrintDouble(5.0, "value=", 8, ',', '.', true);
çµããã«
ä»ã«ãããã¸ã§ã¯ãç¬èªã®è¦ç´ãèªåãªãã®åºæºãããã¾ãããæè¿ããæ°ã«ãã¦ããã®ã¯ã ããããã®è¾ºãã§ãã