scoped_ptr,特定場景 shared_ptr,禁用 auto_ptrscoped_ptr:單一擁有者,作用域結束自動釋放,**零額外開銷**。shared_ptr (C++11 std::shared_ptr):僅用於 STL 容器共享擁有權,引用計數有效能成本且易循環引用。auto_ptr:複製語意詭異(所有權轉移),極易誤用。const T&,輸出參數用 T*const**。void Foo(const string& in, string* out);bind2nd/mem_fun 等 STL 配接器不接受參考,須用指標。AppendString()/AppendInt() 替代 Append() 多載。alloca():禁止scoped_ptr/scoped_array、std::vector。FooBuilder 為 Foo 的 friend 建構內部狀態;單元測試類別為待測類別 friend。public 破壞封裝。dynamic_cast,**勿手工實作類 RTTI 機制**。static_cast 等,禁用 C 風格static_cast:數值轉換、明確向上轉型。const_cast:移除 const。reinterpret_cast:指標/整數不安全轉換,**極度謹慎**。dynamic_cast:僅單元測試允許,其他場合說明設計有問題。printf/read/write%1$s 不支援)、pread 等難實作、<< 重載易誤用(this vs *this)。printf 族。++i後置形式需複製暫存值,迭代器複製成本高。純量型別無差異。
const:能用盡用,提倡前置 const int*const 增加編譯期檢查層級。const int * const * const x → const int** x 即可。mutable 多執行緒不安全,慎用。int,確定寬度用 <stdint.h> (int16_t 等),禁用無號型別表達非負數int 至少 32 位,勿假設更大;需 64 位用 int64_t/uint64_t。for (unsigned i=n-1; i>=0; --i) 無限迴圈;型別提升機制導致行為反直覺。assert(x >= 0) 保護非負語意,型別用有號整數。printf 格式字串用 PRIuS/PRId64 等巨集(含 inttypes.h)。sizeof(void*) != sizeof(int),指標大小整數用 intptr_t。__attribute__((packed)) 或 #pragma pack()。LL/ULL。.h 禁定義、用前 #define 用後 #undef、避免名稱衝突、避免不平衡 C++ 結構。0/NULL/nullptr:整數 0、浮點 0.0、指標 NULL (C++11 nullptr)、字元 '\0'NULL 像指標,某些編譯器提供有用警告(sizeof(NULL) != sizeof(0))。
sizeof:優先 sizeof(varname),型別改變自動同步Struct data; memset(&data, 0, sizeof(data)); // 佳
memset(&data, 0, sizeof(Struct)); // 型別改名易忘記更新
目前允許:Compressed Pair、Pointer Container (除 ptr_array/序列化)。積極評估擴充中。