上個(gè)月,在CppCon2022上Herb Sutter介紹了他的一個(gè)處于實(shí)驗(yàn)中的新編譯器:CppFront。
他通過該編譯器來實(shí)踐一種潛在的C++替換語法,簡(jiǎn)稱為Cpp2,C++當(dāng)前語法則稱為Cpp1。Herb從2015年就著手設(shè)計(jì)該項(xiàng)目,如今推了出來,一時(shí)激起不小浪花,項(xiàng)目地址為https://github.com/hsutter/cppfront。官方描述如下:Cppfront is a experimental compiler from a potential C++ 'syntax 2' (Cpp2) to today's 'syntax 1' (Cpp1), to learn some things, prove out some concepts, and share some ideas. This compiler is a work in progress and currently hilariously incomplete… basic functions work, classes will be next, then metaclasses and lightweight exceptions.C++這是在自我破局,開辟第二曲線啊!標(biāo)準(zhǔn)是想再次實(shí)現(xiàn)C++11那種巨大的成功,使C++再次煥然一新,更加現(xiàn)代、簡(jiǎn)單、高效。(雖然Herb說這個(gè)項(xiàng)目只代表個(gè)人的實(shí)踐,但是好的點(diǎn)子標(biāo)準(zhǔn)也不會(huì)放過,而且是實(shí)踐可行的點(diǎn)子)其實(shí)近幾年C++的發(fā)展速度真不慢,許多特性遲久未入,只是不想剛引入就遭淘汰。當(dāng)你再次見到C++更新時(shí),很可能又會(huì)是像見到了一個(gè)新語言一樣,語法完全變化,近幾次的標(biāo)準(zhǔn)已有這個(gè)趨勢(shì)。下面我將提供一些Cpp2的例子,這些例子都是可以使用CppFront手動(dòng)編譯運(yùn)行的,大家可以看看它的語法。Cpp2的目標(biāo)可以概括為兩個(gè)詞:安全和簡(jiǎn)潔,語法也以此為導(dǎo)向。先來看一個(gè)最簡(jiǎn)單的例子:
main:()->int={ s:std::string="world"; std::cout<"Hello"<" "; }這就是使用純Cpp2編寫的代碼,所有的聲明都由原來的r-to-l變?yōu)榱薼-to-r。其實(shí)從trailing-return-type開始,C++已經(jīng)慢慢變成了這種l-to-r形式的語言。可以混合使用Cpp1和Cpp2的語法,但是對(duì)于純Cpp2語法,它可以隱式地引入C++ 23的"import std"模塊,因此無需手動(dòng)添加任何頭文件。接著來看一個(gè)文件讀寫的例子:
main:()->int={ s:std::string="Lily"; myfile:=fopen("dog","w"); myfile.fprintf("Hello%s! ",s.c_str()); myfile.fclose(); }這個(gè)代碼的確簡(jiǎn)單不少,而且fprintf(), fclose()是作為成員函數(shù)存在的,這意味著可以觸發(fā)IDE的智能提示,提升開發(fā)效率。下面是一個(gè)類型安全的例子:
main:()->int={ v:std::variant<int,double>=42.0; a:std::any="xyzzy"asstd::string; o:std::optional<int>=(); test_generic(3.14); test_generic(v); test_generic(a); test_generic(o); std::cout<" "; v=1; a=2; o=3; test_generic(42); test_generic(v); test_generic(a); test_generic(o); } test_generic:(x:_)={ std::cout <std::setw(30)<typeid(x).name() <"valueis" <std::string{ isint=std::to_string(xasint); isstd::string=xasstd::string; is_="notanintorastring"; } <" "; }Cpp2中函數(shù)不需要前置聲明,它具有順序無關(guān)性(其實(shí)是生成代碼的時(shí)候自動(dòng)提供前置聲明了)。其中的"_"是隱式模板的通配符,相當(dāng)于T,而inspect is as等等這些都是Pattern Matching的語法。在CppFront中,typeid().name()返回的類型名稱是可讀的,因此最終輸出如下圖。

main:()->int={ words:std::vector<std::string>= ("decorated","hello","world"); first:*std::string=words.front()&; last:*std::string=words.back()&; whilefirst<=?last?{ ????????print_and_decorate(first*); ????????first++;?//unsafe //first+1; //first[1]; //first~; //deletefirst; } } print_and_decorate:(thing:_)= std::cout<">>"<" ";當(dāng)試圖對(duì)指針使用這些操作時(shí),將產(chǎn)生編譯期錯(cuò)誤。上述代碼將報(bào)錯(cuò):
error:++-pointerarithmeticisillegal-usestd::spanorgsl::spaninstead它會(huì)推薦你使用更好的替代特性,有些特性甚至內(nèi)置為了原生特性,比如智能指針,它會(huì)推薦你使用unique.new
main:()->int={ cpp2::Bounds.set_handler(call_my_framework); words:std::vector<std::string>= ("decorated","hello","world"); s:std::span=words; print_and_decorate(s[3]); } print_and_decorate:(thing:_)= std::cout<">>"<" "; call_my_frameword:(msg:*constchar)= std::cout <"sendingerrortomyframework...[" <"] ";代碼中訪問s[3]越界,編譯時(shí)指定-s標(biāo)志,便可以幫你開啟邊界檢查,運(yùn)行之時(shí)進(jìn)行報(bào)錯(cuò)。此外,還可以通過第2行的代碼設(shè)置一個(gè)handler,來捕獲錯(cuò)誤。只有一個(gè)表達(dá)式的函數(shù),"{}"將作為可選項(xiàng),省略掉要更加簡(jiǎn)潔。 此外,Cpp2也支持初始化安全,對(duì)此,指針不可賦值為nullptr/0/NULL??匆粋€(gè)例子:
main:()->int={ cpp2::Bounds.set_handler(call_my_framework); p:*std::string; ifstd::rand()%2{ p=words.front()&; } //else{ //p=words.back()&; //} print_and_decorate(p*); } print_and_decorate:(thing:_)= std::cout<">>"<" ";例子中,p是一個(gè)指針,但是賦值語句沒有遍及各個(gè)分支,訪問之時(shí)肯定會(huì)存在錯(cuò)誤。因此它會(huì)直接產(chǎn)生編譯期錯(cuò)誤:
example.cpp2(4,5):error:localvariablepmustbeinitializedonbothbranchesorneitherbranch example.cpp2(6,5):error:"if"initializespon: branchstartingatline6 butnoton: implicitelsebranch ==>programviolatesinitializationsafetyguarantee-seepreviouserrors這個(gè)特性可以保證變量在使用前初始化。最后,還有一個(gè)有意思的特性用于函數(shù)多返回值,一個(gè)簡(jiǎn)單的例子:
f:()->(i:int,s:std::string)={ i=10; s="haha"; return; } automain()->int{ auto[a,b]=f(); std::cout<""<" "; }這個(gè)代碼混合了Cpp1和Cpp2。函數(shù)f()具有多個(gè)返回值,可以不用借用std::paire與std::tuple等等組件。實(shí)際上,它會(huì)自動(dòng)生成一個(gè)結(jié)構(gòu)體,作為函數(shù)的返回值。該例子生成的為:
structf__ret{ inti; std::strings; };這些例子都來自Herb的演講內(nèi)容,他說Cpp2會(huì)比原來的C++語法簡(jiǎn)單和安全10倍,想看的地址為:https://www.youtube.com/watch?v=ELeZAKCN4tY。Cpp2一直只是Herb個(gè)人的一個(gè)實(shí)驗(yàn)性產(chǎn)品,項(xiàng)目必然存在很多缺陷,但正是這些不斷的探索,為C++提供了更多可能。 大家覺得目前的Cpp2設(shè)計(jì)怎樣呢?有沒有哪些不錯(cuò)的點(diǎn)子?
審核編輯 :李倩
-
C++
+關(guān)注
關(guān)注
22文章
2119瀏覽量
75331 -
編譯器
+關(guān)注
關(guān)注
1文章
1662瀏覽量
50232
原文標(biāo)題:Herb Sutter 介紹一個(gè)處于他實(shí)驗(yàn)中的 C++ 新編譯器和一種潛在的 C++ 替換語法
文章出處:【微信號(hào):CPP開發(fā)者,微信公眾號(hào):CPP開發(fā)者】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
什么樣的代碼會(huì)被編譯器優(yōu)化
EE-88:使用21xx編譯器在C中初始化變量

Triton編譯器與GPU編程的結(jié)合應(yīng)用
Triton編譯器如何提升編程效率
Triton編譯器在高性能計(jì)算中的應(yīng)用
Triton編譯器的優(yōu)化技巧
Triton編譯器的優(yōu)勢(shì)與劣勢(shì)分析
Triton編譯器在機(jī)器學(xué)習(xí)中的應(yīng)用
Triton編譯器的常見問題解決方案
Triton編譯器支持的編程語言
Triton編譯器與其他編譯器的比較
Triton編譯器功能介紹 Triton編譯器使用教程
分享關(guān)于編譯器的科普
C7000優(yōu)化C/C++編譯器

評(píng)論