C++核心指导原则: 标准库规则

C++ Core Guidelines 整理目录

  1. 哲学部分
  2. 接口(Interface)部分
  3. 函数部分
  4. 类和类层次结构部分
  5. 枚举部分
  6. 资源管理部分
  7. 表达式和语句部分
  8. 性能部分
  9. 并发和并行
  10. 错误处理
  11. 常量和不可变性
  12. 泛型编程
  13. 源文件
  14. 命名和布局建议
  15. 标准库
  16. 其他规则

The Standard Library

标准库规则总结

SL.1: Use libraries wherever possible

SL.2: Prefer the standard library to other libraries

SL.3: Do not add non-standard entities to namespace std

SL.4: Use the standard library in a type-safe manner

SL.con: 容器规则总结

SL.con.1: Prefer using STL array or vector instead of a C array

SL.con.2: Prefer using STL vector by default unless you have a reason to use a different container

SL.con.3: Avoid bounds errors

SL.con.4: don’t use memset or memcpy for arguments that are not trivially-copyable

SL.str: String 规则

SL.str.1: Use std::string to own character sequences

SL.str.2: Use std::string_view or gsl::span<char> to refer to character sequences

SL.str.3: Use zstring or czstring to refer to a C-style, zero-terminated, sequence of characters

SL.str.4: Use char* to refer to a single character

SL.str.5: Use std::byte to refer to byte values that do not necessarily represent characters

SL.str.10: Use std::string when you need to perform locale-sensitive string operations

SL.str.11: Use gsl::span<char> rather than std::string_view when you need to mutate a string

SL.str.12: Use the s suffix for string literals meant to be standard-library strings

SL.io: Iostream 规则

SL.io.1: Use character-level input only when you have to

SL.io.2: When reading, always consider ill-formed input

SL.io.3: Prefer iostreams for I/O

SL.io.10: Unless you use printf-family functions call ios_base::sync_with_stdio(false)

SL.io.50: Avoid endl

SL.C: C 标准库规则

SL.C.1: Don’t use setjmp/longjmp