@chengzhao-dev/dsh-cpp-workflow 0.6.19 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (163) hide show
  1. package/README.en.md +11 -26
  2. package/README.md +13 -28
  3. package/cordis.patch.yml +1 -1
  4. package/lib/client.js +33 -21
  5. package/lib/comment-policy.js +15 -6
  6. package/lib/index.js +1 -1
  7. package/lib/workspace.js +0 -15
  8. package/package.json +7 -7
  9. package/skills/building-cpp/SKILL.md +34 -21
  10. package/skills/building-cpp/assets/templates/projects/common/running-build.sh +5 -1
  11. package/skills/building-cpp/project/vscode/01-extensions.md +28 -0
  12. package/skills/building-cpp/project/vscode/02-settings.md +39 -0
  13. package/skills/building-cpp/references/build-script.md +14 -7
  14. package/skills/building-cpp/references/environment.md +8 -1
  15. package/skills/building-cpp/steps/01-build.md +8 -8
  16. package/skills/building-cpp/steps/02-tooling.md +17 -9
  17. package/skills/building-cpp/steps/03-check.md +16 -14
  18. package/skills/developing-cpp/SKILL.md +37 -25
  19. package/skills/developing-cpp/references/comments/comment-rules.md +181 -0
  20. package/skills/developing-cpp/references/comments/comment-style.md +80 -0
  21. package/skills/developing-cpp/references/comments/examples/declaration-class.md +66 -0
  22. package/skills/developing-cpp/references/comments/examples/declaration-constructor.md +22 -0
  23. package/skills/developing-cpp/references/comments/examples/declaration-enum.md +48 -0
  24. package/skills/developing-cpp/references/comments/examples/declaration-file.md +66 -0
  25. package/skills/developing-cpp/references/comments/examples/declaration-function.md +127 -0
  26. package/skills/developing-cpp/references/comments/examples/declaration-member.md +23 -0
  27. package/skills/developing-cpp/references/comments/examples/declaration-predicate.md +14 -0
  28. package/skills/developing-cpp/references/comments/examples/declaration-struct.md +18 -0
  29. package/skills/developing-cpp/references/comments/examples/implementation-body.md +64 -0
  30. package/skills/developing-cpp/references/comments/examples/implementation-todo.md +16 -0
  31. package/skills/developing-cpp/references/comments/glossary/comment-terms.md +93 -0
  32. package/skills/developing-cpp/references/design/examples/behavior-sentence.md +14 -0
  33. package/skills/developing-cpp/references/design/examples/coordinate-table.md +31 -0
  34. package/skills/developing-cpp/references/design/examples/header-place.md +26 -0
  35. package/skills/developing-cpp/references/design/examples/library-boundary.md +30 -0
  36. package/skills/developing-cpp/references/design/examples/library-split.md +7 -0
  37. package/skills/developing-cpp/references/design/examples/move-result.md +39 -0
  38. package/skills/developing-cpp/references/design/examples/preview-read.md +40 -0
  39. package/skills/developing-cpp/references/{project-design.md → design/project-design.md} +57 -5
  40. package/skills/developing-cpp/references/{change-model.md → editing/01-edit-operations.md} +5 -3
  41. package/skills/developing-cpp/references/{priority.md → editing/02-rule-priority.md} +2 -2
  42. package/skills/developing-cpp/references/exceptions/examples/throw-boundary.md +28 -0
  43. package/skills/developing-cpp/references/exceptions/examples/throw-build-mode.md +38 -0
  44. package/skills/developing-cpp/references/exceptions/examples/throw-catch.md +21 -0
  45. package/skills/developing-cpp/references/exceptions/examples/throw-destructor.md +21 -0
  46. package/skills/developing-cpp/references/exceptions/examples/throw-format.md +13 -0
  47. package/skills/developing-cpp/references/exceptions/examples/throw-message.md +16 -0
  48. package/skills/developing-cpp/references/exceptions/examples/throw-when.md +27 -0
  49. package/skills/developing-cpp/references/{exceptions.md → exceptions/throw-rules.md} +78 -23
  50. package/skills/developing-cpp/references/{glossary.md → language/glossary/language-terms.md} +2 -2
  51. package/skills/developing-cpp/references/language/language-basics/01-objects.md +15 -0
  52. package/skills/developing-cpp/references/language/language-basics/02-initialization.md +26 -0
  53. package/skills/developing-cpp/references/language/language-basics/03-deduction.md +12 -0
  54. package/skills/developing-cpp/references/language/language-basics/04-const.md +25 -0
  55. package/skills/developing-cpp/references/language/language-basics/05-expressions.md +23 -0
  56. package/skills/developing-cpp/references/language/language-basics/06-functions.md +15 -0
  57. package/skills/developing-cpp/references/language/language-basics/07-classes.md +15 -0
  58. package/skills/developing-cpp/references/language/language-basics/08-raii.md +15 -0
  59. package/skills/developing-cpp/references/language/language-basics/09-templates.md +26 -0
  60. package/skills/developing-cpp/references/language/language-basics/10-checklist.md +10 -0
  61. package/skills/developing-cpp/references/language/performance/01-measure.md +22 -0
  62. package/skills/developing-cpp/references/language/performance/02-complexity.md +13 -0
  63. package/skills/developing-cpp/references/language/performance/03-memory.md +14 -0
  64. package/skills/developing-cpp/references/language/performance/04-undefined-behavior.md +28 -0
  65. package/skills/developing-cpp/references/language/performance/05-pitfalls.md +17 -0
  66. package/skills/developing-cpp/references/language/performance/06-false-optimizations.md +13 -0
  67. package/skills/developing-cpp/references/language/performance/07-checklist.md +8 -0
  68. package/skills/developing-cpp/references/language/standard-library/01-containers.md +29 -0
  69. package/skills/developing-cpp/references/language/standard-library/02-access.md +14 -0
  70. package/skills/developing-cpp/references/language/standard-library/03-iteration.md +18 -0
  71. package/skills/developing-cpp/references/language/standard-library/04-algorithms.md +26 -0
  72. package/skills/developing-cpp/references/language/standard-library/05-string.md +13 -0
  73. package/skills/developing-cpp/references/language/standard-library/06-format.md +106 -0
  74. package/skills/developing-cpp/references/language/standard-library/07-time.md +13 -0
  75. package/skills/developing-cpp/references/language/standard-library/08-random.md +13 -0
  76. package/skills/developing-cpp/references/language/standard-library/09-files.md +14 -0
  77. package/skills/developing-cpp/references/language/standard-library/10-checklist.md +9 -0
  78. package/skills/developing-cpp/references/language/value-types/01-width.md +17 -0
  79. package/skills/developing-cpp/references/language/value-types/02-signed.md +26 -0
  80. package/skills/developing-cpp/references/language/value-types/03-integer-roles.md +38 -0
  81. package/skills/developing-cpp/references/language/value-types/04-enum.md +24 -0
  82. package/skills/developing-cpp/references/language/value-types/05-strong-type.md +17 -0
  83. package/skills/developing-cpp/references/language/value-types/06-const-method.md +22 -0
  84. package/skills/developing-cpp/references/language/value-types/07-nodiscard.md +15 -0
  85. package/skills/developing-cpp/references/language/value-types/08-comparison.md +10 -0
  86. package/skills/developing-cpp/references/language/value-types/09-subscript.md +15 -0
  87. package/skills/developing-cpp/references/language/value-types/10-float-bool.md +13 -0
  88. package/skills/developing-cpp/references/language/value-types/11-checklist.md +8 -0
  89. package/skills/developing-cpp/references/{layout.md → layout/01-source-layout.md} +10 -4
  90. package/skills/developing-cpp/references/{file-template.md → layout/02-file-template.md} +10 -4
  91. package/skills/developing-cpp/references/layout/03-declaration-order.md +138 -0
  92. package/skills/developing-cpp/references/naming/examples/naming-accessor.md +17 -0
  93. package/skills/developing-cpp/references/naming/examples/naming-basic.md +3 -0
  94. package/skills/developing-cpp/references/naming/examples/naming-current-prefix.md +19 -0
  95. package/skills/developing-cpp/references/naming/examples/naming-lookup.md +18 -0
  96. package/skills/developing-cpp/references/naming/examples/naming-lower-layer.md +39 -0
  97. package/skills/developing-cpp/references/naming/examples/naming-multiple-return.md +20 -0
  98. package/skills/developing-cpp/references/naming/examples/naming-player-value.md +16 -0
  99. package/skills/developing-cpp/references/naming/examples/naming-predicate.md +18 -0
  100. package/skills/developing-cpp/references/naming/examples/naming-rename.md +5 -0
  101. package/skills/developing-cpp/references/naming/examples/naming-responsibility.md +19 -0
  102. package/skills/developing-cpp/references/naming/examples/naming-role.md +21 -0
  103. package/skills/developing-cpp/references/naming/examples/naming-state-change.md +57 -0
  104. package/skills/developing-cpp/references/{naming.md → naming/identifier-names.md} +37 -9
  105. package/skills/developing-cpp/references/{google-cpp-style.md → style/google-cpp-style.md} +11 -10
  106. package/skills/developing-cpp/scripts/checking-comments.mjs +71 -14
  107. package/skills/developing-cpp/scripts/checking-declaration-order.mjs +357 -0
  108. package/skills/developing-cpp/steps/01-behavior.md +5 -5
  109. package/skills/developing-cpp/steps/02-boundary.md +10 -10
  110. package/skills/developing-cpp/steps/03-declaration.md +16 -14
  111. package/skills/developing-cpp/steps/04-implementation.md +21 -17
  112. package/skills/engineering/SKILL.md +87 -0
  113. package/skills/{configuring-cmake/assets/cmake → engineering/references/cmake/assets}/CompileSettings.cmake +2 -0
  114. package/skills/{building-cpp/references/linking.md → engineering/references/cmake/linking/01-libraries.md} +7 -48
  115. package/skills/engineering/references/cmake/linking/02-rpath.md +25 -0
  116. package/skills/engineering/references/cmake/linking/03-undefined-reference.md +20 -0
  117. package/skills/engineering/references/cmake/linking/04-inspect.md +21 -0
  118. package/skills/{configuring-cmake/references/modules.md → engineering/references/cmake/modules/functions.md} +8 -5
  119. package/skills/{configuring-cmake/references → engineering/references/cmake/standard}/cxx-standard.md +5 -4
  120. package/skills/engineering/references/cmake/standard/examples/standard.md +19 -0
  121. package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/README.md +9 -9
  122. package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++20/README.md +1 -1
  123. package/skills/engineering/references/cmake/targets/examples/glob.md +22 -0
  124. package/skills/engineering/references/cmake/targets/examples/rpath.md +24 -0
  125. package/skills/{configuring-cmake/examples/cmake.md → engineering/references/cmake/targets/examples/target.md} +2 -70
  126. package/skills/{configuring-cmake/references/cmake.md → engineering/references/cmake/targets/rules.md} +18 -11
  127. package/skills/{building-cpp/references/testing.md → engineering/references/cmake/testing/ctest.md} +11 -4
  128. package/skills/{building-cpp/references/encoding.md → engineering/references/formatting/01-encoding.md} +1 -1
  129. package/skills/engineering/references/formatting/02-clang-format.md +20 -0
  130. package/skills/engineering/references/formatting/03-clang-tidy.md +58 -0
  131. package/skills/engineering/references/formatting/04-clangd.md +90 -0
  132. package/skills/engineering/references/formatting/examples/clang-format.md +38 -0
  133. package/skills/engineering/references/formatting/examples/clang-tidy.md +43 -0
  134. package/skills/{building-cpp/examples/tooling.md → engineering/references/formatting/examples/clangd.md} +1 -80
  135. package/skills/{configuring-cmake → engineering}/steps/01-targets.md +6 -6
  136. package/skills/{configuring-cmake → engineering}/steps/02-modules.md +4 -4
  137. package/skills/{configuring-cmake → engineering}/steps/03-check.md +3 -2
  138. package/templates/cpp-workflow-project/SKILL.md +1 -1
  139. package/skills/building-cpp/references/static-analysis.md +0 -148
  140. package/skills/configuring-cmake/SKILL.md +0 -68
  141. package/skills/developing-cpp/examples/comments-declaration.md +0 -313
  142. package/skills/developing-cpp/examples/comments-implementation.md +0 -66
  143. package/skills/developing-cpp/examples/exceptions.md +0 -173
  144. package/skills/developing-cpp/examples/naming.md +0 -168
  145. package/skills/developing-cpp/examples/structure.md +0 -194
  146. package/skills/developing-cpp/references/comment-terms.md +0 -75
  147. package/skills/developing-cpp/references/comments.md +0 -223
  148. package/skills/developing-cpp/references/declaration-order.md +0 -118
  149. package/skills/developing-cpp/references/language.md +0 -123
  150. package/skills/developing-cpp/references/performance.md +0 -83
  151. package/skills/developing-cpp/references/standard-library.md +0 -138
  152. package/skills/developing-cpp/references/types.md +0 -154
  153. /package/skills/{configuring-cmake/assets/cmake → engineering/references/cmake/assets}/LinkSettings.cmake +0 -0
  154. /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++11/README.md +0 -0
  155. /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++11/cmake-snippet.cmake +0 -0
  156. /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++14/README.md +0 -0
  157. /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++14/cmake-snippet.cmake +0 -0
  158. /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++17/README.md +0 -0
  159. /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++17/cmake-snippet.cmake +0 -0
  160. /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++20/cmake-snippet.cmake +0 -0
  161. /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++23/README.md +0 -0
  162. /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++23/cmake-snippet.cmake +0 -0
  163. /package/skills/{building-cpp → engineering/references/formatting}/scripts/checking-encoding.mjs +0 -0
@@ -0,0 +1,8 @@
1
+ # 检查清单
2
+
3
+ - [ ] 性能目标可测量,测量条件已记录。
4
+ - [ ] 先看复杂度,再看缓存,再看常数。
5
+ - [ ] 没有未测量的“优化”。
6
+ - [ ] 大对象传递和返回没有多余拷贝。
7
+ - [ ] ASan/UBSan 在测试中跑过,警告没有被关闭。
8
+ - [ ] 没有用未定义行为换速度。
@@ -0,0 +1,29 @@
1
+ # 容器选择
2
+
3
+ 标准库不是“能用就行”的工具箱。先确定访问模式、所有权和失败方式,再选容器和算法。
4
+
5
+ | 需求 | 默认选择 | 说明 |
6
+ | --- | --- | --- |
7
+ | 固定长度、编译期已知 | `std::array<T, N>` | 不堆分配,可当聚合成员;长度是类型的一部分 |
8
+ | 可变长度、连续存储 | `std::vector<T>` | 默认容器;随机访问、尾部追加、缓存友好 |
9
+ | 两端增删 | `std::deque<T>` | 分段连续;中间插入仍然昂贵 |
10
+ | 文本 | `std::string` | 拥有字符;修改后 `c_str()` / `data()` 可能失效 |
11
+ | 只读文本视图 | `std::string_view` | 不拥有字符;不能比原字符串活得更久 |
12
+ | 有序键值 | `std::map<K, V>` | 按 key 有序;查找 O(log n);节点独立 |
13
+ | 无序键值 | `std::unordered_map<K, V>` | 平均 O(1),最坏 O(n);迭代顺序不稳定 |
14
+ | 去重集合 | `std::set` / `std::unordered_set` | 只存 key;不要用 `map<K, bool>` 代替 |
15
+ | 连续只读视图 | `std::span<T>` | 不拥有元素;C++20 没有 `at` |
16
+
17
+ 选择顺序:
18
+
19
+ 1. 先问是否需要稳定顺序、是否需要按 key 查找、是否需要连续内存。
20
+ 2. 默认 `vector`;只有剖析或明确复杂度要求时才换。
21
+ 3. 需要“可能没有”的返回值时用 `std::optional<T>`,不要用特殊值(`-1`、空字符串)表示缺失。
22
+ 4. 需要“成功值或错误原因”时用 `std::expected<T, E>`(C++23)——C++20 项目改用项目自己的结果类型或 `std::variant`,不要假称标准库已有。
23
+ 5. 需要多态值集合时用 `std::variant` 或 `std::unique_ptr<Base>`;不要用 `void*`。
24
+
25
+ ## 依据
26
+
27
+ - [ISO C++ `[containers]`](https://eel.is/c++draft/containers):容器要求、复杂度与迭代器失效规则。
28
+ - [cppreference: Containers library](https://en.cppreference.com/w/cpp/container):各容器的接口与复杂度对照。
29
+ - [C++ Core Guidelines: SL.con.1](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#s-stdlib):优先标准容器与算法,不手写动态数组。
@@ -0,0 +1,14 @@
1
+ # 访问与边界
2
+
3
+ - `operator[]` 不检查边界,越界是未定义行为;`at` 检查并抛 `std::out_of_range`。规则与家族差异见 `references/language/value-types/09-subscript.md`。
4
+ - `vector::front()` / `back()` / `data()` 在空容器上未定义;先 `empty()`。
5
+ - `map::operator[]` 缺键会插入默认值;只读查询用 `find`、`contains`(C++20)或 `at`。
6
+ - `string_view` 不保证以 `\0` 结尾;传给需要 C 字符串的 API 前先转成 `std::string`。
7
+ - `span` 不拥有元素,也不延长生命周期;不要从临时容器构造后保存。
8
+ - 迭代器区间是半开的 `[first, last)`;`end()` 不指向元素,不能解引用。
9
+
10
+ ## 依据
11
+
12
+ - [ISO C++ `[sequence.reqmts]`](https://eel.is/c++draft/sequence.reqmts):`at` 与 `operator[]` 的边界行为。
13
+ - [cppreference: `array::at`](https://en.cppreference.com/w/cpp/container/array/at):越界抛 `std::out_of_range` 的契约。
14
+ - [C++ Core Guidelines: SL.con.2](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#s-stdlib):默认用 `at`,只在已证明边界时用 `[]`。
@@ -0,0 +1,18 @@
1
+ # 迭代与失效
2
+
3
+ - 范围 `for` 默认按值复制元素;大对象写 `const auto&`,需要修改写 `auto&`。
4
+ - 在范围 `for` 里修改容器大小会使迭代器失效;先收集要改的 key,再统一修改。
5
+ - `vector` 扩容会使所有指针、引用、迭代器失效;`reserve` 只能减少扩容次数,不能保证永不失效。
6
+ - `map` / `unordered_map` 插入不会使其他元素的引用失效;`unordered_map` 的 rehash 会使迭代器失效。
7
+ - 删除元素用 `erase-remove` 惯用法或 C++20 `std::erase` / `std::erase_if`;不要在遍历中直接 `erase` 当前迭代器以外的元素。
8
+
9
+ ```cpp
10
+ std::vector<int> values{1, 2, 3, 4, 5};
11
+ std::erase_if(values, [](int value) { return value % 2 == 0; });
12
+ ```
13
+
14
+ ## 依据
15
+
16
+ - [ISO C++ `[container.requirements]`](https://eel.is/c++draft/container.requirements):插入、删除与 rehash 对迭代器/引用的影响。
17
+ - [cppreference: Iterator invalidation](https://en.cppreference.com/w/cpp/container#Iterator_invalidation):各容器的失效条件。
18
+ - [C++ Core Guidelines: SL.con.3](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#s-stdlib):用算法与范围操作替代手写遍历。
@@ -0,0 +1,26 @@
1
+ # 算法优先于手写循环
2
+
3
+ 能用标准算法表达意图时,不写下标循环:
4
+
5
+ | 意图 | 算法 |
6
+ | --- | --- |
7
+ | 查找 | `std::find` / `std::find_if` |
8
+ | 计数 | `std::count` / `std::count_if` |
9
+ | 任一/全部 | `std::any_of` / `std::all_of` / `std::none_of` |
10
+ | 变换 | `std::transform` |
11
+ | 排序 | `std::sort` / `std::stable_sort` |
12
+ | 最值 | `std::min_element` / `std::max_element` |
13
+ | 累加 | `std::accumulate`(注意初始值类型) |
14
+ | 二分查找 | `std::lower_bound` / `std::upper_bound`(要求已排序) |
15
+
16
+ - 算法接收迭代器区间,不接收容器;调用时写 `values.begin(), values.end()` 或 C++20 `std::ranges::sort(values)`。
17
+ - 嵌套数组整体赋同一个值时用 `std::ranges::fill` 与 `std::views::join`;逐格做不同处理时仍用 `row` / `col` 循环。
18
+ - 谓词只做判断,不改元素;需要修改时用 `transform`。
19
+ - 捕获列表只捕获需要的变量;默认 `[&]` 容易悬空,默认 `[=]` 容易复制大对象。
20
+ - 不在比较函数里写有副作用的逻辑;排序要求严格弱序,违反会导致未定义行为。
21
+
22
+ ## 依据
23
+
24
+ - [ISO C++ `[algorithms]`](https://eel.is/c++draft/algorithms):算法要求、复杂度与谓词契约。
25
+ - [cppreference: Algorithms library](https://en.cppreference.com/w/cpp/algorithm):算法分类与 `ranges` 版本。
26
+ - [C++ Core Guidelines: SL.11](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#s-stdlib):用标准算法表达意图,避免手写循环。
@@ -0,0 +1,13 @@
1
+ # `std::string` 与 `std::string_view`
2
+
3
+ - 函数只读文本时参数用 `std::string_view`;需要保存或修改时用 `std::string`。
4
+ - `string_view` 不拥有内存;不要返回指向局部 `std::string` 的 `string_view`,也不要把临时 `std::string` 转成 `string_view` 后保存。
5
+ - 拼接少量文本用 `std::string` 的 `+=` 或 `std::format`;大量拼接先 `reserve`。
6
+ - 查找子串用 `find`,找不到返回 `npos`;不要用 `-1` 比较。
7
+ - 数值与文本互转用 `std::to_string` / `std::from_chars` / `std::format`;`std::stoi` 会抛异常且不报告消耗长度。
8
+
9
+ ## 依据
10
+
11
+ - [ISO C++ `[string.view]`](https://eel.is/c++draft/string.view):`string_view` 不拥有字符、不保证空字符结尾。
12
+ - [cppreference: `std::string_view`](https://en.cppreference.com/w/cpp/string/basic_string_view):生命周期与只读接口。
13
+ - [C++ Core Guidelines: SL.str.2](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#s-stdlib):用 `string_view` 传只读文本,用 `string` 保存文本。
@@ -0,0 +1,106 @@
1
+ # `std::format` 与输出
2
+
3
+ - 组合消息优先 `std::format`;它返回 `std::string`,不直接写流。
4
+ - 输出仍走 `std::cout` / `std::cerr` / 项目传入的 `std::ostream&`;C++23 且库支持时程序边界可用 `std::print`,C++20 及更早没有。
5
+ - 格式串里的 `{}` 按顺序取参数;需要位置时写 `{0}`、`{1}`。
6
+ - 自定义类型要格式化时,特化 `std::formatter<T>`;写法与边界见下面的「自定义类型的格式化」。
7
+ - 浮点默认格式可能与预期不同;需要固定精度时写 `{:.2f}`,需要科学计数时写 `{:e}`。
8
+ - 异常消息、日志、用户界面文本都通过 `std::format` 组合,不手工拼接数字和字符串。异常消息的组织方式见 `references/exceptions/throw-rules.md` 的「消息」节;面向用户的中文只留在程序入口的前缀里。
9
+
10
+ ## 自定义类型的格式化
11
+
12
+ - 在类型所在的头文件里特化 `std::formatter<T>`,与类型声明放在一起;输出属于类型合同,不散落在调用点的 `operator<<` 或手工拼接里。
13
+ - 特化必须在首次使用前可见。`std::formatter<T>` 默认是 disabled;没有特化时 `std::format("{}", value)` 不能编译,不是运行时回退。
14
+ - `parse` 只接受默认格式:从 `ctx.begin()` 读到 `}` 或格式串结束;其他说明符抛 `std::format_error`,消息写清只支持默认格式。需要字段对齐、宽度等语法时再实现,不预埋。
15
+ - `format` 用 `std::format_to(ctx.out(), ...)` 输出;内部复用已有类型的 formatter,不重复拼接坐标或枚举文本。`Move` 只组合 `Player` 与 `Position`,不重新拼 `(row, col)`。
16
+ - 枚举的非法值要有稳定回退,例如 `Player(9)`;不要输出空串,也不要让调用方从越界里猜原因。
17
+ - 输出形态短且稳定,例如 `X(1, 2)`;异常消息与测试都用 `std::format("{}", value)` 直接断言。
18
+
19
+ ### formatter 的四个部分
20
+
21
+ | 部分 | 契约 |
22
+ | --- | --- |
23
+ | 特化 | `template <> struct std::formatter<T>`;满足 BasicFormatter:可默认构造、拷贝、赋值、析构、交换;在首次使用前可见 |
24
+ | `parse` | 参数 `std::format_parse_context&`;从 `ctx.begin()` 解析到 `}` 或末尾;只接受默认格式时检查首字符,其他抛 `std::format_error`;返回指向 `}` 的迭代器 |
25
+ | `format` | `template <typename FormatContext> auto format(const T&, FormatContext&) const`;写入 `ctx.out()`,返回输出末尾迭代器 |
26
+ | 失败路径 | 非法枚举值给稳定回退;`parse` 的错误用 `std::format_error` |
27
+
28
+ - 空格式说明符有两种合法形态:`ctx.begin() == ctx.end()`,或 `*ctx.begin() == '}'`;两者都表示没有额外说明符。
29
+ - `format` 的 `const` 与模板参数都是契约的一部分:`std::format` 与 `std::format_to` 传入不同的 `FormatContext`;写死 `std::format_context` 只能配合返回 `std::string` 的 `std::format`。
30
+ - 输出只依赖值、`ctx.locale()`、`ctx.arg(n)` 和上次 `parse` 保存的说明符;不要在 `format` 里读全局状态或修改 formatter。
31
+ - `parse` 直接返回 `ctx.begin()` 只适用于「只接受默认格式」;要拒绝并给出消息时,先检查首字符是不是 `}`。
32
+ - `format_to(ctx.out(), ...)` 直接写入调用方的输出迭代器,避免为每个字段构造临时 `std::string`;需要在函数边界得到完整字符串时才用 `std::format`。
33
+
34
+ ```cpp
35
+ template <>
36
+ struct std::formatter<tictactoe::Player> {
37
+ constexpr auto parse(std::format_parse_context& ctx) {
38
+ if (ctx.begin() != ctx.end() && *ctx.begin() != '}') {
39
+ throw std::format_error("Player: format specifier is not supported, use {} instead");
40
+ }
41
+ return ctx.begin();
42
+ }
43
+
44
+ template <typename FormatContext>
45
+ auto format(tictactoe::Player player, FormatContext& ctx) const {
46
+ switch (player) {
47
+ case tictactoe::Player::kX:
48
+ return std::format_to(ctx.out(), "X");
49
+ case tictactoe::Player::kO:
50
+ return std::format_to(ctx.out(), "O");
51
+ }
52
+ return std::format_to(ctx.out(), "Player({})", static_cast<int>(player));
53
+ }
54
+ };
55
+ ```
56
+
57
+ ## 枚举值与整数输出
58
+
59
+ `std::format` 没有为枚举提供内建 formatter;没有特化时 `std::format("{}", Player::kX)` 不能编译。枚举的数值回退要先把值转成整数,再按整数的格式说明输出;转换目标不同,`{}` 的结果可能不同。
60
+
61
+ | 写法 | 转换后的类型 | `"{}"` | `"{:d}"` | 说明 |
62
+ | --- | --- | --- | --- | --- |
63
+ | `static_cast<int>(player)` | `int` | `9` | `9` | 值在 `int` 范围时最简;底层是 `char`/`bool` 也不会变成字符或 `true` |
64
+ | `static_cast<std::underlying_type_t<Player>>(player)` | `std::uint8_t` | `9` | `9` | `uint8_t` 是 `unsigned char`,默认按十进制;底层是 `char`/`bool` 时 `{}` 会变成字符或 `true` |
65
+ | `std::to_underlying(player)` | `std::uint8_t` | `9` | `9` | C++23,等价于 underlying 转换 |
66
+ | `static_cast<char>(player)` | `char` | 字符 | 数字 | 不要用 `{}` 输出数字 |
67
+ | `static_cast<bool>(player)` | `bool` | `true`/`false` | `1`/`0` | 不要把枚举转 `bool` 输出数字 |
68
+
69
+ - 标准格式说明:`char`/`wchar_t` 的默认 presentation 是复制字符;`bool` 的默认 presentation 是 `true`/`false`;其他整数默认是 `d`。`{:d}` 对 `char`/`bool` 也按整数输出,`char` 先转 `unsigned char`。
70
+ - 所以 `static_cast<std::underlying_type_t<Player>>` 只在底层是普通整数时安全;底层可能是 `char`/`bool` 时,要用 `{:d}` 才能保证十进制。
71
+ - `static_cast<int>` 的边界是值域:枚举值超过 `int`(例如底层为 `std::uint64_t`)时可能窄化。此时用 `std::to_underlying(player)` 配 `{:d}`,或转 `long long`。
72
+ - `Player` 的取值只有 `kX`/`kO` 和少量非法值,`static_cast<int>` 足够;本项目保留这种写法,不为通用性加模板或分支。回退格式串写成 `Player({})`,只输出裸数字会和普通计数混淆。
73
+ - 不用 C 风格转换;Google 的 Casting 规则要求 `static_cast` 或花括号初始化,值转换用 `static_cast`。
74
+
75
+ 如果底层类型可能变化,回退写成强制十进制的通用形式:
76
+
77
+ ```cpp
78
+ // C++23
79
+ return std::format_to(ctx.out(), "Player({:d})", std::to_underlying(player));
80
+
81
+ // C++20
82
+ return std::format_to(ctx.out(), "Player({:d})",
83
+ static_cast<std::underlying_type_t<tictactoe::Player>>(player));
84
+ ```
85
+
86
+ ## 标准、提案与生态
87
+
88
+ - C++20 引入 `std::format` 与 `std::formatter`;标准库为字符、字符串、整数、浮点、指针与 `nullptr` 等提供 enabled 特化,没有枚举。`formatter<T>` 默认 disabled;自定义类型必须特化。
89
+ - C++23 增加 `std::to_underlying`(`<utility>`,等价 `static_cast<std::underlying_type_t<Enum>>`);字符串与字符的格式说明增加 `?` 调试表示;仍然没有枚举的内建 formatter。
90
+ - P3070R2 提议 `format_as` 作为枚举的 ADL 扩展点:`auto format_as(film f) { return std::to_underlying(f); }`,也可返回 `std::string_view`;在 type erasure 前转换,提案给出的测量约为 2× 快。尚未进入标准;fmt 已支持。
91
+ - P4267R0 用 C++26 reflection/annotations 提议 `[[=std::format_as_underlying]]` 与 `[[=std::format_as_identifier]]`,替代 `format_as`;尚未进入标准。
92
+ - fmt:`format_as` 或特化 `formatter`;`fmt::underlying(enum)`;推荐复用已有 formatter(继承/组合)。
93
+ - Abseil `StrFormat`:`%d`/`%i` 直接接受枚举,`%v` 用 `AbslStringify`。
94
+ - LLVM `formatv`:自定义用 `format_provider<T>` 特化;文档没有给出枚举专用入口。
95
+ - 结论:C++23 项目继续用 `std::formatter` 特化;数值回退优先 `static_cast<int>`(值域小)或 `std::to_underlying` + `{:d}`(值域大或底层不定);不要为枚举继承 `formatter<std::string_view>`。
96
+
97
+ ## 是否继承 `std::formatter<std::string_view>`
98
+
99
+ - 不需要。启用的特化只要满足 BasicFormatter:本身可默认构造、拷贝、赋值、析构、交换,并提供 `parse` 与 `format`;标准不要求继承任何 formatter。
100
+ - 继承或组合标准 formatter 的用途是复用格式说明符的解析。输出本质是字符串、并且确实要支持 `{:>10}`、`{:.3}` 这类字符串语法时才值得;`Position` 是最底层的坐标类型,把两个整数拼成 `(row, col)`,输出不是字符串值,继承 `formatter<std::string_view>` 只会让字符串说明符被接受,而 `format` 仍要自己拼文本。
101
+ - `Player` 与 `Move` 也不需要继承:`Player` 输出 `X`/`O` 符号,继承 `formatter<int>` 只会放宽可接受的说明符,输出仍由本类的 `format` 决定;`Move` 用 `"{}{}"` 组合 `Player` 与 `Position` 的 formatter。
102
+ - 现实中继承也确实在用:fmt 文档的 `formatter<color> : formatter<string_view>` 让 `parse` 转调基类、`format` 复用基类输出,只用于「输出是字符串」的类型,不是通用模板。
103
+ - 需要复用时,组合与继承都能工作。组合把 `std::formatter<std::string_view> base;` 作为成员,`parse` 转调 `base.parse(ctx)`,`format` 先拼临时字符串再 `base.format(temp, ctx)`,不改变本类型的继承关系;标准草案 [format.formatter.spec] 的示例则直接继承 `formatter<const char*>`。继承标准库类型只当薄适配层,不把它当多态基类。
104
+ - `format` 写成 `const`;LWG 3636 之后这是要求。
105
+
106
+ 依据:[cppreference: 标准格式说明](https://en.cppreference.com/w/cpp/utility/format/spec)、[cppreference: std::formatter](https://en.cppreference.com/w/cpp/utility/format/formatter)、[cppreference: BasicFormatter](https://en.cppreference.com/w/cpp/named_req/BasicFormatter)、[formatter.requirements](https://eel.is/c++draft/formatter.requirements)、[format.parse.ctx](https://eel.is/c++draft/format.parse.ctx)、[format.context](https://eel.is/c++draft/format.context)、[format.formatter.spec](https://eel.is/c++draft/format.formatter.spec)、[std::to_underlying](https://en.cppreference.com/w/cpp/utility/to_underlying)、[Google Casting](https://google.github.io/styleguide/cppguide.html#Casting)、[P3070R2](https://wg21.link/P3070R2)、[P4267R0](https://wg21.link/P4267R0)、[fmt API](https://fmt.dev/latest/api/#formatting-user-defined-types)、[Abseil StrFormat](https://abseil.io/docs/cpp/guides/format)、[LLVM formatv](https://llvm.org/docs/ProgrammersManual.html#formatting-strings)、[std::basic_format_string](https://en.cppreference.com/w/cpp/utility/format/basic_format_string)、[LWG 3636](https://cplusplus.github.io/LWG/issue3636)。
@@ -0,0 +1,13 @@
1
+ # 时间
2
+
3
+ - 测耗时用 `std::chrono::steady_clock`:单调、不受系统时间调整影响;`system_clock` 只用于日历时间,不用来算间隔。
4
+ - 两个时间点相减得到 `duration`;换算单位用 `duration_cast`,它会截断,需要舍入时用 C++20 的 `floor` / `ceil` / `round`。
5
+ - 需要毫秒、秒等具名单位时写 `std::chrono::milliseconds` 等别名,不手算 `1000`。
6
+ - `sleep_for` / `sleep_until` 至少睡到指定时长,调度可能让它更晚;不要用它做精确节拍。
7
+ - C++20 的日历与时区用 `<chrono>` 的 `year_month_day`、`zoned_time`;跨时区时间不要手工加减小时。
8
+
9
+ ## 依据
10
+
11
+ - [ISO C++ `[time]`](https://eel.is/c++draft/time):时钟、时长、时间点与日历类型。
12
+ - [cppreference: `std::chrono`](https://en.cppreference.com/w/cpp/chrono):`steady_clock`、`duration_cast` 与 C++20 日历/时区。
13
+ - [cppreference: `steady_clock`](https://en.cppreference.com/w/cpp/chrono/steady_clock):单调时钟的保证与用途。
@@ -0,0 +1,13 @@
1
+ # 随机
2
+
3
+ - 随机数用 `<random>` 的引擎 + 分布;不要用 `rand()` / `random_shuffle`。
4
+ - 引擎只播种一次并复用;每次调用都重新播种会让序列重复。线程各自持有引擎,或加锁。
5
+ - 需要均匀整数用 `uniform_int_distribution`;不要写 `engine() % n`,取模有偏。
6
+ - `random_device` 只用于播种,不保证密码学安全;可复现实验用固定种子或 `seed_seq`。
7
+ - 洗牌用 `std::shuffle(first, last, engine)`。
8
+
9
+ ## 依据
10
+
11
+ - [ISO C++ `[rand]`](https://eel.is/c++draft/rand):引擎、分布与随机数生成要求。
12
+ - [cppreference: Pseudo-random number generation](https://en.cppreference.com/w/cpp/numeric/random):引擎/分布对照与 `seed_seq`。
13
+ - [cppreference: `uniform_int_distribution`](https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution):均匀整数分布,避免取模偏差。
@@ -0,0 +1,14 @@
1
+ # 文件
2
+
3
+ - 文件用 `std::ifstream` / `std::ofstream`;构造后检查 `is_open()`,读写后检查流状态(`fail` / `bad`),不要假设成功。
4
+ - 文本按行读用 `std::getline`;二进制在 Windows 用 `std::ios::binary`,避免换行转换。
5
+ - 需要异常时先 `exceptions()` 设置掩码;默认 iostream 不抛异常。
6
+ - 路径用 `std::filesystem::path`,拼接用 `./`;遍历/查询用 `std::filesystem`,失败用 `error_code` 重载或捕获 `filesystem_error`。
7
+ - 写文件先写临时文件再改名,避免半写状态;需要原子替换时用 `rename`。
8
+
9
+ ## 依据
10
+
11
+ - [ISO C++ `[filesystems]`](https://eel.is/c++draft/filesystems):路径、目录遍历与文件状态。
12
+ - [cppreference: Filesystem library](https://en.cppreference.com/w/cpp/filesystem):`path`、`exists`、`error_code` 重载。
13
+ - [cppreference: `std::basic_fstream`](https://en.cppreference.com/w/cpp/io/basic_fstream):打开模式、流状态与 `exceptions()`。
14
+ - [C++ Core Guidelines: SL.io.1](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#s-stdlib):只在需要时用 iostream,检查每次 I/O 的结果。
@@ -0,0 +1,9 @@
1
+ # 检查清单
2
+
3
+ - [ ] 容器选择有明确理由,默认是 `vector`。
4
+ - [ ] 访问前有边界证明,或使用 `at`。
5
+ - [ ] 没有保存 `string_view` / `span` 指向的临时对象。
6
+ - [ ] 修改容器大小前处理了迭代器失效。
7
+ - [ ] 能用算法表达时没有手写下标循环。
8
+ - [ ] 组合消息用 `std::format` 或流输出;`std::print` 只出现在 C++23 的程序边界,库代码没有。
9
+ - [ ] 自定义类型的 formatter 只接受默认格式,非法枚举有稳定回退,输出可直接用 `std::format` 断言。
@@ -0,0 +1,17 @@
1
+ # 宽度、范围与 `std::size_t`
2
+
3
+ 类型不是存储格式的同义词。先写清一个值允许什么、禁止什么,再选 `int`、`std::int32_t`、`enum class` 还是包装类型。
4
+
5
+ - C++ 只保证基础整数类型的最小范围,不保证 `int` 固定 32 位,也不保证 `long` 一定宽于 `int`。
6
+ - `sizeof(T)` 返回 `std::size_t`;`<limits>` 的 `std::numeric_limits<T>` 查询最大值、最小值和特殊值。
7
+ - 协议、文件格式或跨平台布局要求固定宽度时,用 `<cstdint>` 的 `std::int32_t`、`std::uint64_t`,并确认目标平台提供该类型。
8
+ - `char` 是否有符号由实现决定;存字符时不要把它当成固定范围的小整数。
9
+ - 整数字面量的类型由后缀和候选类型决定;宽度或符号属于接口时,写出目标类型并检查转换。
10
+
11
+ `std::size_t` 是无符号类型,适合对象大小和容器下标。把它和负的 `int` 比较会先转成无符号,结果与直觉相反。统一下标类型,或先验证非负再比较。
12
+
13
+ ## 依据
14
+
15
+ - ISO C++ `[basic.fundamental]`:整数类型的最小范围、`char` 的符号由实现决定。https://eel.is/c++draft/basic.fundamental
16
+ - cppreference `std::size_t`:无符号、足以表示对象大小。https://en.cppreference.com/w/cpp/types/size_t
17
+ - Google C++ Style, Integer Types:默认用 `int`,固定宽度用 `<cstdint>`,不用无符号类型表达“不会为负”。https://google.github.io/styleguide/cppguide.html#Integer_Types
@@ -0,0 +1,26 @@
1
+ # 有符号、无符号与溢出
2
+
3
+ - 普通整数类型默认有符号;只有确有非负语义且理解边界时才用无符号。
4
+ - 无符号运算按模 `2^N` 回绕,这是有定义的行为;有符号整数溢出是未定义行为。
5
+ - 数组越界、空指针解引用、读取未初始化变量都属于未定义行为。编译器可以假设它们不发生,优化后的结果可能完全不符合直觉。
6
+ - 两边都是整数时,`./` 向零截断,`%` 给余数;`7 / 2` 是 `3`。整数除以零是未定义行为,不能与浮点的 `inf` / `nan` 混为一谈。
7
+ - 浮点转整数会截断小数;大整数转小整数可能丢高位或改变符号。先检查范围,再 `static_cast`。
8
+ - 禁止 C 风格强转 `(int)value`;它隐藏转换类别,`google-readability-casting` 会报。
9
+
10
+ ```cpp
11
+ int CountWide(const std::vector<int>& values) {
12
+ int count = 0;
13
+ for (std::size_t index = 0; index < values.size(); ++index) {
14
+ if (values[index] > 0) ++count;
15
+ }
16
+ return count;
17
+ }
18
+ ```
19
+
20
+ 循环下标与容器大小都用 `std::size_t`,避免混合比较。业务编号与坐标另按下面的约定。
21
+
22
+ ## 依据
23
+
24
+ - ISO C++ `[basic.fundamental]` 与 `[expr.mul]`:无符号按模回绕,有符号溢出是未定义行为,整数除法向零截断。https://eel.is/c++draft/expr.mul
25
+ - cppreference 算术运算符:整数提升、常用算术转换与回绕规则。https://en.cppreference.com/w/cpp/language/operator_arithmetic
26
+ - Google C++ Style, Integer Types:转换与提升可能丢失信息,混用有符号/无符号要小心。https://google.github.io/styleguide/cppguide.html#Integer_Types
@@ -0,0 +1,38 @@
1
+ # 项目对整型角色的约定
2
+
3
+ | 角色 | 类型 | 理由 |
4
+ | --- | --- | --- |
5
+ | 不能为负的尺寸、容器长度、`std::array` 长度 | `std::size_t` | 与 STL 和 NTTP 一致 |
6
+ | 坐标等允许表示范围外取值的下标 | `int` | 能表达负值;越界判断交给持有尺寸的类型 |
7
+ | 没有上面两种理由的整数 | `int` | 宽度足够,写起来最短 |
8
+ | 取值很少的 `enum class` 的底层类型 | `std::uint8_t` | 只表达取值集合和布局,不参与算术 |
9
+
10
+ - 协议、文件格式与跨平台布局仍用 `<cstdint>` 的定宽整数(如 `std::int32_t`);工程内普通代码不必写定宽类型。
11
+ - 不用裸 `int` 承担跨平台协议或布局;它的宽度不可预测。
12
+ - 不用 `std::uint8_t` / `std::uint16_t` 当业务编号;负值无法表达,整数提升还会制造意外。
13
+ - 常量容器长度由有符号整数相乘得到时,操作数都是编译期常量,先相乘再整体转换:
14
+
15
+ ```cpp
16
+ inline constexpr int kBatchSize = 3;
17
+ inline constexpr int kItemsPerBatch = 3;
18
+ inline constexpr std::size_t kItemCount =
19
+ static_cast<std::size_t>(kBatchSize * kItemsPerBatch);
20
+ std::array<TaskState, kItemCount> items_{};
21
+ ```
22
+
23
+ 溢出在常量表达式求值时就暴露,不必逐个转换;写成 `static_cast<std::size_t>(kBatchSize) * static_cast<std::size_t>(kItemsPerBatch)` 反而把同一件事拆成两次转换。
24
+
25
+ - 操作数里只要有一个是运行期值(普通 `int` 变量、参数或读到的数字),就逐个拓宽再乘加;先算后转会把乘加留在 `int` 里,溢出发生在转换之前。不要为了通过检查而关闭它,也不要改业务编号的类型。
26
+
27
+ ```cpp
28
+ // 不要:乘加先在 int 里完成,溢出发生在转换之前
29
+ return static_cast<std::size_t>(row * column_count + col);
30
+
31
+ // 要:每个操作数先拓宽,乘加在 std::size_t 里做
32
+ return static_cast<std::size_t>(row) * static_cast<std::size_t>(column_count) +
33
+ static_cast<std::size_t>(col);
34
+ ```
35
+
36
+ - 编译期常量走整体转换,运行期值逐个拓宽;尺寸很小时两者结果相同,规则约束的是尺寸或坐标来源变化后的行为。有符号 `int` 溢出是未定义行为,拓宽后整个表达式按无符号回绕,行为有定义。
37
+ - `std::size_t{row}` 不能替代 `static_cast`:`row` 可能为负且不是常量表达式,花括号初始化会因窄化编译失败。先检查范围,再转换。
38
+ - 工程另开 `bugprone-implicit-widening-of-multiplication-result` 时,先算后转会被这条检查报出;项目模板默认不启用 `bugprone-*`,本规则仍要遵守。
@@ -0,0 +1,24 @@
1
+ # 枚举类
2
+
3
+ `enum class` 定义封闭取值集合:
4
+
5
+ - 枚举值只在类型作用域内可见:`TaskState::kPending`,不写 `kPending`。
6
+ - 与整数之间不发生隐式转换;需要编号时写 `static_cast<int>(state)`,只用于输出或既有接口。
7
+ - 同类型值可以比较和赋值;不能参与算术。
8
+ - 需要固定布局时写底层类型。编号要在头文件里直接读到,就逐个写出来,不靠第一项默认为 0:`enum class TaskState : std::uint8_t { kPending = 0, kActive = 1, kDone = 2 };`
9
+
10
+ 约定:
11
+
12
+ - 新代码用 `enum class`;unscoped `enum` 只用于兼容 C 接口,不作为默认写法。
13
+ - 需要固定布局、序列化或前向声明时写底层类型;本项目的封闭状态码默认写 `: std::uint8_t`,普通枚举用默认底层类型。改动枚举值或底层类型要同步序列化、测试与注释。
14
+ - 枚举值的行尾注释要么全写要么全不写;两种句式与禁止项见 `references/comments/comment-rules.md`「枚举项与 `struct` 短字段:两种句式」。
15
+ - 需要“编号”与“名字”两套表示时,写显式映射函数,例如 `TaskStateToName` / `TaskStateFromName`,不要在枚举和整数之间来回隐式转换。
16
+ - 编号按“正常优先”排:0 表示正常态——空、进行中或成功;失败与异常态随后,按检查顺序排列。带缺省语义的枚举(`kEmpty`、`kInProgress`)和返回结果的枚举(`kSuccess`、`kRejected`)因此共用同一条约定。身份枚举按领域编号,如 `Player::kX = 1`、`Player::kO = 2`,不套用 0 = 正常态。
17
+ - 编号只用于输出和既有接口;要从编号找回名字,仍走显式映射函数,不拿编号做算术。
18
+
19
+ ## 标准与推荐
20
+
21
+ - ISO C++ `[dcl.enum]`:`enum class` 的作用域、不隐式转换与底层类型规则。https://eel.is/c++draft/dcl.enum
22
+ - Google C++ Style, Enumerator Names:枚举子按常量命名(`kEnumName`),不用宏风格。https://google.github.io/styleguide/cppguide.html#Enumerator_Names
23
+ - C++ Core Guidelines `Enum.1`/`Enum.3` 建议用枚举代替宏、优先 `enum class`;`Enum.7`/`Enum.8` 建议底层类型与显式编号只在必要时写。本项目为稳定存储与输出固定 `uint8_t` 并显式编号,是有意偏离,改动时要同步序列化与测试。https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#s-enum
24
+ - cppreference `std::error_code`:`clear()` 把值置为 0,`operator bool` 只把非 0 当作错误;结果编号沿用 0 = 成功。https://en.cppreference.com/w/cpp/error/error_code
@@ -0,0 +1,17 @@
1
+ # 强类型包装
2
+
3
+ 同一底层类型承载不同领域含义时,轻量包装能把“不该混用”变成编译错误:
4
+
5
+ ```cpp
6
+ struct Meters {
7
+ double value{0.0};
8
+ };
9
+
10
+ struct Seconds {
11
+ double value{0.0};
12
+ };
13
+ ```
14
+
15
+ - 包装是聚合,成员公开,仍可构造不合理值;需要不变量时改用 `class` 并在构造函数检查。
16
+ - 取值封闭的领域值用 `enum class`,不要为了“强类型”再包一层。
17
+ - 包装的代价是取值要写 `.value`;只在确实存在混用风险时使用,不为每个 `int` 造类型。
@@ -0,0 +1,22 @@
1
+ # 成员函数尾部的 `const`
2
+
3
+ ```cpp
4
+ class TaskBoard {
5
+ public:
6
+ [[nodiscard]] TaskState GetTaskState(TaskId id) const;
7
+ void SetTaskState(TaskId id, TaskState state);
8
+
9
+ private:
10
+ std::array<TaskState, kTaskCount> tasks_{};
11
+ };
12
+ ```
13
+
14
+ - 尾部 `const` 承诺不修改实例自身;与参数上的 `const`、返回类型上的 `const`、局部变量的 `const` 各管一段。
15
+ - 查询型成员一律标尾部 `const`;修改型成员不标,误标会被编译器拒绝。
16
+ - 返回成员容器的只读视图时,返回 `const` 引用或按值返回;不要返回可变内部引用。
17
+ - `mutable` 只用于缓存、互斥量等“逻辑 const”场景,并在注释里写理由;不要用它绕过正常的 const 设计。
18
+
19
+ ## 依据
20
+
21
+ - Google C++ Style, Use of `const`:成员函数不修改对象时就标 `const`。https://google.github.io/styleguide/cppguide.html#Use_of_const
22
+ - cppreference 成员函数:尾部 `const` 限定符与重载规则。https://en.cppreference.com/w/cpp/language/member_functions
@@ -0,0 +1,15 @@
1
+ # `[[nodiscard]]`
2
+
3
+ - 只给“结果携带信息、忽略会出错”的函数:查询型 `const` 成员与返回结果对象的 API。
4
+ - 不给所有非 `void` 函数机械添加;标注的价值来自稀缺。
5
+ - 写法位置:`[[nodiscard]]` 在返回类型前面,然后才是 `static` 或 `constexpr`;参数列表之后才是 `const`、`override`、`noexcept`。
6
+ - 类内 `friend` 不加 `[[nodiscard]]`:Clang 拒绝 `[[nodiscard]] friend`、`friend [[nodiscard]]` 和参数列表后的 `[[nodiscard]]`。不要再猜下一种写法。
7
+ - 运算符不加 `[[nodiscard]]`,成员与 `friend` 都一样。比较的参数不具名:成员写 `bool operator==(const T&) const = default`,`friend` 写 `friend bool operator==(const T&, const T&);`;注释不给这个不具名参数写 `@param`。
8
+ - 不要用 `static_cast<void>` 丢弃 `[[nodiscard]]` 的结果:先接住返回值并分支,把失败分支写出来;不能处理的失败说明接口或合同有问题,不是转换能解决的。测试里验证抛异常时,用 `[[maybe_unused]]` 的局部变量接住,不要用 `static_cast<void>` 跳过约定。
9
+ - 测试里验证“必须处理结果”时,同时验证忽略能被编译器发现。
10
+
11
+ ## 依据
12
+
13
+ - ISO C++ `[dcl.attr.nodiscard]`:`[[nodiscard]]` 的位置与可应用的对象。https://eel.is/c++draft/dcl.attr.nodiscard
14
+ - cppreference `[[nodiscard]]`:函数、类与枚举上的用法。https://en.cppreference.com/w/cpp/language/attributes/nodiscard
15
+ - 本项目的 Clang 验证(C++23):类内 `[[nodiscard]] friend`、`friend [[nodiscard]]`、参数列表后的写法都会被拒绝;只有返回类型前的普通函数声明可加。不要把“能编译”当成标准保证。
@@ -0,0 +1,10 @@
1
+ # 比较运算符
2
+
3
+ - 默认比较只给调用方真的会比较的值类型:`Position`、`Move` 这类坐标与记录可以默认比较;`Board` 这类实体在正常流程里不比较,不因为成员可比较就提供 `operator==`。测试要比较实体时,逐格或逐字段比较它的可观察状态,不把比较运算符加进生产接口。
4
+ - 签名与 `[[nodiscard]]` 规则见上一节。
5
+
6
+ ## 标准与推荐
7
+
8
+ - ISO C++ `[class.compare.default]`:默认 `operator==` 与 `operator<=>` 的逐成员语义。https://eel.is/c++draft/class.compare.default
9
+ - ISO C++ `[expr.eq]`:相等比较的类型与结果规则。https://eel.is/c++draft/expr.eq
10
+ - 需要排序或三路比较时写 `auto operator<=>(const T&) const = default;`,不要手写四个关系运算符;`<=>` 不自动提供 `==`,需要同时默认 `==`。只给真正会比较的值类型提供比较,实体类不因成员可比较就获得比较运算符。https://en.cppreference.com/w/cpp/language/operator_comparison
@@ -0,0 +1,15 @@
1
+ # 下标与 `at`
2
+
3
+ 同一容器上,`operator[]` 与 `at` 的失败方式不同:
4
+
5
+ | 族 | 容器 | `[]` 越界 | `at` 越界 |
6
+ | --- | --- | --- | --- |
7
+ | 按下标 | `array`、`vector`、`deque`、`string`、`string_view` | 未定义行为 | 抛 `std::out_of_range` |
8
+ | 按键 | `map`、`unordered_map` | 缺键则插入默认值 | 缺键则抛 `std::out_of_range` |
9
+ | 无下标 | `set`、`unordered_set`、`multimap` | 无 `operator[]` / `at` | — |
10
+
11
+ - 默认用 `at()`,越界时抛出 `std::out_of_range`,而不是未定义行为;只有调用点已经证明下标合法、并且这条路径对性能敏感时,才用 `[]`,同时保留证明依据。坐标类型不拥有容器区间;区间判断属于持有尺寸的类型(如 `Board::IsInRange`)。`std::span` 在 C++20 没有 `at`,`span::at` 到 C++26 才进入标准。
12
+ - 已经证明非负的坐标在 `[]` 前收成 `std::size_t`:写 `static_cast<std::size_t>(index)`,不要先转成 `int`;默认仍用 `at()`,只有选择 `[]` 时才做这一步。
13
+ - 行列下标是乘加结果时,先把各操作数拓宽再乘加,不要先乘加再转换;见 `references/language/value-types/03-integer-roles.md`。
14
+ - `map::operator[]` 会插入,不是“读取可能缺失的键”;只读查询用 `find` / `contains` / `at`。
15
+ - 契约失败的消息用 `std::format`,见 `references/exceptions/throw-rules.md`。
@@ -0,0 +1,13 @@
1
+ # 浮点与布尔
2
+
3
+ - 二进制浮点通常不能精确表示十进制小数,`0.1 + 0.2` 不要直接与 `0.3` 用 `==` 比较。
4
+ - 比较计算结果时使用与量纲、误差预算匹配的容差;单独处理 `NaN` 与无穷大。
5
+ - 金额存最小货币单位的整数(如“分”);折扣率、比例、物理量可用 `double`,但要写清精度边界。
6
+ - 整数 `0` 转 `false`,非 `0` 转 `true`;流默认输出 `0` / `1`,需要可读文本时先写 `std::boolalpha`。
7
+ - 不要把 `bool` 当成承载多个状态的整数;互斥状态用 `enum class`。
8
+
9
+ ## 依据
10
+
11
+ - ISO C++ `[basic.fundamental]`:浮点表示不保证能精确表示十进制小数。https://eel.is/c++draft/basic.fundamental
12
+ - cppreference `std::numeric_limits`:`epsilon`、`infinity`、`quiet_NaN` 与浮点分类。https://en.cppreference.com/w/cpp/types/numeric_limits
13
+ - cppreference `std::boolalpha`:流默认把 `bool` 输出为 `0`/`1`,需要可读文本时切换。https://en.cppreference.com/w/cpp/io/manip/boolalpha
@@ -0,0 +1,8 @@
1
+ # 检查清单
2
+
3
+ - [ ] 参与协议、布局或跨平台边界的整数有明确宽度。
4
+ - [ ] 业务编号与容器下标没有混用有符号和无符号。
5
+ - [ ] 封闭状态集合用 `enum class`,没有裸整数状态码。
6
+ - [ ] 查询成员标了尾部 `const`;`[[nodiscard]]` 只出现在结果型 API,结果都有分支处理。
7
+ - [ ] 下标访问前有边界证明,或使用 `at`;乘加得到下标前先拓宽各操作数。
8
+ - [ ] 浮点比较有容差,金额没有用 `double` 结算。
@@ -1,6 +1,6 @@
1
1
  # 源码布局
2
2
 
3
- 布局服务于边界:库是纯的,程序是薄的,测试从公共接口进入。这里只定目录和文件职责;CMake target 由 `configuring-cmake` 的 `steps/01-targets.md` 定。
3
+ 布局服务于边界:库是纯的,程序是薄的,测试从公共接口进入。这里只定目录和文件职责;CMake target 由 `engineering` 的 `steps/01-targets.md` 定。
4
4
 
5
5
  ## 组件
6
6
 
@@ -49,11 +49,11 @@ app/
49
49
 
50
50
  ## 文件扩展名与归属
51
51
 
52
- 扩展名、槽位与守卫名以 `references/file-template.md` 为准;本文只定位置:公共头放 `include/`,私有头放 `src/`,实现文件放 `src/`,测试放 `tests/`。
52
+ 扩展名、槽位与守卫名以 `references/layout/02-file-template.md` 为准;本文只定位置:公共头放 `include/`,私有头放 `src/`,实现文件放 `src/`,测试放 `tests/`。
53
53
 
54
54
  ## 头文件契约
55
55
 
56
- 自包含、共享常量、`using namespace`、模板实现与 `<iostream>` 的规则见 `references/google-cpp-style.md`「头文件」和 `references/file-template.md`。本文只强调一条边界:公共头只暴露稳定契约,测试只包含公共头。
56
+ 自包含、共享常量、`using namespace`、模板实现与 `<iostream>` 的规则见 `references/style/google-cpp-style.md`「头文件」和 `references/layout/02-file-template.md`。本文只强调一条边界:公共头只暴露稳定契约,测试只包含公共头。
57
57
 
58
58
  ## 测试
59
59
 
@@ -68,6 +68,12 @@ app/
68
68
 
69
69
  1. 先定边界(第 2 步),列出公共头。
70
70
  2. 移动文件,不改行为;测试保持绿色。
71
- 3. 再改 CMake target(`configuring-cmake` 的 `steps/01-targets.md`),最后跑 `building-cpp` 的 `steps/03-check.md`。
71
+ 3. 再改 CMake target(`engineering` 的 `steps/01-targets.md`),最后跑 `building-cpp` 的 `steps/03-check.md`。
72
72
 
73
73
  一次只做一种迁移;不要把「拆库 + 改名 + 改行为」混在一起。
74
+
75
+ ## 依据
76
+
77
+ - [Google C++ Style Guide: File Names](https://google.github.io/styleguide/cppguide.html#File_Names):文件名全小写,`_` 或 `-` 分隔。
78
+ - [C++ Core Guidelines: NL.27](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#rl-file-suffix):代码文件用 `.cpp`,接口文件用 `.h`。
79
+ - [C++ Core Guidelines: SF.1](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#rs-file-suffix):按文件职责区分实现与接口。
@@ -1,6 +1,6 @@
1
1
  # 文件模板
2
2
 
3
- 本文件是头文件与实现文件槽位的唯一权威。`google-cpp-style.md` 只保留「路径式守卫、不用 `#pragma once`」的决定;`layout.md` 只定文件放在哪;`steps/03-declaration.md` 指向这里。类内声明顺序仍只写在 `declaration-order.md`,本文件不复制。
3
+ 本文件是头文件与实现文件槽位的唯一权威。`references/style/google-cpp-style.md` 只保留「路径式守卫、不用 `#pragma once`」的决定;`references/layout/01-source-layout.md` 只定文件放在哪;`steps/03-declaration.md` 指向这里。类内声明顺序仍只写在 `references/layout/03-declaration-order.md`,本文件不复制。
4
4
 
5
5
  ## 选择扩展名
6
6
 
@@ -28,9 +28,9 @@
28
28
  | --- | --- |
29
29
  | `tictactoe/include/board.h` | `TICTACTOE_INCLUDE_BOARD_H_` |
30
30
  | `tictactoe/include/game.h` | `TICTACTOE_INCLUDE_GAME_H_` |
31
- | `tictactoe/include/ai.h` | `TICTACTOE_INCLUDE_AI_H_` |
31
+ | `tictactoe/include/computer.h` | `TICTACTOE_INCLUDE_COMPUTER_H_` |
32
32
  | `apps/include/cli.h` | `APPS_INCLUDE_CLI_H_` |
33
- | `tests/supporting-tests.h` | `TESTS_SUPPORTING_TESTS_H_` |
33
+ | `tictactoe/tests/computer_test_support.h` | `TICTACTOE_TESTS_COMPUTER_TEST_SUPPORT_H_` |
34
34
 
35
35
  - 通用示例:`taskflow/include/task_table.h` → `TASKFLOW_INCLUDE_TASK_TABLE_H_`。
36
36
  - 不用 `TICTACTOE_BOARD_H` 这类省略路径的名字;两个同名头会撞宏。
@@ -39,7 +39,7 @@
39
39
 
40
40
  槽位顺序固定:
41
41
 
42
- 1. 文件头注释(格式见 `comments.md`,留在守卫外)
42
+ 1. 文件头注释(格式见 `references/comments/comment-rules.md`,留在守卫外)
43
43
  2. `#ifndef` / `#define`
44
44
  3. 分组包含(组间空行,组内字母序)
45
45
  4. 命名空间内:常量 → 枚举 → 结构体 → 自由函数 → 类
@@ -129,3 +129,9 @@ void ThingTable::Add(const Thing& thing) { things_.push_back(thing); }
129
129
  - 头文件能单独编译:只包含它的一个空 `.cpp` 必须通过。
130
130
  - 实现文件没有守卫,配对头在第一组。
131
131
  - 工程内没有 `#pragma once`,也没有守卫与 pragma 并存。
132
+
133
+ ## 依据
134
+
135
+ - [Google C++ Style Guide: Header Files](https://google.github.io/styleguide/cppguide.html#Header_Files):头文件自包含、包含顺序与路径式守卫。
136
+ - [Google C++ Style Guide: The #define Guard](https://google.github.io/styleguide/cppguide.html#The__define_Guard):`<PROJECT>_<PATH>_<FILE>_H_` 命名与唯一性要求。
137
+ - [C++ Core Guidelines: SF.8](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#rs-guards):所有 `.h` 文件使用包含守卫。