小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

C 20最新進展:移除合約(Contracts)、新增類模板參數推導優(yōu)化和constinit關鍵字...

 昵稱16619343 2019-08-03

上周 C++ 標準委員會在科隆舉行了7月會議(WG21)。會議中重點針對 C++20 標準已經投票通過的特性進行了若干修正,并對一些草案進行了討論和投票。相比上次會議,通過了一些新的草案。

會議報告請訪問:
《Trip Report: C++ Standards Meeting in Cologne, July 2019》

  • 合約(Contracts)

  • 類模板參數推導優(yōu)化

  • constexpr 容器的語言支持

  • constinit 關鍵字

  • using enum

  • 在大多數場景下廢棄 volatile 關鍵字

  • 文本格式化支持

  • C++20 Synchronization Library

  • Input Range 適配器

  • constexpr std::vector

  • constexpr std::string

  • 線程可中斷 join 支持(Stop Token)

  • std::source_location

  • 概念(Concepts)采用 C++ 標準庫命名規(guī)范

  • constexpr std::invoke

新增特性介紹

上述特性是本人認為的本次會議投票通過的主要特性。若要瀏覽全部特性列表,請訪問引言中的鏈接。

下面我將其中一些關鍵特性作簡要介紹。

constinit 關鍵字

這是新引入的一個關鍵字,用于強制標記一個全局變量的初始化在編譯期完成。若初始化表達式無法在編譯期求值,則會引發(fā)編譯錯誤。這樣便可以避免全局變量的隱式運行時初始化帶來的各種各樣難以調試的 BUG。

示例:

constchar* g{ return'運行時初始化'; }
constexprconstchar* f(bool p){ return p ? '常量初始化' : g; }
constinit auto c = f(true); // OK
constinit auto d = f(false); // 編譯錯誤

using enum

這個特性算是對現有using語句用法的一個功能補全。它允許 using 語句運用在enumenum class之中。
給出枚舉:
// china_railway.hpp
enumclasschina_railway
{
cr400af,
cr400bf,
cr200j
};
用法1:
usingenum china_railway;
constexprauto train_type = cr400af;
用法2:
structfoo
{
// 引入枚舉成員。
usingenum china_railway;
};
voidgeek
{
foo f;
auto a = f.cr400af; // 使用類實例,合法
auto b = foo::cr200j; // 使用類名,合法
}
用法3:
using china_fuxing_trains = china_railway;
using rubbish = china_railway::cr200j; // 枚舉值別名
constexprauto type = china_fuxing_trains::cr400bf;
constexprauto fake_emu_train = rubbish; // 和 cr200j 等價

文本格式化支持

這是繼模塊、協(xié)程和概念后又一個重磅特性。它彌補了 C++ 標準庫缺乏文本格式化支持的一個遺憾。這次通過的提案基于開源庫 fmt,語法十分優(yōu)雅。文本格式化主要通過兩個新的標準庫函數std::formatstd::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);
示例2:std::formatstd::format函數支持高級格式化控制。它使用格式控制表達式進行格式化輸出。標準格式控制表達式的形式如下所示。
'{[arg_index]:[[fill]align][sign]['#']['0'][width]['.' precision][type]}'
說明:上式中的方括號代表為可選參數。
怎么樣,是不是通過上表的說明,大家對這個格式控制有了一個初步的理解呢?下面我們繼續(xù)展開,深入講講alignsigntype參數取值的具體意義。表格1:align參數說明
表格2:sign參數說明
表格3: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 '
示例3:std::format_tostd::format_to的格式化添加值。
#include <vector>
#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,可為用戶提供當前代碼的上下文信息。該類的定義如下所示。
namespacestd
{
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 <source_location>
#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::invocable
std::ConvertibleTo 改為 std::convertible_to
std::EqualityComparable改為

等等

總結

由于這次投票通過的特性還是比較多的,我只挑選了幾個有代表性的展開,其他的就不再一一贅述。(畢竟肉翻還是很費腦細胞的233)。感興趣的同學請查看「閱讀原文」進行查閱。由于本人水平有限,若有瑕疵,在所難免。歡迎大家拍磚指正!

按照計劃的發(fā)布進度,C++20 預計將于明年夏季推出。

開源中國征稿啦!

開源中國 www.oschina.net 是目前備受關注、具有強大影響力的開源技術社區(qū),擁有超過 400 萬的開源技術精英。我們傳播開源的理念,推廣開源項目,為 IT 開發(fā)者提供一個發(fā)現、使用、并交流開源技術的平臺。

現在我們開始對外征稿啦!如果你有優(yōu)秀的技術文章想要分享,熱點的行業(yè)資訊需要報道等等,歡迎聯(lián)系開源中國進行投稿。

這款數據庫將企業(yè)版功能100%開源還不收費,why?

    本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現有害或侵權內容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多