C++實用指南

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"C++發展得非常快!例如,C++標準的頁數從C++98\/03的879頁增加到了C++20的1834頁,多了近1000頁!更重要的是,C++每次修訂後,我們都會獲得幾十個新特性。你需要學習所有這些東西才能寫出好代碼嗎?如何在當今的C++世界中保持理智?"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"介紹"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"你可能知道C++是一種複雜的語言。我甚至發現了一整頁"},{"type":"link","attrs":{"href":"https:\/\/en.wikipedia.org\/wiki\/Criticism_of_C%2B%2B?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"Wiki"}]},{"type":"text","text":"是講對Cpp的批判的。現代C++甚至爲生態添加了更多內容。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"以下是我之前提到的規範頁數的完整數據:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"C++98\/03-879,"},{"type":"link","attrs":{"href":"http:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2005\/n1905.pdf?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"N1905"}]},{"type":"text","text":",2005年10月"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"C++11-1324,最後草案,"},{"type":"link","attrs":{"href":"http:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2012\/n3337.pdf?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"N3337"}]},{"type":"text","text":",2012年1月"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"C++14-1368,"},{"type":"link","attrs":{"href":"http:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2014\/n4296.pdf?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"最後草案"}]},{"type":"text","text":",2014年11月"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"C++17-1586,草案,"},{"type":"link","attrs":{"href":"http:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2016\/n4606.pdf?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"N4606"}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"C++20-1834,草案,"},{"type":"link","attrs":{"href":"https:\/\/timsong-cpp.github.io\/cppwp\/n4861\/draft.pdf?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"N4861"}]}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/ff\/ff995e6d8fc992d9ccc568e4232bc590.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"看起來C++17幾乎比C++98\/03“大”了80%,而C++的最新草案比C++03多了將近1000頁。你可以抱怨增加的這些複雜性,想學好所有這些東西也很困難。但這有那麼可怕嗎?面對這樣的情況,你能做些什麼?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先,我們來看看你在C++中可能會遇到的一些問題。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"一些問題"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"僅舉幾例:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"節奏太慢"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"節奏太快"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"特性的混亂\/複雜性"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"編譯時間慢"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"缺乏依賴管理"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們來仔細研究一下。"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"節奏太慢"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"2017年,我們迎來C++17。雖然每三年就迎來一個新標準是很棒的,但許多開發人員抱怨新版本並不是每個人都期待的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"很多特性:比如概念(concept)、模塊、範圍(range)、協程(co-routine)……都沒有被接受,我們至少需要再等三年才能讓它們進入規範。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在2020年,C++20已經準備就緒,並且這些重要特性將隨編譯器一起提供!但我們還是會抱怨合約(contract)還沒加進來,反射(reflection)、執行器(executor)或網絡(networking)仍在討論中。它們可能出現在C++23甚至更高版本中。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"看起來有些特性接受起來比較慢……而且總有東西值得抱怨。"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"節奏太快"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"像往常一樣,我們在這裏可能有兩種相互矛盾的意見。儘管對某些人來說升級節奏很慢,但對其他人來說卻很難跟上變化。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"你剛剛學習了C++11\/14……現在你就需要更新C++17的知識,然後C++20就在路上了。三年並不是那麼短的時間,但請記住,編譯器一致性、公司政策、團隊指南可能會以不同的節奏前進。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"你的公司是立即更新到最新的C++版本還是等待幾年?"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"特性的混淆\/複雜性"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"只需閱讀這條評論:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https:\/\/www.reddit.com\/r\/programming\/comments\/4zo5p7\/lvalues_and_rvalues_used_to_be_simple_things_now\/d6xs9db\/?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"CallMeDonk"}]},{"type":"text","text":":"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我喜歡C++。這是我的首選語言,但你必須承認,它對值類的“大雜燴”實現是很怪異的。包括我在內的大多數程序員更喜歡簡單的、定義明確的語言結構,而不是奇怪和複雜的語法。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"C++在各個方面都很清晰嗎?可能不是……"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"以下是一些可能難以理解並可能讓程序員糊塗的主題:"}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"移動語義"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"移動語義的原則非常明確:不要複製,而是嘗試“竊取”託管資源的內部結構,你應該獲得不錯的性能提升。但魔鬼都藏在細節中。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我不會寫很多通用代碼,所以幸運的是,我不必一直考慮移動語義。但是,當我遇到 move 和 const 時會很困惑——請參閱我上一篇關於該主題的"},{"type":"link","attrs":{"href":"https:\/\/www.cppstories.com\/2017\/01\/const-move-and-rvo.html?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"文章"}]},{"type":"text","text":"。我不相信所有C++開發人員都會理解這裏的規則。特別是你現在需要記住編譯器生成的六個默認操作:默認構造器、析構函數、複製構造器、移動構造器、賦值運算符和移動賦值運算符。"}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"Rvalues\/xvalues\/prvalues……myValues、fooValues"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"最後一個是我編的……但那麼多值類別實在太讓人頭疼了!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在C(或C++98\/03)中,你只需要知道左值與右值,現在它有點微妙了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"不過,問題是你是否需要記住它?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一些不錯的評論:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https:\/\/www.reddit.com\/r\/programming\/comments\/4zo5p7\/lvalues_and_rvalues_used_to_be_simple_things_now\/d6xh562\/?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"c0r3ntin"}]},{"type":"text","text":":"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這很複雜,但不是每天都能遇到。這個值可以address嗎?可以複製嗎?可以移動嗎?應該移動嗎?只有在極少數情況下,你才需要主動去澄清並充分理解它們。(模板化庫編寫、熱路徑等)。大多數時候C++並不比java或其他東西複雜。可悲的是大多數人都忘了這一點。C++可能是最複雜的語言,但是你可以編寫非常好的代碼而無需關心具體的細節。BigObject o=getBigObject();"}]}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"初始化"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"現在有18種方式(從C++17開始)!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"參閱:"},{"type":"link","attrs":{"href":"https:\/\/tartanllama.github.io\/c++\/2017\/01\/20\/initialization-is-bonkers\/?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"C++中的初始化是瘋狂的"}]},{"type":"text","text":";"},{"type":"link","attrs":{"href":"https:\/\/www.reddit.com\/r\/cpp\/comments\/5p5ed7\/initialization_in_c_is_bonkers\/?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"r\/cpp線程"}]}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"模板(和模板推導)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當我看到C++17的所有變更時,我很迷茫;關於模板的細節太多了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"同樣的情況發生在C++20中,我們迎來了一個重大且期待已久的改進:概念——它徹底改變了C++。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"然而,如果你想學習模板,一開始可能會不知所措。"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"ABI"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"隨着新特性列表的不斷增長,“從頭開始”修復C++設計中的舊問題可能是很誘人的主題。但這種語言的原則是不能破壞舊代碼,所以委員會非常嚴格,不喜歡改變已引入特性的路線。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這個問題沒有正確的答案,但無論如何,一個經過充分討論的主題要比倉促的舉動更好。"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"缺乏依賴管理工具"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們可以抱怨C++沒有“交​​付”一個很酷的依賴管理系統。但現實情況是,在可預見的未來,這可能都不會實現。擁有一個“標準”的包管理器是一個艱難的選擇,尤其是它必須處理如此多的可用C++的平臺和系統。"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"不夠安全"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"前段時間,你可以讀到提到這個問題的一些文章("},{"type":"link","attrs":{"href":"https:\/\/www.zdnet.com\/article\/chrome-70-of-all-security-bugs-are-memory-safety-issues\/?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"這篇"}]},{"type":"text","text":"和"},{"type":"link","attrs":{"href":"https:\/\/www.zdnet.com\/article\/microsoft-70-percent-of-all-security-bugs-are-memory-safety-issues\/?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"這篇"}]},{"type":"text","text":"):"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"谷歌工程師本週表示,Chrome代碼庫中大約70%的嚴重安全漏洞是內存管理和安全漏洞。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"微軟也是如此。由於大部分代碼是C或C++,所以每個人都指責C++不夠安全。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"其他問題?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"你在這種語言上遇到的主要問題都有哪些?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"到目前爲止,我們已經討論了一些問題……那麼如何應對它們呢?有機會解決這些問題嗎?"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"如何保持理智"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"沒有完美的編程語言;每種語言都有一些問題。以下是我關於如何處理現代C++問題的建議:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"保持樂觀"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用最佳指南"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用最好的工具"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"跟上最新進展"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"不要打開引擎蓋"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用你需要的"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"增量變更"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"最後的底線:你的舊代碼仍然安全並且可以編譯"}]}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"保持樂觀,語言在不斷髮展"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"沒有人願意使用舊的語法和結構來編寫代碼。我們已經看到很多關於C++11之前的舊版C++的抱怨。人們花了將近13年的時間(從主要的C++98算起,不包括次要的C++03)才提出新的主要版本:C++11。現在我們可以很高興回到了正軌,每三年都會有一些變化。歸根結底,你不能說你的語言已經死了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"雖然某些特性非常龐大,可能會帶來混亂或需要學習更多東西,但實際情況其實很簡單:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在C++03之後添加的1000個新頁面中的大部分用於標準庫。這意味着你可以使用更多助手和子系統,而無需查找第三方庫。這絕對會讓你的生活更輕鬆。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"對於移動語義,你可以依賴庫類型,因爲它們會爲你完成正確的工作。例如,你現在可以安全地返回"},{"type":"codeinline","content":[{"type":"text","text":"std::vector"}]},{"type":"text","text":"並確保它可能被移動甚至被刪除,而無需額外副本。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"至於模板,它變得越來越容易使用。概念讓代碼更安全,沒有像SFINAE這樣的技巧。更重要的是,我們有了"},{"type":"codeinline","content":[{"type":"text","text":"constexpr"}]},{"type":"text","text":"和"},{"type":"codeinline","content":[{"type":"text","text":"auto"}]},{"type":"text","text":",讓泛型代碼更簡單了(幾乎就像常規代碼一樣)。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"至於安全性:在這裏查看C++指南的安全配置文件的自動化工具。"},{"type":"link","attrs":{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/new-safety-rules-in-c-core-check\/?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"C++"}]},{"type":"link","attrs":{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/new-safety-rules-in-c-core-check\/?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":" "}]},{"type":"link","attrs":{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/new-safety-rules-in-c-core-check\/?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"Core"}]},{"type":"link","attrs":{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/new-safety-rules-in-c-core-check\/?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":" "}]},{"type":"link","attrs":{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/new-safety-rules-in-c-core-check\/?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"Check中的新安全規則|C++團隊博客"}]},{"type":"text","text":"。我們可以期待新的、更好的工具來執行代碼分析甚至檢測,以儘快發現潛在的安全問題。或者看這篇文章:"},{"type":"link","attrs":{"href":"https:\/\/www.youtube.com\/watch?v=_pQGRr4P16w&fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"使用靜態分析原理縮小Rust和C++之間的差距"}]},{"type":"link","attrs":{"href":"https:\/\/www.youtube.com\/watch?v=_pQGRr4P16w&fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"——"}]},{"type":"link","attrs":{"href":"https:\/\/www.youtube.com\/watch?v=_pQGRr4P16w&fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"SunnyChatterjee"}]},{"type":"link","attrs":{"href":"https:\/\/www.youtube.com\/watch?v=_pQGRr4P16w&fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"——"}]},{"type":"link","attrs":{"href":"https:\/\/www.youtube.com\/watch?v=_pQGRr4P16w&fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"CppCon"}]}]}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"使用指南"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你對C++代碼的許多方面都感到困惑,那麼你應該查閱C++核心指南。它由熱心的C++開發社區創建,主要編輯是Herb Sutter和Bjarne Stroustrup。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"看這裏:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https:\/\/github.com\/isocpp\/CppCoreGuidelines\/blob\/master\/CppCoreGuidelines.md?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"C++核心指南@Github"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這裏有一個漂亮的網站:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"C++核心指南:網站"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"只需輸入你面臨的問題(例如"},{"type":"codeinline","content":[{"type":"text","text":"return value"}]},{"type":"text","text":"),你就可以輕鬆找到建議——例如:"},{"type":"link","attrs":{"href":"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rf-out-multi?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"指南:返回值"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用這些指南將爲你節省大量時間,並且你可以非常快地學習一些好的模式。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"還有工具!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"感謝Clang以及其他平臺上的開發速度提升,我們獲得瞭如下工具:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"http:\/\/clang.llvm.org\/extra\/clang-tidy\/index.html?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"ClangTidy"}]},{"type":"text","text":"(以前是clang-modernise)"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https:\/\/clang.llvm.org\/docs\/ClangFormat.html?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"Clang"}]},{"type":"link","attrs":{"href":"https:\/\/clang.llvm.org\/docs\/ClangFormat.html?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":" Format"}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https:\/\/clang-analyzer.llvm.org\/?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"Clang"}]},{"type":"link","attrs":{"href":"https:\/\/clang-analyzer.llvm.org\/?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":" Static Analyzer"}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"http:\/\/www.wholetomato.com\/?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"VisualAssist"}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https:\/\/www.jetbrains.com\/resharper-cpp\/?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"Clion\/ResharperC++"}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"VisualStudio——像"},{"type":"link","attrs":{"href":"https:\/\/www.nuget.org\/packages\/Microsoft.CppCoreCheck?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"C++"}]},{"type":"link","attrs":{"href":"https:\/\/www.nuget.org\/packages\/Microsoft.CppCoreCheck?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":" "}]},{"type":"link","attrs":{"href":"https:\/\/www.nuget.org\/packages\/Microsoft.CppCoreCheck?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"Core"}]},{"type":"link","attrs":{"href":"https:\/\/www.nuget.org\/packages\/Microsoft.CppCoreCheck?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":" "}]},{"type":"link","attrs":{"href":"https:\/\/www.nuget.org\/packages\/Microsoft.CppCoreCheck?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"Checker"}]},{"type":"text","text":"這樣的工具"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"http:\/\/www.viva64.com\/en\/pvs-studio\/?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"PVS"}]},{"type":"link","attrs":{"href":"http:\/\/www.viva64.com\/en\/pvs-studio\/?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":" Studio"}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"用於VisualStudio的"},{"type":"link","attrs":{"href":"https:\/\/clangpowertools.com\/?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"Clang"}]},{"type":"link","attrs":{"href":"https:\/\/clangpowertools.com\/?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":" "}]},{"type":"link","attrs":{"href":"https:\/\/clangpowertools.com\/?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"Power"}]},{"type":"link","attrs":{"href":"https:\/\/clangpowertools.com\/?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":" "}]},{"type":"link","attrs":{"href":"https:\/\/clangpowertools.com\/?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"Tools"}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/new-c-core-check-rules\/?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"新的C++核心檢查規則|C++團隊博客"}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https:\/\/docs.microsoft.com\/en-us\/cpp\/code-quality\/code-analysis-for-cpp-corecheck?view=vs-2019&fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"C++核心指南檢查器參考|微軟文檔"}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/introducing-vcperf-timetrace-for-cpp-build-time-analysis\/?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"介紹vcperf\/timetrace"}]},{"type":"link","attrs":{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/introducing-vcperf-timetrace-for-cpp-build-time-analysis\/?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":","}]},{"type":"link","attrs":{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/introducing-vcperf-timetrace-for-cpp-build-time-analysis\/?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"用於C++構建時間分析|C++團隊博客"}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/new-safety-rules-in-c-core-check\/?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"C++"}]},{"type":"link","attrs":{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/new-safety-rules-in-c-core-check\/?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":" "}]},{"type":"link","attrs":{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/new-safety-rules-in-c-core-check\/?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"Core"}]},{"type":"link","attrs":{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/new-safety-rules-in-c-core-check\/?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":" "}]},{"type":"link","attrs":{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/new-safety-rules-in-c-core-check\/?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"Check中的新安全規則|C++團隊博客"}]},{"type":"text","text":"——C++和Rust一樣安全嗎?"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"或者查看我關於其他工具的文章:"},{"type":"link","attrs":{"href":"https:\/\/www.cppstories.com\/2019\/10\/cppecosystem.html?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"C++生態系統:編譯器、IDE、工具、測試等"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"雖然它不像其他語言(主要基於Java或基於.NET)那麼好,但它正在變得越來越好。請記住,由於C++語法複雜,因此很難實現即時分析代碼的工具。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"努力跟上最新進展"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"C++社區非常活躍。有很多博客、書籍、會議……甚至有可能在你所在的城市有本地社區。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先,我建議去isocpp.org查看所有事件\/新聞\/文章。然後你可以查看"},{"type":"link","attrs":{"href":"http:\/\/meetingcpp.com\/?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"Meeting C++"}]},{"type":"text","text":"和有關"},{"type":"link","attrs":{"href":"http:\/\/meetingcpp.com\/index.php\/user-groups.html?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"本地C++小組"}]},{"type":"text","text":"的信息。還有"},{"type":"link","attrs":{"href":"https:\/\/www.reddit.com\/r\/cpp\/?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"reddit\/cpp"}]},{"type":"text","text":",你可以在那裏看到一些最棒的C++故事。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"還有"},{"type":"link","attrs":{"href":"https:\/\/cppcast.com\/?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"CppCast"}]},{"type":"text","text":"——一個針對C++開發人員的每週播客。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"並參考以下書籍:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https:\/\/amzn.to\/2Sqpete?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"C++編程語言第4版"}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https:\/\/amzn.to\/3jvyCHQ?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"高效"}]},{"type":"link","attrs":{"href":"https:\/\/amzn.to\/3jvyCHQ?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"的現代C++"}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https:\/\/amzn.to\/2kJ3mJM?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"編程:使用C++的原理和實踐"}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"http:\/\/amzn.to\/2kNdhOG?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"發現現代C++:科學家、工程師和程序員的強化課程"}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https:\/\/amzn.to\/34oqLFr?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"C++之旅(C++深入系列)第2版"}]}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"你還可以查看推薦的C++資源列表:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https:\/\/www.cppstories.com\/p\/resources.html?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"Bartek的編程博客:資源"}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"細節太多了?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"C++如此強大的原因之一是它允許你實現非常接近底層的代碼。你可以控制所有細節、內存佈局、性能優化等……同時,這些能力增加了語言的複雜性。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"不過,如果你不需要走那麼遠,你可以停留在相對較高的抽象級別。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"例如,你不需要編寫可選類型,因爲你可以使用標準庫中的"},{"type":"codeinline","content":[{"type":"text","text":"std::optional"}]},{"type":"text","text":"。如果你不想涉及低級別和容易出錯的聯合類型,你應該意識到"},{"type":"codeinline","content":[{"type":"text","text":"std::variant"}]},{"type":"text","text":"是一個安全的選項。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"使用你需要的東西"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"C++是一種多範式語言;你可以以多種不同的方式使用它。最近,我讀到了一條有趣的評論,說Cpp程序員在不接觸模板元編程甚至異常等高級內容的情況下,也能持續多年表現出色。這在很大程度上取決於項目的代碼風格。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"例如,即使像谷歌這樣的公司也限制了C++的特性,比如說"},{"type":"link","attrs":{"href":"https:\/\/google.github.io\/styleguide\/cppguide.html#Exceptions?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"他們不使用異常"}]},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你不是庫開發人員,你可能不會遇到自定義移動運算符或移動構造器的麻煩。同樣,高級元編程的內容也可能不是你的代碼的關鍵部分。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"增量變更"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你是從頭開始或只有一個小的代碼庫,那麼轉到C++11\/14應該相對容易一些。可是20年(或更久!)前開始創建的上百萬行代碼呢?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"只需一步一步來就行了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"至少對於新代碼,你應該開始使用現代C++。此外,通過應用“"},{"type":"link","attrs":{"href":"http:\/\/programmer.97things.oreilly.com\/wiki\/index.php\/The_Boy_Scout_Rule?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"童子軍規則"}]},{"type":"text","text":"”,你可以改進你所接觸的那些代碼。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這可能會帶來一些混合代碼,但還是比只保留老舊風格要好。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"最後的底線:你的舊代碼仍然可以編譯"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"C++規範越來越大的原因之一是該語言向後兼容。所以委員會通常會引入新特性,但很少刪除舊的東西。所以……你的代碼仍然可以編譯。如果你不想前進,不想使用新的東西,那麼你還是可以保持當前的風格。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有時你會收到一些關於不推薦使用的內容或刪除特性的警告(如C++17中的"},{"type":"codeinline","content":[{"type":"text","text":"auto_ptr"}]},{"type":"text","text":"),但即使在這種情況下,你也可以將編譯器切換到一些較舊的C++標準。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"總結"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這篇文章有一些抱怨,也有一些“美化”。我試圖找出這種語言及其演變過程中存在的各種問題,以及一些積極的改進跡象。雖然我們可以抱怨複雜性、變化的速度等,但我認爲我們不能說這種語言已經死掉了。這是好事!:)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我認爲你不必快速追逐新特性並立即重寫現有代碼。試着跟上進展,使用真正改進你工作的特性,你的代碼應該逐漸改進並變得更加“現代化”(這是可以定義的,請參閱meetingcpp的相關"},{"type":"link","attrs":{"href":"http:\/\/meetingcpp.com\/index.php\/br\/items\/what-does-modern-c-really-mean.html?fileGuid=HjDhgwWw6jPKDcCK","title":"","type":null},"content":[{"type":"text","text":"文章"}]},{"type":"text","text":")。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在採用C++11\/14\/17\/20的新特性時,你的方法是什麼?"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"你使用C++的主要問題是什麼?"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"你在工作中使用現代C++嗎?"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"原文鏈接:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"https:\/\/www.cppstories.com\/2017\/02\/how-to-stay-sane-with-modern-c\/"}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章