(原文連結:https://abseil.io/tips/ 譯者:[email protected])
C++每周貼士
背景介紹:大約五年前,我們在Google内部開始釋出一系列C++貼士,大約每周一個,被稱為“C++每周貼士”(TotW)。這個系列非常成功,直到今天我們還在釋出它們。(這也表明像C++這麼豐富的語言短期内不會讓我們缺少主題)。
我們不光讨論更精細的語言知識點,我們是真的“貼士”風格——提供建議或設計偏好。C++ TotW集合已經成為Google内部的聖經(canon),每周都會在代碼審查和内部郵件清單讨論中被引用數千次。它們常以數字被引用,其中一些已經被簡化為“totw/110”或"totw/77"。
我們決定将其中大部分貼士開放給Abseil開發社群,以及全體C++社群。在接下來的幾個月裡,我們會檢查,改善然後釋出新的貼士。不是所有的貼士都和Google外的世界有關;有些貼士是為我們的内部工具和概念量身定制的,有些已經随着語言的演進而變得過時了。但我們會在此頁面上釋出我們能釋出的貼士和我們發現有價值的貼士(甚至撰寫新的貼士!)
注:我們會儲存貼士原有的數字命名規範,最初釋出時間,這樣已經接觸過原有數字編号的那1.2萬人就不必學習新的引用位址了。也是因為如此,新來的讀者可能會發現一些編号的貼士不見了或順序是亂的。但請放心,我們給你的都是幹貨。
注2:一些貼士會包括曆史資訊——雖然準确——但可能反映的是初寫貼士時的設計哲學或使用方式。大部分情況下,我們已經更新了這些資訊來反映現今的實踐,而且在适當的地方标注了曆史原因造成的例外情況。
因為這些貼士并不是按照原始順序進行釋出的,我們把它們按重新釋出的時間列在了下面。
-
2020-11-11
每周貼士 #186: 最好把函數放在匿名命名空間裡
每周貼士 #187:
必須被移動std::unique_ptr
-
2020-09-11
每周貼士 #116: 如何存儲指向參數的引用
每周貼士 #165: 帶初始化器的
和if
switch
語句
每周貼士 #181: 通路
StatusOr<T>
裡的值
每周貼士 #5: 消逝的演出
每周貼士 #76: 使用absl::Status
-
2020-09-01
每周貼士 #140: 常量:安全用法
-
2020-06-01
每周貼士 #5: 消逝的演出
-
2020-04-06
每周貼士 #163: 傳遞
absl::optional
參數
Tip of the Week #171: Avoid Sentinel Values
Tip of the Week #172: Designated Initializers
每周貼士 #173: 以Option結構體傳遞參數
每周貼士 #175: C++14和C++17裡對字面值常量的改變
每周貼士 #176: 傳回值優于輸出參數
Tip of the Week #177: Assignability vs. Data Member Types
-
2019-12-19
每周貼士 #108: 避免使用std::bind
-
2019-12-12
Tip of the Week #146: Default vs Value Initialization
Tip of the Week #166: When a Copy is not a Copy
-
2019-11-25
Tip of the Week #161: Good Locals and Bad Locals
Tip of the Week #168: inline Variables
-
2019-09-09
Tip of the Week #147: Use Exhaustive switch Statements Responsibly
-
2019-05-10
Tip of the Week #103: Flags Are Globals
Tip of the Week #45: Avoid Flags, Especially in Library Code
Tip of the Week #90: Retired Flags
-
2018-09-30
Tip of the Week #153: Don’t use using-directives
-
2018-09-28
Tip of the Week #136: Unordered Containers
Tip of the Week #144: Heterogeneous Lookup in Associative Containers
每周貼士 #152: AbslHashValue和你
-
2018-05-03
Tip of the Week #117: Copy Elision and Pass-by-value
Tip of the Week #148: Overload Sets
Tip of the Week #149: Object Lifetimes vs.
=delete
-
2018-04-20
每周貼士 #11: 傳回政策
Tip of the Week #120: Return Values are Untouchable
每周貼士 #24: 複制簡明判定法
-
2018-03-05
Tip of the Week #141: Beware Implicit Conversions to bool
Tip of the Week #143: C++11 deleted functions (
)= delete
-
2018-02-22
Tip of the Week #134: make_unique and private constructors
Tip of the Week #61: Default Member Initializers
Tip of the Week #93: using absl::Span
-
2018-02-15
Tip of the Week #142: Multi-parameter Constructors and
Tip of the Week #88: Initialization: =, (), and {}explicit
-
2018-01-25
每周貼士 #10: 分割字元串,不必拘小節
每周貼士 #3: 字元串拼接,
對比operator+
StrCat()
每周貼士 #36: 新的拼接API
Tip of the Week #59: Joining Tuples
-
2017-12-21
每周貼士 #42: 工廠函數優于初始化方法
Tip of the Week #74: Delegating and Inheriting Constructors
-
2017-12-07
Tip of the Week #123: absl::optional and std::unique_ptr
Tip of the Week #131: Special Member Functions and
= default
-
2017-11-30
Tip of the Week #119: Using-declarations and namespace aliases
Tip of the Week #130: Namespace Naming
-
2017-11-23
Tip of the Week #109: Meaningful
const
in Function Declarations
Tip of the Week #126:
is the newmake_unique
new
-
2017-11-16
Tip of the Week #112: emplace vs. push_back
Tip of the Week #65: Putting Things in their Place
Tip of the Week #99: Nonmember Interface Etiquette
-
2017-11-09
Tip of the Week #135: Test the Contract, not the Implementation
每周貼士 #49: 參數依賴查找
-
2017-11-02
Tip of the Week #101: Return Values, References, and Lifetimes
Tip of the Week #107: Reference Lifetime Extension
-
2017-10-26
Tip of the Week #64: Raw String Literals
Tip of the Week #86: Enumerating with Class
Tip of the Week #94: Callsite Readability and bool Parameters
-
2017-10-20
Tip of the Week #122: Test Fixtures, Clarity, and Dataflow
Tip of the Week #55: Name Counting and unique_ptr
每周貼士 #77: 臨時變量,移動,和複制
-
2017-09-26
每周貼士 #1: string_view