|
上周 C++ 標準委員會在科隆舉行了7月會議(WG21)。會議中重點針對 C++20 標準已經投票通過的特性進行了若干修正,并對一些草案進行了討論和投票。相比上次會議,通過了一些新的草案。 會議報告請訪問:
上述特性是本人認為的本次會議投票通過的主要特性。若要瀏覽全部特性列表,請訪問引言中的鏈接。 下面我將其中一些關鍵特性作簡要介紹。 constinit 關鍵字這是新引入的一個關鍵字,用于強制標記一個全局變量的初始化在編譯期完成。若初始化表達式無法在編譯期求值,則會引發(fā)編譯錯誤。這樣便可以避免全局變量的隱式運行時初始化帶來的各種各樣難以調試的 BUG。 示例: constexprconstchar* f(bool p){ return p ? '常量初始化' : g; } constinit auto c = f(true); // OK constinit auto d = f(false); // 編譯錯誤 using enum這個特性算是對現有using語句用法的一個功能補全。它允許 using 語句運用在enum和enum class之中。enumclasschina_railway { cr400af, cr400bf, cr200j }; constexprauto train_type = cr400af; { // 引入枚舉成員。 usingenum china_railway; }; voidgeek { foo f; auto a = f.cr400af; // 使用類實例,合法 auto b = foo::cr200j; // 使用類名,合法 } using rubbish = china_railway::cr200j; // 枚舉值別名 constexprauto type = china_fuxing_trains::cr400bf; constexprauto fake_emu_train = rubbish; // 和 cr200j 等價 文本格式化支持這是繼模塊、協(xié)程和概念后又一個重磅特性。它彌補了 C++ 標準庫缺乏文本格式化支持的一個遺憾。這次通過的提案基于開源庫 fmt,語法十分優(yōu)雅。文本格式化主要通過兩個新的標準庫函數std::format和std::format_to示例1:std::format基本用法auto text1 = std::format('{} 是動車組,但 {} 卻不是。', 'CR400AF', 'CR200J'); // 手動編號 auto text2 = std::format('我國領土面積 {0} 萬平方公里,人口有 {1} 億。', 960, 14); // 取消“{}”的轉義。 // text3 = '我們的分組是:{100, 200, 300}。' auto text3 = std::format('我們的分組是:{{{0}, {1}, {2}}}。', 100, 200, 300); std::formatstd::format函數支持高級格式化控制。它使用格式控制表達式進行格式化輸出。標準格式控制表達式的形式如下所示。'{[arg_index]:[[fill]align][sign]['#']['0'][width]['.' precision][type]}'align、sign和type參數取值的具體意義。表格1:align參數說明sign參數說明type參數說明char c = 120; auto s0 = std::format('{:6}', 42); // s0 == ' 42' auto s1 = std::format('{:6}', 'x'); // s1 == 'x ' auto s2 = std::format('{:*<6}', 'x'); // s2 == 'x*****' auto s3 = std::format('{:*>6}', 'x'); // s3 == '*****x' auto s4 = std::format('{:*^6}', 'x'); // s4 == '**x***' auto s6 = std::format('{:6d}', c); // s6 == ' 120' auto s7 = std::format('{:=+06d}', c); // s7 == '+00120' auto s8 = std::format('{:0=#6x}', 0xa); // s8 == '0x000a' auto s9 = std::format('{:6}', true); // s9 == 'true ' std::format_tostd::format_to的格式化添加值。#include <format> #include <string> intmain { std::vector<std::string> data; for (size_t i = 0; i < 100; i++) { std::format_to(std::back_inserter(data), '我的工號是:{} ', i); } } std::format這是一個比較實用的特性。它為標準庫新增了一個元數據類std::source_location,可為用戶提供當前代碼的上下文信息。該類的定義如下所示。{ structsource_location { constexprsource_locationnoexcept; constexpr uint_least32_t lineconstnoexcept; constexpr uint_least32_t columnconstnoexcept; constexprconstchar* file_nameconstnoexcept; constexprconstchar* function_nameconstnoexcept; static consteval source_location currentnoexcept; }; } #include <format> #include <iostream> voidfoo { // Get the location of this line. auto loc = std::source_location::current; std::cout << std::format('Line: {}, Column: {}, Function: {}, File: {}', loc.line, loc.column, loc.function_name, loc.file_name) << std::endl; } 概念(Concepts)采用標準庫命名規(guī)范這個特性是指原本采用 Pascal 命名法的概念,在這次會議中投票通過改為與標準庫一致的小寫下劃線命名法。如: std::Invocable 改為 std::invocablestd::ConvertibleTo 改為 std::convertible_tostd::EqualityComparable改為等等 總結由于這次投票通過的特性還是比較多的,我只挑選了幾個有代表性的展開,其他的就不再一一贅述。(畢竟肉翻還是很費腦細胞的233)。感興趣的同學請查看「閱讀原文」進行查閱。由于本人水平有限,若有瑕疵,在所難免。歡迎大家拍磚指正! 按照計劃的發(fā)布進度,C++20 預計將于明年夏季推出。 開源中國征稿啦! 開源中國 www.oschina.net 是目前備受關注、具有強大影響力的開源技術社區(qū),擁有超過 400 萬的開源技術精英。我們傳播開源的理念,推廣開源項目,為 IT 開發(fā)者提供一個發(fā)現、使用、并交流開源技術的平臺。 現在我們開始對外征稿啦!如果你有優(yōu)秀的技術文章想要分享,熱點的行業(yè)資訊需要報道等等,歡迎聯(lián)系開源中國進行投稿。 這款數據庫將企業(yè)版功能100%開源還不收費,why? |
|
|
來自: 昵稱16619343 > 《區(qū)塊鏈》