新年伊始,要說什么選題最合適,那無疑是C++23了。
23是個小版本,主要在于「完善」二字,而非「新增」。因此,值得單獨拿出來寫篇文章的特性其實并不多,大多特性都是些瑣碎小點,三言兩語便可講清。
本篇包含絕大多數C++23特性,難度三星就表示只會介紹基本用法,但有些特性的原理也會深入講講。
1Deducing this(P0847)
Deducing this是C++23中最主要的特性之一。msvc在去年3月份就已支持該特性,可以在v19.32之后的版本使用。
為什么我們需要這個特性?
大家知道,成員函數都有一個隱式對象參數,對于非靜態成員函數,這個隱式對象參數就是this指針;而對于靜態成員函數,這個隱式對象參數被定義為可以匹配任何參數,這僅僅是為了保證重載決議可以正常運行。
Deducing this所做的事就是提供一種將非靜態成員函數的「隱式對象參數」變為「顯式對象參數」的方式。為何只針對非靜態成員函數呢?因為靜態成員函數并沒有this指針,隱式對象參數并不能和this指針劃等號,靜態函數擁有隱式對象參數只是保證重載決議能夠正常運行而已,這個參數沒有其他用處。
于是,現在便有兩種寫法編寫非靜態成員函數。
1structS_implicit{
2voidfoo(){}
3};
4
5structS_explicit{
6voidfoo(thisS_explicit&){}
7};
通過Deducing this,可以將隱式對象參數顯式地寫出來,語法為this+type。
該提案最根本的動機是消除成員函數修飾所帶來的冗余,舉個例子:
1//Before
2structS_implicit{
3intdata_;
4
5int&foo()&{returndata_;}
6constint&foo()const&{returndata_;}
7};
8
9//After
10structS_explicit{
11intdata_;
12
13template
14auto&&foo(thisSelf&self){
15returnstd::forward(self).data_;
16}
17};
原本你也許得為同一個成員函數編寫各種版本的修飾,比如&, const&, &&, const &&,其邏輯并無太大變化,完全是重復的機械式操作。如今借助Deducing this,你只需編寫一個版本即可。 這里使用了模板形式的參數,通常來說,建議是使用Self作為顯式對象參數的名稱,顧名思義的同時又能和其他語言保持一致性。 該特性還有許多使用場景,同時也是一種新的定制點表示方式。 比如,借助Deducing this,可以實現遞歸Lambdas。
1intmain(){
2autogcd=[](thisautoself,inta,intb)->int{
3returnb==0?a:self(b,a%b);
4};
5
6std::cout<
這使得Lambda函數再次得到增強。 又比如,借助Deducing this,可以簡化CRTP。
1////Before
2//CRTP
3template
4structBase{
5voidfoo(){
6auto&self=*static_cast(this);
7self.bar();
8}
9};
10
11structDerived:Base{
12voidbar()const{
13std::cout<"CRTP?Derived
";
14????}
15};
16
17////////////////////////////////////////////
18////?After
19//?Deducing?this
20struct?Base?{
21????template?
22voidfoo(thisSelf&self){
23self.bar();
24}
25};
26
27structDerived:Base{
28voidbar()const{
29std::cout<"Deducing?this?Derived
";
30????}
31};
這種新的方式實現CRTP,可以省去CR,甚至是T,要更加自然,更加清晰。 這也是一種新的定制點方式,稍微舉個簡單點的例子:
1//Library
2namespacemylib{
3
4structS{
5autoabstract_interface(thisauto&self,intparam){
6self.concrete_algo1(self.concrete_algo2(param));
7}
8};
9}//namespacemylib
10
11namespaceuserspace{
12structM:mylib::S{
13autoconcrete_algo1(intval){}
14autoconcrete_algo2(intval)const{
15returnval*6;
16}
17};
18}//namespaceuserspace
19
20intmain(){
21usinguserspace::M;
22Mm;
23m.abstract_interface(4);
24}
這種方式依舊屬于靜態多態的方式,但代碼更加清晰、無侵入,并支持顯式opt-in,是一種值得使用的方式。 定制點并非一個簡單的概念,若是看不懂以上例子,跳過便是。 下面再來看其他的使用場景。 Deducing this還可以用來解決根據closure類型完美轉發Lambda捕獲參數的問題。 亦即,如果Lambda函數的類型為左值,那么捕獲的參數就以左值轉發;如果為右值,那么就以右值轉發。下面是一個例子:
1#include
2#include
3#include//forstd::forward_like
4
5autoget_message(){
6return42;
7}
8
9structScheduler{
10autosubmit(auto&&m){
11std::cout<::value<"
";
13????????std::cout?<::value<"
";
14????????return?m;
15????}
16};
17
18int?main()?{
19????Scheduler?scheduler;
20????auto?callback?=?[m=get_message(),?&scheduler](this?auto&&?self)?->bool{
21returnscheduler.submit(std::forward_like(m));
22};
23callback();//retry(callback)
24std::move(callback)();//try-or-fail(rvalue)
25}
26
27//Output:
28//true
29//false
30//false
31//true
若是沒有Deducing this,那么將無法簡單地完成這個操作。 另一個用處是可以將this以值形式傳遞,對于小對象來說,可以提高性能。 一個例子:
1structS{
2intdata_;
3intfoo();//implicitthispointer
4//intfoo(thisS);//Passthisbyvalue
5};
6
7intmain(){
8Ss{42};
9returns.foo();
10}
11
12// implicit this pointer生成的匯編代碼:
13//subrsp,40;00000028H
14//learcx,QWORDPTRs$[rsp]
15//movDWORDPTRs$[rsp],42;0000002aH
16//callintS::foo(void);S::foo
17//addrsp,40;00000028H
18//ret0
19
20// Pass this by value生成的匯編代碼:
21//movecx,42;0000002aH
22//jmpstaticintS::foo(thisS);S::foo
對于隱式的this指針,生成的匯編代碼需要先分配棧空間,保存this指針到rcx寄存器中,再將42賦值到data_中,然后調用foo(),最后平棧。 而以值形式傳遞this,則無需那些操作,因為值傳遞的this不會影響s變量,中間的步驟都可以被優化掉,也不再需要分配和平棧操作,所以可以直接將42保存到寄存器當中,再jmp到foo()處執行。 Deducing this是個單獨就可寫篇四五星難度文章的特性,用處很多,值得深入探索的地方也很多,所以即便是概述這部分也寫得比較多。
2Monadicstd::optional(P0798R8)
P0798提議為std::optional增加三個新的成員:map(), and_then()和or_else()。 功能分別為:
map:對optional的值應用一個函數,返回optional中wrapped的結果。若是optional中沒有值,返回一個空的optional;
and_then:組合使用返回optional的函數;
or_else:若是有值,返回optional;若是無值,則調用傳入的函數,在此可以處理錯誤。
在R2中map()被重命名為transform(),因此實際新增的三個函數為transform(),and_then()和or_else()。 這些函數主要是避免手動檢查optional值是否有效,比如:
1//Before
2if(opt_string){
3std::optionali=stoi(*opt_string);
4}
5
6//After
7std::optionali=opt_string.and_then(stoi);
一個使用的小例子:
1//chainaseriesoffunctionsuntilthere'sanerror
2std::optionalopt_string("10");
3std::optionali=opt_string
4.and_then(std::stoi)
5.transform([](autoi){returni*2;});
錯誤的情況:
1//fails,transformnotcalled,j==nullopt
2std::optionalopt_string_bad("abcd");
3std::optionalj=opt_string_bad
4.and_then(std::stoi)
5.transform([](autoi){returni*2;});
目前GCC 12,Clang 14,MSVC v19.32已經支持該特性。
3std::expected(P0323)
該特性用于解決錯誤處理的問題,增加了一個新的頭文件。 錯誤處理的邏輯關系為條件關系,若正確,則執行A邏輯;若失敗,則執行B邏輯,并需要知道確切的錯誤信息,才能對癥下藥。 當前的常用方式是通過錯誤碼或異常,但使用起來還是多有不便。 std::expected表示期望,算是std::variant和std::optional的結合,它要么保留T(期望的類型),要么保留E(錯誤的類型),它的接口又和std::optional相似。 一個簡單的例子:
1enumclassStatus:uint8_t{
2Ok,
3connection_error,
4no_authority,
5format_error,
6};
7
8boolconnected(){
9returntrue;
10}
11
12boolhas_authority(){
13returnfalse;
14}
15
16boolformat(){
17returnfalse;
18}
19
20std::expectedread_data(){
21if(!connected())
22returnstd::unexpected{Status::connection_error};
23if(!has_authority())
24returnstd::unexpected{Status::no_authority};
25if(!format())
26returnstd::unexpected{Status::format_error};
27
28return{"myexpectedtype"};
29}
30
31
32intmain(){
33autoresult=read_data();
34if(result){
35std::cout<
這種方式無疑會簡化錯誤處理的操作。
該特性目前在GCC 12,Clang 16(還未發布),MSVC v19.33已經實現。
4MultidimensionalArrays(P2128)
這個特性用于訪問多維數組,之前C++operator[]只支持訪問單個下標,無法訪問多維數組。
因此要訪問多維數組,以前的方式是:
重載operator(),于是能夠以m(1, 2)來訪問第1行第2個元素。但這種方式容易和函數調用產生混淆;
重載operator[],并以std::initializer_list作為參數,然后便能以m[{1, 2}]來訪問元素。但這種方式看著別扭。
鏈式鏈接operator[],然后就能夠以m[1][2]來訪問元素。同樣,看著別扭至極。
定義一個at()成員,然后通過at(1, 2)訪問元素。同樣不方便。
感謝該提案,在C++23,我們終于可以通過m[1, 2]這種方式來訪問多維數組。
一個例子:
1template
2structmatrix{
3T&operator[](constsize_tr,constsize_tc)noexcept{
4returndata_[r*C+c];
5}
6
7constT&operator[](constsize_tr,constsize_tc)constnoexcept{
8returndata_[r*C+c];
9}
10
11private:
12std::arraydata_;
13};
14
15
16intmain(){
17matrixm;
18m[0,0]=0;
19m[0,1]=1;
20m[1,0]=2;
21m[1,1]=3;
22
23for(autoi=0;i2;?++i)?{
24????????for?(auto?j?=?0;?j?2;?++j)?{
25????????????std::cout?<
該特性目前在GCC 12和Clang 15以上版本已經支持。
5if consteval(P1938)
該特性是關于immediate function的,即consteval function。 解決的問題其實很簡單,在C++20,consteval function可以調用constexpr function,而反過來卻不行。
1constevalautobar(intm){
2returnm*6;
3}
4
5constexprautofoo(intm){
6returnbar(m);
7}
8
9
10intmain(){
11[[maybe_unused]]autores=foo(42);
12}
以上代碼無法編譯通過,因為constexpr functiong不是強保證執行于編譯期,在其中自然無法調用consteval function。 但是,即便加上if std::is_constant_evaluated()也無法編譯成功。
1constexprautofoo(intm){
2if(std::is_constant_evaluated()){
3returnbar(m);
4}
5return42;
6}
這就存在問題了,P1938通過if consteval修復了這個問題。在C++23,可以這樣寫:
1constexprautofoo(intm){
2ifconsteval{
3returnbar(m);
4}
5return42;
6}
該特性目前在GCC 12和Clang 14以上版本已經實現。
6FormattedOutput(P2093)
該提案就是std::print(),之前已經說過,這里再簡單地說下。
標準cout的設計非常糟糕,具體表現在:
可用性差,基本沒有格式化能力;
會多次調用格式化I/0函數;
默認會同步標準C,性能低;
內容由參數交替組成,在多線程環境,內容會錯亂顯示;
二進制占用空間大;
……
隨著Formatting Library加入C++20,已在fmt庫中使用多年的fmt::print()加入標準也是順理成章。
格式化輸出的目標是要滿足:可用性、Unicode編碼支持、良好的性能,與較小的二進制占用空間。為了不影響現有代碼,該特性專門加了一個新的頭文件,包含兩個主要函數:
1#include
2
3intmain(){
4constchar*world="world";
5std::print("Hello{}",world);//doesn'tprintanewline
6std::println("Hello{}",world);//printanewline
7}
這對cout來說絕對是暴擊,std::print的易用性和性能簡直完爆它。 其語法就是Formatting Library的格式化語法。 性能對比:
----------------------------------------------------------
BenchmarkTimeCPUIterations
----------------------------------------------------------
printf87.0ns86.9ns7834009
ostream255ns255ns2746434
print78.4ns78.3ns9095989
print_cout89.4ns89.4ns7702973
print_cout_sync91.5ns91.4ns7903889
結果顯示,printf與print幾乎要比cout快三倍,print默認會打印到stdout。當打印到cout并同步標準C的流時(print_cout_sync),print大概要快14%;當不同步標準C的流時(print_cout),依舊要快不少。 遺憾的是,該特性目前沒有編譯器支持。
7FormattingRanges(P2286)
同樣屬于Formatting大家族,該提案使得我們能夠格式化輸出Ranges。 也就是說,我們能夠寫出這樣的代碼:
importstd;
automain()->int{
std::vectorvec{1,2,3};
std::print("{}
",vec);//Output:[1,2,3]
}
這意味著再也不用迭代來輸出Ranges了。 這是非常有必要的,考慮一個簡單的需求:文本分割。 Python的實現:
1print("howyoudoing".split(""))
2
3#Output:
4#['how','you','doing']
Java的實現:
1importjava.util.Arrays;
2
3classMain{
4publicstaticvoidmain(Stringargs[]){
5System.out.println("howyoudoing".split(""));
6System.out.println(Arrays.toString("howyoudoing".split("")));
7}
8}
9
10//Output:
11//[Ljava.lang.String;@2b2fa4f7
12//[how,you,doing]
Rust的實現:
1useitertools::Itertools;
2
3fnmain(){
4println!("{:?}","Howyoudoing".split(''));
5println!("[{}]","Howyoudoing".split('').format(","));
6println!("{:?}","Howyoudoing".split('').collect::>());
7}
8
9//Output:
10//Split(SplitInternal{start:0,end:13,matcher:CharSearcher{haystack:"Howyoudoing",finger:0,finger_back:13,needle:'',utf8_size:1,utf8_encoded:[32,0,0,0]},allow_trailing_empty:true,finished:false})
11//[How,you,doing]
12//["How","you","doing"]
JS的實現:
1console.log('Howyoudoing'.split(''))
2
3//Output:
4//["How","you","doing"]
Go的實現:
1packagemain
2import"fmt"
3import"strings"
4
5funcmain(){
6fmt.Println(strings.Split("Howyoudoing",""));
7}
8
9//Output:
10//[Howyoudoing]
Kotlin的實現:
1funmain(){
2println("Howyoudoing".split(""));
3}
4
5//Output:
6//[How,you,doing]
C++的實現:
1intmain(){
2std::string_viewcontents{"Howyoudoing"};
3
4autowords=contents
5|std::split('')
6|std::transform([](auto&&str){
7returnstd::string_view(&*str.begin(),std::distance(str));
8});
9
10std::cout<"[";
11????char?const*?delim?=?"";
12????for?(auto?word?:?words)?{
13????????std::cout?<
借助fmt,可以簡化代碼:
1intmain(){
2std::string_viewcontents{"Howyoudoing"};
3
4autowords=contents
5|std::split('')
6|std::transform([](auto&&str){
7returnstd::string_view(&*str.begin(),std::distance(str));
8});
9
10fmt::print("{}
",words);
11fmt::print("<<{}>>",fmt::join(words,"--"));
12
13}
14
15//Output:
16//["How","you","doing"]
17//<>
因為views::split()返回的是一個subrange,因此需要將其轉變成string_view,否則,輸出將為:
1intmain(){
2std::string_viewcontents{"Howyoudoing"};
3
4autowords=contents|std::split('');
5
6fmt::print("{}
",words);
7fmt::print("<<{}>>",fmt::join(words,"--"));
8
9}
10
11//Output:
12//[[H,o,w],[y,o,u],[d,o,i,n,g]]
13//<<['H',?'o',?'w']--['y',?'o',?'u']--['d',?'o',?'i',?'n',?'g']>>
總之,這個特性將極大簡化Ranges的輸出,是值得興奮的特性之一。
該特性目前沒有編譯器支持。
7import std(P2465)
C++20模塊很難用的一個原因就是標準模塊沒有提供,因此這個特性的加入是自然趨勢。
現在,可以寫出這樣的代碼:
1importstd;
2
3intmain(){
4std::print("Hellostandardlibrarymodules!
");
5}
性能對比:

如何你是混合C和C++,那可以使用std.compat module,所有的C函數和標準庫函數都會包含進來。
目前基本沒有編譯器支持此特性。
8out_ptr(P1132r8)
23新增了兩個對于指針的抽象類型,std::out_ptr_t和std::inout_ptr_t,兩個新的函數std::out_ptr()和std::inout_ptr()分別返回這兩個類型。 主要是在和C API交互時使用的,一個例子對比一下:
1//Before
2intold_c_api(int**);
3
4intmain(){
5autoup=std::make_unique(5);
6
7int*up_raw=up.release();
8if(intec=foreign_resetter(&up)){
9returnec;
10}
11
12up.reset(up_raw);
13}
14
15////////////////////////////////
16//After
17intold_c_api(int**);
18
19intmain(){
20autoup=std::make_unique(5);
21
22if(intec=foreign_resetter(std::inout_ptr(up))){
23returnec;
24}
25
26//*upisstillvalid
27}
該特性目前在MSVC v19.30支持。
9auto(x) decay copy(P0849)
該提案為auto又增加了兩個新語法:auto(x)和auto{x}。兩個作用一樣,只是寫法不同,都是為x創建一份拷貝。
為什么需要這么個東西?
看一個例子:
1voidbar(constauto&);
2
3voidfoo(constauto¶m){
4autocopy=param;
5bar(copy);
6}
foo()中調用bar(),希望傳遞一份param的拷貝,則我們需要單獨多聲明一個臨時變量。或是這樣:
1voidfoo(constauto¶m){
2bar(std::decay_t{param});
3}
這種方式需要手動去除多余的修飾,只留下T,要更加麻煩。 auto(x)就是內建的decay copy,現在可以直接這樣寫:
1voidfoo(constauto¶m){
2bar(auto{param});
3}
大家可能還沒意識到其必要性,來看提案當中更加復雜一點的例子。
1voidpop_front_alike(auto&container){
2std::erase(container,container.front());
3}
4
5intmain(){
6std::vectorfruits{"apple","apple","cherry","grape",
7"apple","papaya","plum","papaya","cherry","apple"};
8pop_front_alike(fruits);
9
10fmt::print("{}
",fruits);
11}
12
13//Output:
14//["cherry","grape","apple","papaya","plum","papaya","apple"]
請注意該程序的輸出,是否如你所想的一樣。若沒有發現問題,請讓我再提醒一下:pop_front_alike()要移除容器中所有跟第1個元素相同的元素。
因此,理想的結果應該為:
["cherry","grape","papaya","plum","papaya","cherry"]
是哪里出了問題呢?讓我們來看看gcc std::erase()的實現:
1template
2_ForwardIterator
3__remove_if(_ForwardIterator__first,_ForwardIterator__last,
4_Predicate__pred)
5{
6__first=std::__find_if(__first,__last,__pred);
7if(__first==__last)
8return__first;
9_ForwardIterator__result=__first;
10++__first;
11for(;__first!=__last;++__first)
12if(!__pred(__first)){
13*__result=_GLIBCXX_MOVE(*__first);
14++__result;
15}
16
17return__result;
18}
19
20template
21inlinetypenamevector<_Tp,?_Alloc>::size_type
22erase(vector<_Tp,?_Alloc>&__cont,const_Up&__value)
23{
24constauto__osz=__cont.size();
25__cont.erase(std::remove(__cont.begin(),__cont.end(),__value),
26__cont.end());
27return__osz-__cont.size();
28}
std::remove()最終調用的是remove_if(),因此關鍵就在這個算法里面。這個算法每次會比較當前元素和欲移除元素,若不相等,則用當前元素覆蓋當前__result迭代器的值,然后__result向后移一位。重復這個操作,最后全部有效元素就都跑到__result迭代器的前面去了。
問題出在哪里呢?欲移除元素始終指向首個元素,而它會隨著元素覆蓋操作被改變,因為它的類型為const T&。
此時,必須重新copy一份值,才能得到正確的結果。
故將代碼小作更改,就能得到正確的結果。
voidpop_front_alike(auto&container){
autocopy=container.front();
std::erase(container,copy);
}
然而這種方式是非常反直覺的,一般來說這兩種寫法的效果應該是等價的。
我們將copy定義為一個單獨的函數,表達效果則要好一點。
autocopy(constauto&value){
returnvalue;
}
voidpop_front_alike(auto&container){
std::erase(container,copy(container.front()));
}
而auto{x}和auto(x),就相當于這個copy()函數,只不過它是內建到語言里面的而已。
10Narrowingcontextualconversions to bool
這個提案允許在static_assert和if constexpr中從整形轉換為布爾類型。
以下表格就可以表示所有內容。
Before
After
if constexpr(bool(flags & Flags::Exec))
if constexpr(flags & Flags::Exec)
if constexpr(flags & Flags::Exec != 0)
if constexpr(flags & Flags::Exec)
static_assert(N % 4 != 0);
static_assert(N % 4);
static_assert(bool(N));
static_assert(N);
對于嚴格的C++編譯器來說,以前在這種情境下int無法向下轉換為bool,需要手動強制轉換,C++23這一情況得到了改善。
目前在GCC 9和Clang 13以上版本支持該特性。
11forward_like(P2445)
這個在Deducing this那節已經使用過了,是同一個作者。 使用情境讓我們回顧一下這個例子:
1autocallback=[m=get_message(),&scheduler](thisauto&&self)->bool{
2returnscheduler.submit(std::forward_like(m));
3};
4
5callback();//retry(callback)
6std::move(callback)();//try-or-fail(rvalue)
std::forward_like加入到了中,就是根據模板參數的值類別來轉發參數。
如果closure type為左值,那么m將轉發為左值;如果為右值,將轉發為右值。
聽說Clang 16和MSVC v19.34支持該特性,但都尚未發布。
12#eifdef and #eifndef(P2334)
這兩個預處理指令來自WG14(C的工作組),加入到了C23。C++為了兼容C,也將它們加入到了C++23。
也是一個完善工作。
#ifdef和#ifndef分別是#if defined()和#if !defined()的簡寫,而#elif defined()和#elif !defined()卻并沒有與之對應的簡寫指令。因此,C23使用#eifdef和#eifndef來補充這一遺漏。
總之,是兩個非常簡單的小特性。目前已在GCC 12和Clang 13得到支持。
13#warning(P2437)
#warning是主流編譯器都會支持的一個特性,最終倒逼C23和C++23也加入了進來。 這個小特性可以用來產生警告信息,與#error不同,它并不會停止翻譯。 用法很簡單:
1#ifndefFOO
2#warning"FOOdefined,performancemightbelimited"
3#endif
目前MSVC不支持該特性,其他主流編譯器都支持。
14constexpr std::unique_ptr(P2273R3)
std::unique_ptr也支持編譯期計算了,一個小例子:
1constexprautofun(){
2autop=std::make_unique(4);
3return*p;
4}
5
6intmain(){
7constexprautoi=fun();
8static_assert(4==i);
9}
目前GCC 12和MSVC v19.33支持該特性。
15improving string and string_view(P1679R3, P2166R1, P1989R2, P1072R10, P2251R1)
string和string_view也獲得了一些增強,這里簡單地說下。
P1679為二者增加了一個contain()函數,小例子:
1std::stringstr("dummytext");
2if(str.contains("dummy")){
3//dosomething
4}
目前GCC 11,Clang 12,MSVC v19.30支持該特性。 P2166使得它們從nullptr構建不再產生UB,而是直接編譯失敗。
1std::strings{nullptr};//error!
2std::string_viewsv{nullptr};//error!
目前GCC 12,Clang 13,MSVC v19.30支持該特性。 P1989是針對std::string_view的,一個小例子搞定:
1intmain(){
2std::vectorv{'a','b','c'};
3
4//Before
5std::string_viewsv(v.begin(),v.end());
6
7//After
8std::string_viewsv23{v};
9}
以前無法直接從Ranges構建std::string_view,而現在支持這種方式。
該特性在GCC 11,Clang 14,MSVC v19.30已經支持。
P1072為string新增了一個成員函數:
1template
2constexprvoidresize_and_overwrite(size_typecount,Operationop);
可以通過提案中的一個示例來理解:
1intmain(){
2std::strings{"Food:"};
3
4s.resize_and_overwrite(10,[](char*buf,intn){
5returnstd::find(buf,buf+n,':')-buf;
6});
7
8std::cout<
主要是兩個操作:改變大小和覆蓋內容。第1個參數是新的大小,第2個參數是一個op,用于設置新的內容。
然后的邏輯是:
如果maxsize <= s.size(),刪除最后的size()-maxsize個元素;
如果maxsize > s.size(),追加maxsize-size()個默認元素;
調用erase(begin() + op(data(), maxsize), end())。
這里再給出一個例子,可以使用上面的邏輯來走一遍,以更清晰地理解該函數。
1constexprstd::string_viewfruits[]{"apple","banana","coconut","date","elderberry"};
2std::strings1{"Food:"};
3
4s1.resize_and_overwrite(16,[sz=s1.size()](char*buf,std::size_tbuf_size){
5constautoto_copy=std::min(buf_size-sz,fruits[1].size());//6
6std::memcpy(buf+sz,fruits[1].data(),to_copy);//append"banana"tos1.
7returnsz+to_copy;//6+6
8});
9
10std::cout<
注意一下,maxsize是最大的可能大小,而op返回才是實際大小,因此邏輯的最后才有一個erase()操作,用于刪除多余的大小。
這個特性在GCC 12,Clang 14,MSVC v19.31已經實現。
接著來看P2251,它更新了std::span和std::string_view的約束,從C++23開始,它們必須滿足TriviallyCopyable Concept。
主流編譯器都支持該特性。
最后來看P0448,其引入了一個新的頭文件。
大家都知道,stringstream現在被廣泛使用,可以將數據存儲到string或vector當中,但這些容器當數據增長時會發生「挪窩」的行為,若是不想產生這個開銷呢?
提供了一種選擇,你可以指定固定大小的buffer,它不會重新分配內存,但要小心數據超出buffer大小,此時內存的所有權在程序員這邊。
一個小例子:
1#defineASSERT_EQUAL(a,b)assert(a==b)
2#defineASSERT(a)assert(a)
3
4intmain(){
5charinput[]="102030";
6std::ispanstreamis{std::span{input}};
7inti;
8
9is>>i;
10ASSERT_EQUAL(10,i);
11
12is>>i;
13ASSERT_EQUAL(20,i);
14
15is>>i;
16ASSERT_EQUAL(30,i);
17
18is>>i;
19ASSERT(!is);
20}
目前GCC 12和MSVC v19.31已支持該特性。
16staticoperator()(P1169R4)
因為函數對象,Lambdas使用得越來越多,經常作為標準庫的定制點使用。這種函數對象只有一個operator (),如果允許聲明為static,則可以提高性能。
至于原理,大家可以回顧一下Deducing this那節的Pass this by value提高性能的原理。明白靜態函數和非靜態函數在重載決議中的區別,大概就能明白這點。
順便一提,由于mutidimensional operator[]如今已經可以達到和operator()一樣的效果,它也可以作為一種新的函數語法,你完全可以這樣調用foo[],只是不太直觀。因此,P2589也提議了static operator[]。
17std::unreachable(P0627R6)
當我們知道某個位置是不可能執行到,而編譯器不知道時,使用std::unreachalbe可以告訴編譯器,從而避免沒必要的運行期檢查。 一個簡單的例子:
1voidfoo(inta){
2switch(a){
3case1:
4//dosomething
5break;
6case2:
7//dosomething
8break;
9default:
10std::unreachable();
11}
12}
13
14boolis_valid(inta){
15returna==1||a==2;
16}
17
18intmain(){
19inta=0;
20while(!is_valid(a))
21std::cin>>a;
22foo(a);
23}
該特性位于,在GCC 12,Clang 15和MSVC v19.32已經支持。
18std::to_underlying(P1682R3)
同樣位于,用于枚舉到其潛在的類型,相當于以下代碼的語法糖:
static_cast>(e);
一個簡單的例子就能看懂:
1voidprint_day(inta){
2fmt::print("{}
",a);
3}
4
5enumclassDay:std::uint8_t{
6Monday=1,
7Tuesday,
8Wednesday,
9Thursday,
10Friday,
11Saturday,
12Sunday
13};
14
15
16intmain(){
17//Before
18print_day(static_cast>(Day::Monday));
19
20//C++23
21print_day(std::Friday));
22}
的確很簡單吧!
該特性目前在GCC 11,Clang 13,MSVC v19.30已經實現。
19std::byteswap(P1272R4)
位于,顧名思義,是關于位操作的。
同樣,一個例子看懂:
1template
2voidprint_hex(Tv)
3{
4for(std::size_ti=0;i>=8)
5{
6fmt::print("{:02X}",static_cast(T(0xFF)&v));
7}
8std::cout<'
';
9????}
10
11int?main()
12{
13????unsigned?char?a?=?0xBA;
14????print_hex(a);?????????????????????//?BA
15????print_hex(std::byteswap(a));??????//?BA
16????unsigned?short?b?=?0xBAAD;
17????print_hex(b);?????????????????????//?AD?BA
18????print_hex(std::byteswap(b));??????//?BA?AD
19????int?c?=?0xBAADF00D;
20????print_hex(c);?????????????????????//?0D?F0?AD?BA
21????print_hex(std::byteswap(c));??????//?BA?AD?F0?0D
22????long?long?d?=?0xBAADF00DBAADC0FE;
23????print_hex(d);?????????????????????//?FE?C0?AD?BA?0D?F0?AD?BA
24????print_hex(std::byteswap(d));??????//?BA?AD?F0?0D?BA?AD?C0?FE
25}
可以看到,其作用是逆轉整型的字節序。當需要在兩個不同的系統傳輸數據,它們使用不同的字節序時(大端小端),這個工具就會很有用。
該特性目前在GCC 12,Clang 14和MSVC v19.31已經支持。
20std::stacktrace(P0881R7, P2301R1)
位于,可以讓我們捕獲調用棧的信息,從而知道哪個函數調用了當前函數,哪個調用引發了異常,以更好地定位錯誤。
一個小例子:
1voidfoo(){
2autotrace=std::current();
3std::cout<
輸出如下。
0#foo()at/app/example.cpp:5
1#at/app/example.cpp:10
2#at:0
3#at:0
4#
注意,目前GCC 12.1和MSVC v19.34支持該特性,GCC編譯時要加上-lstdc++_libbacktrace參數。
std::stacktrace是std::basic_stacktrace使用默認分配器時的別名,定義為:
usingstacktrace=std::basic_stacktrace>;
而P2301,則是為其添加了PMR版本的別名,定義為:
namespacepmr{
usingstacktrace=
std::basic_stacktrace>;
}
于是使用起來就會方便一些。
1//Before
2charbuffer[1024];
3
4std::monotonic_buffer_resourcepool{
5std::data(buffer),std::size(buffer)};
6
7std::basic_stacktrace<
8????std::polymorphic_allocator>
9trace{&pool};
10
11//After
12charbuffer[1024];
13
14std::monotonic_buffer_resourcepool{
15std::data(buffer),std::size(buffer)};
16
17std::stacktracetrace{&pool};
這個特性到時再單獨寫篇文章,在此不細論。
21Attributes(P1774R8, P2173R1, P2156R1)
Attributes在C++23也有一些改變。
首先,P1774新增了一個Attribute [[assume]],其實在很多編譯器早已存在相應的特性,例如__assume()(MSVC, ICC),__builtin_assume()(Clang)。GCC沒有相關特性,所以它也是最早實現標準[[assume]]的,目前就GCC 13支持該特性(等四月發布,該版本對Rangs的支持也很完善)。
現在可以通過宏來玩:
1#ifdefined(__clang__)
2#defineASSUME(expr)__builtin_assume(expr)
3#elifdefined(__GNUC__)&&!defined(__ICC)
4#defineASSUME(expr)if(expr){}else{__builtin_unreachable();}
5#elifdefined(_MSC_VER)||defined(__ICC)
6#defineASSUME(expr)__assume(expr)
7#endif
論文當中的一個例子:
1voidlimiter(float*data,size_tsize){
2ASSUME(size>0);
3ASSUME(size%32==0);
4
5for(size_ti=0;i
第一個是假設size永不為0,總是正數;第二個告訴編譯器size總是32的倍數;第三個表明數據不是NaN或無限小數。
這些假設不會被評估,也不會被檢查,編譯器假設其為真,依此優化代碼。若是假設為假,可能會產生UB。
使用該特性與否編譯產生的指令數對比結果如下圖。 
其次,P2173使得可以在Lambda表達式上使用Attributes,一個例子:
1//Anyattributesospecifieddoesnotappertaintothefunction
2//calloperatororoperatortemplateitself,butitstype.
3autolam=[][[nodiscard]]->int{return42;};
4
5intmain()
6{
7lam();
8}
9
10//Output:
11//:Infunction'intmain()':
12//8:warning:ignoringreturnvalueof'',declaredwithattribute'nodiscard'[-Wunused-result]
13//12|lam();
14//|~~~^~
15//12:note:declaredhere
16//8|autolam=[][[nodiscard]]->int{return42;};
17//|^
注意,Attributes屬于closure type,而不屬于operator ()。
因此,有些Attributes不能使用,比如[[noreturn]],它表明函數的控制流不會返回到調用方,而對于Lambda函數是會返回的。
除此之外,此處我還展示了C++的另一個Lambda特性。
在C++23之前,最簡單的Lambda表達式為[](){},而到了C++23,則是[]{},可以省略無參時的括號,這得感謝P1102。
早在GCC 9就支持Attributes Lambda,Clang 13如今也支持。
最后來看P2156,它移除了重復Attributes的限制。
簡單來說,兩種重復Attributes的語法評判不一致。例子:
1//Notallow
2[[nodiscard,nodiscard]]autofoo(){
3return42;
4}
5
6//Allowed
7[[nodiscard]][[nodiscard]]autofoo(){
8return42;
9}
為了保證一致性,去除此限制,使得標準更簡單。
什么時候會出現重復Attributes,看論文怎么說:
During this discussion, it was brought up that
the duplication across attribute-specifiers are to support cases where macros are used to conditionally add attributes to an
attribute-specifier-seq, however it is rare for macros to be used to generate attributes within the same attribute-list. Thus,
removing the limitation for that reason is unnecessary.
在基于宏生成的時候可能會出現重復Attributes,因此允許第二種方式;宏生成很少使用第一種形式,因此標準限制了這種情況。但這卻并沒有讓標準變得更簡單。因此,最終移除了該限制。
目前使用GCC 11,Clang 13以上兩種形式的結果將保持一致。
22Lambdas(P1102R2, P2036R3, P2173R1)
Lambdas表達式在C++23也再次迎來了一些新特性。
像是支持Attributes,可以省略(),這在Attributes這一節已經介紹過,不再贅述。
另一個新特性是P2036提的,接下來主要說說這個。
這個特性改變了trailing return types的Name Lookup規則,為什么?讓我們來看一個例子。
1doublej=42.0;
2//...
3autocounter=[j = 0]()mutable->decltype(j){
4returnj++;
5};
counter最終的類型是什么?是int嗎?還是double?其實是double。
無論捕獲列表當中存在什么值,trailing return type的Name Lookup都不會查找到它。
這意味著單獨這樣寫將會編譯出錯:
1autocounter=[j=0]()mutable->decltype(j){
2returnj++;
3};
4
5//Output:
6//44:error:useofundeclaredidentifier'j'
7//autocounter=[j=0]()mutable->decltype(j){
8//^
因為對于trailing return type來說,根本就看不見捕獲列表中的j。
以下例子能夠更清晰地展示這個錯誤:
1templateintbar(int&,T&&);//#1
2templatevoidbar(intconst&,T&&);//#2
3
4inti;
5autof=[=](auto&&x)->decltype(bar(i,x)){
6returnbar(i,x);
7}
8
9f(42);//error
在C++23,trailing return types的Name Lookup規則變為:在外部查找之前,先查找捕獲列表,從而解決這個問題。 目前沒有任何編譯器支持該特性。
23Literal suffixes for (signed) size_t(P0330R8)
這個特性為std::size_t增加了后綴uz,為signed std::size_t加了后綴z。
有什么用呢?看個例子:
1#include
2
3intmain(){
4std::vectorv{0,1,2,3};
5for(autoi=0u,s=v.size();i
這代碼在32 bit平臺編譯能夠通過,而放到64 bit平臺編譯,則會出現錯誤:
1(5):errorC3538:inadeclarator-list'auto'mustalwaysdeducetothesametype
2(5):note:couldbe'unsignedint'
3(5):note:or'unsigned__int64'
在32 bit平臺上,i被推導為unsigned int,v.size()返回的類型為size_t。而size_t在32 bit上為unsigned int,而在64 bit上為unsigned long long。(in MSVC)
因此,同樣的代碼,從32 bit切換到64 bit時就會出現錯誤。
而通過新增的后綴,則可以保證這個代碼在任何平臺上都能有相同的結果。
1#include
2
3intmain(){
4std::vectorv{0,1,2,3};
5for(autoi=0uz,s=v.size();i
如此一來就解決了這個問題。
目前GCC 11和Clang 13支持該特性。
24std::mdspan(P0009r18)
std::mdspan是std::span的多維版本,因此它是一個多維Views。 看一個例子,簡單了解其用法。
1intmain()
2{
3std::vectorv={1,2,3,4,5,6,7,8,9,10,11,12};
4
5//Viewdataascontiguousmemoryrepresenting2rowsof6intseach
6automs2=std::mdspan(v.data(),2,6);
7//Viewthesamedataasa3Darray2x3x2
8automs3=std::mdspan(v.data(),2,3,2);
9
10//writedatausing2Dview
11for(size_ti=0;i!=ms2.extent(0);i++)
12for(size_tj=0;j!=ms2.extent(1);j++)
13ms2[i,j]=i*1000+j;
14
15//readbackusing3Dview
16for(size_ti=0;i!=ms3.extent(0);i++)
17{
18fmt::print("slice@i={}
",i);
19for(size_tj=0;j!=ms3.extent(1);j++)
20{
21for(size_tk=0;k!=ms3.extent(2);k++)
22fmt::print("{}",ms3[i,j,k]);
23fmt::print("
");
24}
25}
26}
目前沒有編譯器支持該特性,使用的是https://raw.githubusercontent.com/kokkos/mdspan/single-header/mdspan.hpp實現的版本,所以在experimental下面。 ms2是將數據以二維形式訪問,ms3則以三維訪問,Views可以改變原有數據,因此最終遍歷的結果為:
1slice@i=0
201
323
445
5slice@i=1
610001001
710021003
810041005
這個特性值得剖析下其設計,這里不再深究,后面單獨出一篇文章。
25flat_map, flat_set(P0429R9, P1222R4)
C++23多了flat version的map和set:
flat_map
flat_set
flat_multimap
flat_multiset
過去的容器,有的使用二叉樹,有的使用哈希表,而flat版本的使用的連續序列的容器,更像是容器的適配器。
無非就是時間或空間復雜度的均衡,目前沒有具體測試,也沒有編譯器支持,暫不深究。
26總結
本篇已經夠長了,C++23比較有用的特性基本都包含進來了。
其中的另一個重要更新Ranges并沒有包含。讀至此,大家應該已經感覺到C++23在于完善,而不在于增加。沒有什么全新的東西,也沒什么太大的特性,那些就得等到C++26了。
大家喜歡哪些C++23特性?
審核編輯:湯梓紅
-
參數
+關注
關注
11文章
1869瀏覽量
33943 -
函數
+關注
關注
3文章
4417瀏覽量
67499 -
指針
+關注
關注
1文章
484瀏覽量
71843 -
C++
+關注
關注
22文章
2123瀏覽量
77110
原文標題:C++23 特性概覽
文章出處:【微信號:CPP開發者,微信公眾號:CPP開發者】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
LLVM clang 公開 -std=c++23
EIA 364 23C
CAT-D38999-DTS23C CAT-D38999-DTS23C 標準圓形連接器
C++23和C++26新標準的展望
LCD驅動控制AiP16C23,可替代合泰HT16C23
C++23特性概覽
評論