@chengzhao-dev/dsh-cpp-workflow 0.6.20 → 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 (173) hide show
  1. package/README.en.md +10 -25
  2. package/README.md +12 -27
  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/workspace.js +0 -15
  7. package/package.json +7 -7
  8. package/skills/building-cpp/SKILL.md +23 -23
  9. package/skills/building-cpp/assets/templates/projects/common/running-build.sh +5 -1
  10. package/skills/building-cpp/project/vscode/01-extensions.md +6 -0
  11. package/skills/building-cpp/project/vscode/02-settings.md +7 -1
  12. package/skills/building-cpp/references/build-script.md +10 -3
  13. package/skills/building-cpp/references/environment.md +8 -1
  14. package/skills/building-cpp/steps/01-build.md +4 -4
  15. package/skills/building-cpp/steps/02-tooling.md +10 -10
  16. package/skills/building-cpp/steps/03-check.md +13 -11
  17. package/skills/developing-cpp/SKILL.md +32 -36
  18. package/skills/developing-cpp/references/comments/comment-rules.md +181 -0
  19. package/skills/developing-cpp/references/comments/comment-style.md +80 -0
  20. package/skills/developing-cpp/{source/07-comments → references/comments}/examples/declaration-class.md +24 -2
  21. package/skills/developing-cpp/references/comments/examples/declaration-constructor.md +22 -0
  22. package/skills/developing-cpp/references/comments/examples/declaration-enum.md +48 -0
  23. package/skills/developing-cpp/{source/07-comments → references/comments}/examples/declaration-file.md +3 -3
  24. package/skills/developing-cpp/references/comments/examples/declaration-function.md +127 -0
  25. package/skills/developing-cpp/{source/07-comments → references/comments}/examples/declaration-member.md +7 -9
  26. package/skills/developing-cpp/{source/07-comments → references/comments}/examples/declaration-predicate.md +2 -1
  27. package/skills/developing-cpp/references/comments/examples/declaration-struct.md +18 -0
  28. package/skills/developing-cpp/references/comments/examples/implementation-body.md +64 -0
  29. package/skills/developing-cpp/references/comments/glossary/comment-terms.md +93 -0
  30. package/skills/developing-cpp/references/design/examples/move-result.md +39 -0
  31. package/skills/developing-cpp/{source/03-design → references/design}/examples/preview-read.md +7 -7
  32. package/skills/developing-cpp/{source/03-design/01-project-design.md → references/design/project-design.md} +57 -5
  33. package/skills/developing-cpp/{source/01-editing → references/editing}/01-edit-operations.md +4 -2
  34. package/skills/developing-cpp/{source/01-editing → references/editing}/02-rule-priority.md +2 -2
  35. package/skills/developing-cpp/{source/08-exceptions → references/exceptions}/examples/throw-build-mode.md +2 -2
  36. package/skills/developing-cpp/{source/08-exceptions → references/exceptions}/examples/throw-message.md +2 -3
  37. package/skills/developing-cpp/{source/08-exceptions/01-throw-rules.md → references/exceptions/throw-rules.md} +74 -21
  38. package/skills/developing-cpp/{source/05-language → references/language}/glossary/language-terms.md +2 -2
  39. package/skills/developing-cpp/{source/05-language/01-language-basics → references/language/language-basics}/01-objects.md +7 -1
  40. package/skills/developing-cpp/{source/05-language/01-language-basics → references/language/language-basics}/02-initialization.md +8 -1
  41. package/skills/developing-cpp/{source/05-language/01-language-basics → references/language/language-basics}/03-deduction.md +6 -0
  42. package/skills/developing-cpp/references/language/language-basics/04-const.md +25 -0
  43. package/skills/developing-cpp/references/language/language-basics/05-expressions.md +23 -0
  44. package/skills/developing-cpp/{source/05-language/01-language-basics → references/language/language-basics}/06-functions.md +7 -1
  45. package/skills/developing-cpp/{source/05-language/01-language-basics → references/language/language-basics}/07-classes.md +7 -1
  46. package/skills/developing-cpp/references/language/language-basics/08-raii.md +15 -0
  47. package/skills/developing-cpp/{source/05-language/01-language-basics → references/language/language-basics}/09-templates.md +6 -0
  48. package/skills/developing-cpp/{source/05-language/01-language-basics → references/language/language-basics}/10-checklist.md +2 -1
  49. package/skills/developing-cpp/references/language/performance/01-measure.md +22 -0
  50. package/skills/developing-cpp/{source/05-language/04-performance → references/language/performance}/02-complexity.md +6 -0
  51. package/skills/developing-cpp/{source/05-language/04-performance → references/language/performance}/03-memory.md +7 -1
  52. package/skills/developing-cpp/{source/05-language/04-performance → references/language/performance}/04-undefined-behavior.md +9 -1
  53. package/skills/developing-cpp/{source/05-language/04-performance → references/language/performance}/05-pitfalls.md +6 -0
  54. package/skills/developing-cpp/references/language/performance/06-false-optimizations.md +13 -0
  55. package/skills/developing-cpp/{source/05-language/03-standard-library → references/language/standard-library}/01-containers.md +6 -0
  56. package/skills/developing-cpp/{source/05-language/03-standard-library → references/language/standard-library}/02-access.md +7 -1
  57. package/skills/developing-cpp/{source/05-language/03-standard-library → references/language/standard-library}/03-iteration.md +6 -0
  58. package/skills/developing-cpp/{source/05-language/03-standard-library → references/language/standard-library}/04-algorithms.md +6 -0
  59. package/skills/developing-cpp/{source/05-language/03-standard-library → references/language/standard-library}/05-string.md +6 -0
  60. package/skills/developing-cpp/references/language/standard-library/06-format.md +106 -0
  61. package/skills/developing-cpp/references/language/standard-library/07-time.md +13 -0
  62. package/skills/developing-cpp/references/language/standard-library/08-random.md +13 -0
  63. package/skills/developing-cpp/references/language/standard-library/09-files.md +14 -0
  64. package/skills/developing-cpp/{source/05-language/02-value-types → references/language/value-types}/01-width.md +6 -0
  65. package/skills/developing-cpp/{source/05-language/02-value-types → references/language/value-types}/02-signed.md +7 -1
  66. package/skills/developing-cpp/references/language/value-types/03-integer-roles.md +38 -0
  67. package/skills/developing-cpp/references/language/value-types/04-enum.md +24 -0
  68. package/skills/developing-cpp/{source/05-language/02-value-types → references/language/value-types}/06-const-method.md +5 -0
  69. package/skills/developing-cpp/{source/05-language/02-value-types → references/language/value-types}/07-nodiscard.md +6 -0
  70. package/skills/developing-cpp/references/language/value-types/08-comparison.md +10 -0
  71. package/skills/developing-cpp/{source/05-language/02-value-types → references/language/value-types}/09-subscript.md +2 -1
  72. package/skills/developing-cpp/{source/05-language/02-value-types → references/language/value-types}/10-float-bool.md +6 -0
  73. package/skills/developing-cpp/{source/05-language/02-value-types → references/language/value-types}/11-checklist.md +1 -1
  74. package/skills/developing-cpp/{source/04-layout → references/layout}/01-source-layout.md +8 -2
  75. package/skills/developing-cpp/{source/04-layout → references/layout}/02-file-template.md +10 -4
  76. package/skills/developing-cpp/references/layout/03-declaration-order.md +138 -0
  77. package/skills/developing-cpp/references/naming/examples/naming-basic.md +3 -0
  78. package/skills/developing-cpp/{source/06-naming → references/naming}/examples/naming-lower-layer.md +9 -9
  79. package/skills/developing-cpp/references/naming/examples/naming-player-value.md +16 -0
  80. package/skills/developing-cpp/references/naming/examples/naming-state-change.md +57 -0
  81. package/skills/developing-cpp/{source/06-naming/01-identifier-names.md → references/naming/identifier-names.md} +34 -8
  82. package/skills/developing-cpp/{source/02-style/01-google-cpp-style.md → references/style/google-cpp-style.md} +11 -10
  83. package/skills/developing-cpp/scripts/checking-comments.mjs +71 -14
  84. package/skills/developing-cpp/scripts/checking-declaration-order.mjs +357 -0
  85. package/skills/developing-cpp/steps/01-behavior.md +5 -5
  86. package/skills/developing-cpp/steps/02-boundary.md +9 -9
  87. package/skills/developing-cpp/steps/03-declaration.md +16 -14
  88. package/skills/developing-cpp/steps/04-implementation.md +21 -17
  89. package/skills/engineering/SKILL.md +41 -38
  90. package/skills/engineering/{cmake → references/cmake}/assets/CompileSettings.cmake +2 -0
  91. package/skills/engineering/{cmake → references/cmake}/linking/01-libraries.md +6 -0
  92. package/skills/engineering/references/cmake/linking/02-rpath.md +25 -0
  93. package/skills/engineering/{cmake → references/cmake}/linking/03-undefined-reference.md +6 -0
  94. package/skills/engineering/{cmake → references/cmake}/linking/04-inspect.md +6 -0
  95. package/skills/engineering/{cmake/02-modules/01-functions.md → references/cmake/modules/functions.md} +8 -5
  96. package/skills/engineering/{cmake/03-standard/01-cxx-standard.md → references/cmake/standard/cxx-standard.md} +5 -4
  97. package/skills/engineering/{cmake/03-standard → references/cmake/standard}/standards/README.md +8 -8
  98. package/skills/engineering/{cmake/01-targets → references/cmake/targets}/examples/target.md +1 -1
  99. package/skills/engineering/{cmake/01-targets/01-rules.md → references/cmake/targets/rules.md} +18 -11
  100. package/skills/engineering/{cmake → references/cmake}/testing/ctest.md +9 -2
  101. package/skills/engineering/{formatting → references/formatting}/01-encoding.md +1 -1
  102. package/skills/engineering/{formatting → references/formatting}/02-clang-format.md +6 -0
  103. package/skills/engineering/{formatting → references/formatting}/03-clang-tidy.md +7 -0
  104. package/skills/engineering/{formatting → references/formatting}/04-clangd.md +7 -0
  105. package/skills/engineering/references/formatting/examples/clang-format.md +38 -0
  106. package/skills/engineering/{formatting → references/formatting}/examples/clang-tidy.md +0 -21
  107. package/skills/engineering/{cmake/steps → steps}/01-targets.md +6 -6
  108. package/skills/engineering/{cmake/steps → steps}/02-modules.md +4 -4
  109. package/skills/engineering/{cmake/steps → steps}/03-check.md +3 -2
  110. package/skills/developing-cpp/source/03-design/examples/move-result.md +0 -39
  111. package/skills/developing-cpp/source/04-layout/03-declaration-order.md +0 -118
  112. package/skills/developing-cpp/source/05-language/01-language-basics/04-const.md +0 -15
  113. package/skills/developing-cpp/source/05-language/01-language-basics/05-expressions.md +0 -8
  114. package/skills/developing-cpp/source/05-language/01-language-basics/08-raii.md +0 -9
  115. package/skills/developing-cpp/source/05-language/02-value-types/03-integer-roles.md +0 -23
  116. package/skills/developing-cpp/source/05-language/02-value-types/04-enum.md +0 -17
  117. package/skills/developing-cpp/source/05-language/02-value-types/08-comparison.md +0 -4
  118. package/skills/developing-cpp/source/05-language/03-standard-library/06-format.md +0 -44
  119. package/skills/developing-cpp/source/05-language/03-standard-library/07-time.md +0 -3
  120. package/skills/developing-cpp/source/05-language/03-standard-library/08-random.md +0 -3
  121. package/skills/developing-cpp/source/05-language/03-standard-library/09-files.md +0 -4
  122. package/skills/developing-cpp/source/05-language/04-performance/01-measure.md +0 -15
  123. package/skills/developing-cpp/source/05-language/04-performance/06-false-optimizations.md +0 -6
  124. package/skills/developing-cpp/source/06-naming/examples/naming-basic.md +0 -3
  125. package/skills/developing-cpp/source/06-naming/examples/naming-player-mark.md +0 -17
  126. package/skills/developing-cpp/source/06-naming/examples/naming-state-change.md +0 -57
  127. package/skills/developing-cpp/source/07-comments/01-comment-rules.md +0 -239
  128. package/skills/developing-cpp/source/07-comments/examples/declaration-constructor.md +0 -22
  129. package/skills/developing-cpp/source/07-comments/examples/declaration-enum.md +0 -24
  130. package/skills/developing-cpp/source/07-comments/examples/declaration-function.md +0 -100
  131. package/skills/developing-cpp/source/07-comments/examples/declaration-struct.md +0 -18
  132. package/skills/developing-cpp/source/07-comments/examples/implementation-body.md +0 -42
  133. package/skills/developing-cpp/source/07-comments/glossary/comment-terms.md +0 -75
  134. package/skills/engineering/cmake/linking/02-rpath.md +0 -17
  135. package/skills/engineering/formatting/examples/clang-format.md +0 -17
  136. /package/skills/developing-cpp/{source/07-comments → references/comments}/examples/implementation-todo.md +0 -0
  137. /package/skills/developing-cpp/{source/03-design → references/design}/examples/behavior-sentence.md +0 -0
  138. /package/skills/developing-cpp/{source/03-design → references/design}/examples/coordinate-table.md +0 -0
  139. /package/skills/developing-cpp/{source/03-design → references/design}/examples/header-place.md +0 -0
  140. /package/skills/developing-cpp/{source/03-design → references/design}/examples/library-boundary.md +0 -0
  141. /package/skills/developing-cpp/{source/03-design → references/design}/examples/library-split.md +0 -0
  142. /package/skills/developing-cpp/{source/08-exceptions → references/exceptions}/examples/throw-boundary.md +0 -0
  143. /package/skills/developing-cpp/{source/08-exceptions → references/exceptions}/examples/throw-catch.md +0 -0
  144. /package/skills/developing-cpp/{source/08-exceptions → references/exceptions}/examples/throw-destructor.md +0 -0
  145. /package/skills/developing-cpp/{source/08-exceptions → references/exceptions}/examples/throw-format.md +0 -0
  146. /package/skills/developing-cpp/{source/08-exceptions → references/exceptions}/examples/throw-when.md +0 -0
  147. /package/skills/developing-cpp/{source/05-language/04-performance → references/language/performance}/07-checklist.md +0 -0
  148. /package/skills/developing-cpp/{source/05-language/03-standard-library → references/language/standard-library}/10-checklist.md +0 -0
  149. /package/skills/developing-cpp/{source/05-language/02-value-types → references/language/value-types}/05-strong-type.md +0 -0
  150. /package/skills/developing-cpp/{source/06-naming → references/naming}/examples/naming-accessor.md +0 -0
  151. /package/skills/developing-cpp/{source/06-naming → references/naming}/examples/naming-current-prefix.md +0 -0
  152. /package/skills/developing-cpp/{source/06-naming → references/naming}/examples/naming-lookup.md +0 -0
  153. /package/skills/developing-cpp/{source/06-naming → references/naming}/examples/naming-multiple-return.md +0 -0
  154. /package/skills/developing-cpp/{source/06-naming → references/naming}/examples/naming-predicate.md +0 -0
  155. /package/skills/developing-cpp/{source/06-naming → references/naming}/examples/naming-rename.md +0 -0
  156. /package/skills/developing-cpp/{source/06-naming → references/naming}/examples/naming-responsibility.md +0 -0
  157. /package/skills/developing-cpp/{source/06-naming → references/naming}/examples/naming-role.md +0 -0
  158. /package/skills/engineering/{cmake → references/cmake}/assets/LinkSettings.cmake +0 -0
  159. /package/skills/engineering/{cmake/03-standard → references/cmake/standard}/examples/standard.md +0 -0
  160. /package/skills/engineering/{cmake/03-standard → references/cmake/standard}/standards/c++11/README.md +0 -0
  161. /package/skills/engineering/{cmake/03-standard → references/cmake/standard}/standards/c++11/cmake-snippet.cmake +0 -0
  162. /package/skills/engineering/{cmake/03-standard → references/cmake/standard}/standards/c++14/README.md +0 -0
  163. /package/skills/engineering/{cmake/03-standard → references/cmake/standard}/standards/c++14/cmake-snippet.cmake +0 -0
  164. /package/skills/engineering/{cmake/03-standard → references/cmake/standard}/standards/c++17/README.md +0 -0
  165. /package/skills/engineering/{cmake/03-standard → references/cmake/standard}/standards/c++17/cmake-snippet.cmake +0 -0
  166. /package/skills/engineering/{cmake/03-standard → references/cmake/standard}/standards/c++20/README.md +0 -0
  167. /package/skills/engineering/{cmake/03-standard → references/cmake/standard}/standards/c++20/cmake-snippet.cmake +0 -0
  168. /package/skills/engineering/{cmake/03-standard → references/cmake/standard}/standards/c++23/README.md +0 -0
  169. /package/skills/engineering/{cmake/03-standard → references/cmake/standard}/standards/c++23/cmake-snippet.cmake +0 -0
  170. /package/skills/engineering/{cmake/01-targets → references/cmake/targets}/examples/glob.md +0 -0
  171. /package/skills/engineering/{cmake/01-targets → references/cmake/targets}/examples/rpath.md +0 -0
  172. /package/skills/engineering/{formatting → references/formatting}/examples/clangd.md +0 -0
  173. /package/skills/engineering/{formatting → references/formatting}/scripts/checking-encoding.mjs +0 -0
@@ -24,7 +24,7 @@
24
24
  - 位置是行和列,不是格子的另一种取值。胜负、轮到谁、已下过的序列属于棋盘或对局,不要塞进位置类型。坐标类型单独一个头文件;持有尺寸的类型包含它,不把坐标塞回尺寸类型里。
25
25
  - 有不变量的格子用公开构造的类,默认是空,X 或 O 至多写入一次。私有构造函数只留给必须经过工厂才能建立的对象,不用来表达三种取值,也不用来藏起行和列。
26
26
  - 一次写入能区分的失败都放进结果枚举。传入空取值是结果项,不抛异常。
27
- - 只由某一个类型产生的结果枚举嵌在该类型里:格子的放置结果和对局的落子结果取值不同,不合成命名空间里的一个 `PlaceResult`。
27
+ - 只由某一个类型产生的结果枚举嵌在该类型里:格子的放置结果和对局的落子结果取值不同,不合成一个 `PlaceResult` 或 `PlaceResult`。
28
28
  - 调用方已经把完整值对象交进来时,下层直接用它,不再用散参数或查询重新拼出同一对象。可以校验,并在不符时返回原因;不要静默替换成自己重算的值。
29
29
  - 构造值对象所需的只读查询留在拥有状态的一层;调用方用它组值对象,下层函数不重复推导。不要为了去掉查询,让每个调用方各自维护轮次副本。
30
30
  - 范围检查在接收位置的那一层直接做,越界直接抛出;不要为了统一诊断把 `std::source_location` 穿过每个公共接口。
@@ -40,8 +40,13 @@
40
40
  - 状态用连续取值的 `enum class`:`kPending`、`kActive`、`kDone`。
41
41
  - 状态转换写成显式函数或规则对象,不用一串 `if` 直接改整数。
42
42
  - “动作是否合法”和“动作执行结果”分开:合法性查询返回 `bool` 或原因,执行返回结果对象。
43
- - 失败是调用方的正常结果(编号不存在、输入格式不对)时用返回值表达;函数无法完成自己的任务、或状态机进入不可能分支时抛异常,规则见 `source/08-exceptions/01-throw-rules.md`。
44
- - 结果对象携带足够信息让调用方决定下一步,不要只返回 `bool` 再让调用方猜原因。
43
+ - 失败是调用方的正常结果(编号不存在、输入格式不对)时用返回值表达;函数无法完成自己的任务、或状态机进入不可能分支时抛异常,规则见 `references/exceptions/throw-rules.md`。
44
+ - 结果对象携带足够信息让调用方决定下一步;失败原因不止一种时不要只返回 `bool` 再让调用方猜原因。
45
+ - 动作只有一个失败原因、调用方也不需要区分时返回 `bool`,`@return` 写清 true 与 false 各自的含义,例如「撤回成功返回 true;没有可撤回的落子时返回 false」;不要为单一失败建只有一个失败取值的枚举。
46
+ - 结果枚举先列成功(编号 0),再按检查顺序列失败:`kSuccess = 0`、`kDenied = 1`、`kRejected = 2`。
47
+ - 每类能区分的失败单独一个取值,不合并成 `kInvalid`;调用方与测试才知道究竟哪条规则拒绝。
48
+ - 终局、已占用、轮到别人这类「现在不能落子」是正常结果项,不抛异常;异常只留给违反前置条件(传空、手里没有这一手)。
49
+ - 调用方穷举枚举时不写 `default`,让 `-Wswitch` 在新增取值时报错。一个值对多个离散取值分派时用 `switch`;范围比较、布尔组合或「失败提前返回、成功落到末尾」的守卫用 `if`,末尾直接写成功路径,不补 `throw` 兜底。
45
50
 
46
51
  ## 规则核心与外围
47
52
 
@@ -79,7 +84,7 @@
79
84
  - 平台变化(Windows/WSL/macOS 路径、动态库查找)放构建脚本或适配层,不进核心。
80
85
  - 只有调用方应当替换的外部依赖(时钟、随机源、输入输出)才通过接口注入;组件自己规定的策略留在实现里,测试断言可观察结果。
81
86
 
82
- 公共头文件只暴露稳定契约;实现细节放 `.cpp` 或私有头。目录形态、扩展名与头文件槽位以 `source/04-layout/01-source-layout.md` 和 `source/04-layout/02-file-template.md` 为准。
87
+ 公共头文件只暴露稳定契约;实现细节放 `.cpp` 或私有头。目录形态、扩展名与头文件槽位以 `references/layout/01-source-layout.md` 和 `references/layout/02-file-template.md` 为准。
83
88
 
84
89
  ## 什么时候抽共享库
85
90
 
@@ -95,11 +100,58 @@
95
100
  - 用户回答后,把决定写进项目技能的行为表或测试名;下次遇到同一歧义不再猜。
96
101
  - 不能问时,选更保守的解释(不产生非法状态、不丢信息),并在交付说明里标注假设。
97
102
 
103
+ ## 函数接口设计
104
+
105
+ - 一个函数只做一件事;同一动作的不同形态(是否传坐标、是否传选项)优先用默认参数合并,不写多份实现。默认参数要让调用点读得懂,如 `void Clear(std::optional<Position> pos = std::nullopt);`:省略坐标时清空整盘,传入时只清那一格。
106
+ - 语义不同的动作不要共用名字,也不要靠重载区分(Google:调用点必须能直接看出在调用什么);重载只用于语义相同的变体。
107
+ - 不用 `bool` 参数切换行为;两个分支做不同的事时拆成两个函数,或把开关提成有名字的枚举。
108
+ - 参数顺序:被操作的对象在前,控制量与选项在后;输出参数放最后并标 `[out]`。
109
+ - 公共接口只保留调用方需要的形态;测试专用的入口不放公共头文件。
110
+ - 默认参数只写在声明处;调用点省略参数时,注释要写清省略后的行为,避免「不传 = 全部」成为隐藏约定。
111
+
112
+ ## 撤销与回退
113
+
114
+ 撤销只回退受影响的那一处,不要清空整个容器再重放全部历史。重建的写法时间与空间都是 O(n),还把简单问题复杂化;能定位到上一处改动的结构,应当直接改那一个元素。
115
+
116
+ 反例(撤回一手却清空整个棋盘,再按历史逐手重放):
117
+
118
+ ```cpp
119
+ board_ = Board{};
120
+ for (const Move& move : history_) {
121
+ board_.Place(move.position, PlayerToSquareValue(move.player));
122
+ }
123
+ ```
124
+
125
+ 正例(清空那一格,再把轮次还给落子的一方):
126
+
127
+ ```cpp
128
+ Move last = history_.back();
129
+ history_.pop_back();
130
+ board_.Clear(last.position);
131
+ current_player_ = last.player;
132
+ ```
133
+
134
+ 棋盘有自己的单格清空接口时直接用它,不要用「清空整个棋盘」代替。
135
+
136
+ 回溯搜索(minimax、走迷宫、试走一步再退回)同样复用「落子 + 撤回」:在同一个对象上推进、递归、撤回,不为每个分支复制整局。复制把 O(1) 的撤回变成 O(n) 的拷贝,搜索树一深就被放大。
137
+
98
138
  ## 检查清单
99
139
 
100
140
  - [ ] 领域值有明确类型,封闭集合用 `enum class`。
101
141
  - [ ] 规则核心不依赖终端、GUI、Web、Python 或当前环境。
102
- - [ ] 失败是正常结果时用返回值,函数无法完成任务时抛异常;异常类型和捕获方式符合 `source/08-exceptions/01-throw-rules.md`。
142
+ - [ ] 失败是正常结果时用返回值,函数无法完成任务时抛异常;异常类型和捕获方式符合 `references/exceptions/throw-rules.md`。
103
143
  - [ ] 边界按变化原因划分,依赖方向从外向内。
104
144
  - [ ] 共享抽象有第二个真实使用方和明确理由。
105
145
  - [ ] 规则歧义已问清或记录了保守假设。
146
+ - [ ] 函数只做一件事;同一动作用默认参数合并,语义不同的动作用不同名字;没有 `bool` 开关参数。
147
+ - [ ] 回退只改受影响的一处,不清空容器重放全部历史。
148
+ - [ ] 回溯搜索复用落子与撤回,不在每个分支复制整局。
149
+
150
+ ## 依据
151
+
152
+ - [C++ Core Guidelines: Interfaces](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#s-interfaces):把接口与实现分开,规则与不变量写在接口上,依赖方向从外向内。
153
+ - [C++ Core Guidelines: Philosophy](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#s-philosophy):用类型表达意图,避免裸值穿过边界。
154
+ - [Google C++ Style Guide: Classes](https://google.github.io/styleguide/cppguide.html#Classes):让类保持小而职责清楚,接口只暴露调用方需要的行为。
155
+ - [Google C++ Style Guide: Function Overloading](https://google.github.io/styleguide/cppguide.html#Function_Overloading):重载只用于语义相同的变体,调用点必须能直接看出在调用什么。
156
+ - [C++ Core Guidelines: F.51](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#rf-default-args):有选择时优先默认参数,避免为同一语义写多份重载。
157
+ - [cppreference: Default arguments](https://en.cppreference.com/w/cpp/language/default_arguments):默认参数在调用点生效,后续声明不能重复默认值。
@@ -32,6 +32,8 @@
32
32
 
33
33
  ## 四个域
34
34
 
35
+ 四个域也叫四类改动;两种说法指同一组内容、结构、版式、措辞。域是操作内部的层次,不是第四种操作。
36
+
35
37
  | 域 | 回答的问题 | 可以改 | 不能改 |
36
38
  | --- | --- | --- | --- |
37
39
  | 内容 | 这次要保证什么新事实或新行为? | 行为、契约、后置条件、成员事实 | 已有事实、否定、条件、数字、引文 |
@@ -39,7 +41,7 @@
39
41
  | 版式 | 它长什么样? | 缩进、行宽、空行、注释符号、标签格式 | 标识符、字符串、命令、语义 |
40
42
  | 措辞 | 这句话是否准确、必要、可读? | 命名、用词、句式、复述、术语统一 | 结构、事实、段落顺序、信息量 |
41
43
 
42
- 四域按「内容 → 结构 → 版式 → 措辞」推进。前一层没落定,不进入下一层;后一层发现前一层没做完,退回那一层,不在当前层补。`source/01-editing/02-rule-priority.md` 的五级次序是冲突时的判据:行为与契约 > 结构 > 清晰 > 一致 > 形式。
44
+ 四域按「内容 → 结构 → 版式 → 措辞」推进。前一层没落定,不进入下一层;后一层发现前一层没做完,退回那一层,不在当前层补。`references/editing/02-rule-priority.md` 的五级次序是冲突时的判据:行为与契约 > 结构 > 清晰 > 一致 > 形式。
43
45
 
44
46
  ## 跨技能交接
45
47
 
@@ -69,5 +71,5 @@
69
71
  3. 事实有没有变:有没有新增数字、命令、结论、引文,或把「可能」「通常」改成确定?
70
72
  4. 边界写正了吗:有没有「不检查…」清单?前置条件是否写成「调用方保证…」「仅接受…」或 `@pre`?
71
73
  5. 原样保留了吗:术语、命令、标识符、路径、URL 与围栏内容是否未被改写?
72
- 6. 次序守住了吗:前一层是否已落定并经检查,后一层才开始?冲突是否按 `source/01-editing/02-rule-priority.md` 判?
74
+ 6. 次序守住了吗:前一层是否已落定并经检查,后一层才开始?冲突是否按 `references/editing/02-rule-priority.md` 判?
73
75
 
@@ -2,13 +2,13 @@
2
2
 
3
3
  源码规则不是同一层的检查表。两条规则冲突时,先保住更高层,再谈低层的「更短」「更统一」「更整齐」。
4
4
 
5
- 本表管写作内部的取舍顺序;三种操作与四个域的先后见 `source/01-editing/01-edit-operations.md`。
5
+ 本表管写作内部的取舍顺序;三种操作与四个域的先后见 `references/editing/01-edit-operations.md`。
6
6
 
7
7
  | 级别 | 保住什么 | 典型判断 | 冲突时让位给谁 |
8
8
  | --- | --- | --- | --- |
9
9
  | 1 行为与契约 | 可观察行为、不变量、所有权、异常保证不走样 | 这个后置条件删掉后,调用方还知道构造出来的对象是什么状态吗? | 不让位。为短、为顺、为统一删掉契约,都是失败 |
10
10
  | 2 结构 | 库与程序的边界、依赖方向、头文件自包含 | 这个 include 把哪一层带进了公共头? | 让位给行为;高于清晰、一致、形式 |
11
- | 3 清晰 | 名字、签名、注释能直接读出用途与限制 | 读者要不要猜 `cells_` 的两层下标哪层是行? | 让位给行为与结构;高于一致、形式 |
11
+ | 3 清晰 | 名字、签名、注释能直接读出用途与限制 | 读者要不要猜 `squares_` 的两层下标哪层是行? | 让位给行为与结构;高于一致、形式 |
12
12
  | 4 一致 | 同类声明用同一种句式、位置和术语 | 两个成员的注释是否都在说同一类事实? | 让位给行为、结构、清晰;高于形式 |
13
13
  | 5 形式 | 行宽、缩进、空行、符号 | 这行能不能再短一点? | 前四级都通过后才动 |
14
14
 
@@ -25,13 +25,13 @@ void Place(Position pos, Player player);
25
25
  **差**:用 `assert` 承担 Release 也必须执行的 invariant 检查;`NDEBUG` 后检查消失。
26
26
 
27
27
  ```cpp
28
- assert(game.GetBoard().GetState() == BoardState::kInProgress);
28
+ assert(game.GetBoard().GetStatus() == BoardStatus::kInProgress);
29
29
  ```
30
30
 
31
31
  **好**:两种模式都必须检查的 invariant 用 `Check`(项目保留时),失败时打印诊断并终止;只在开发期值得做、Release 可以省掉的自检用 `DebugCheck`。没有这两个宏时分别用 `assert` 与 `std::terminate`。
32
32
 
33
33
  ```cpp
34
- Check(game.GetBoard().GetState() == BoardState::kInProgress, "Ai::Choose: game is already over");
34
+ Check(game.GetBoard().GetStatus() == BoardStatus::kInProgress, "Ai::Choose: game is already over");
35
35
  DebugCheck(index < history_.size(), "history index out of range");
36
36
  ```
37
37
 
@@ -10,8 +10,7 @@ throw std::out_of_range{std::format("GetTask: 下标 {} 越界", index)};
10
10
  **好**:语义已经由标准类型给出时直接用标准类型;消息按「标签: 完整句」组织,句子主语是失败的值,实际值直接插进句子,限度用 `for` 接在后面;语言由项目约定,入口需要面向用户的前缀时只加前缀。
11
11
 
12
12
  ```cpp
13
- throw std::out_of_range{std::format(
14
- "GetTask: index {} is out of range", index)};
13
+ throw std::out_of_range{std::format("GetTask: index {} is out of range", index)};
15
14
  ```
16
15
 
17
- 消息写操作和关键值;英文消息小写开头、句末不加句号,中文消息按中文标点,句式不变;实际值用 `{}` 直接放进句子,不要写成 `(index={})`。`std::format` 只是构造消息的方式,句式见 `source/08-exceptions/01-throw-rules.md` 的「消息」节。项目若定义 `Error` 基类,按该文件先定基类再派生,调用方捕获基类接住整族。
16
+ 消息写操作和关键值;英文消息小写开头、句末不加句号,中文消息按中文标点,句式不变;实际值用 `{}` 直接放进句子,不要写成 `(index={})`。`std::format` 只是构造消息的方式,句式见 `references/exceptions/throw-rules.md` 的「消息」节。项目若定义 `Error` 基类,按该文件先定基类再派生,调用方捕获基类接住整族。
@@ -6,8 +6,10 @@ Google C++ Style Guide 明确写「We do not use C++ exceptions」。本工作
6
6
 
7
7
  规范来源:
8
8
 
9
- - [C++ Core Guidelines: Error handling](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#S-errors)(E.1–E.31)。
9
+ - [C++ Core Guidelines: Error handling](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#s-errors)(E.1–E.31)。
10
10
  - [Boost.Exception](https://www.boost.org/doc/libs/latest/libs/exception/doc/boost-exception.html):异常类型层级的工程典范,只借它的结构,不把 Boost 变成依赖。
11
+ - [ISO C++ `[except]`](https://eel.is/c++draft/except):异常对象、栈展开与构造/析构期间的规则。
12
+ - [cppreference: Exceptions](https://en.cppreference.com/w/cpp/language/exceptions):异常安全保证与 `noexcept` 的语法参考。
11
13
 
12
14
  ## 先定错误策略
13
15
 
@@ -15,7 +17,7 @@ E.1 要求尽早决定错误处理策略:先问调用方还能不能继续、
15
17
 
16
18
  策略一旦定下,声明、实现、测试和入口都用同一套;不要在同一层里一半返回错误码、一半抛异常。
17
19
 
18
- 判断是否算「混用」,看函数合同而不是看返回类型:查询必须产出一个值、失败时产不出(如 `GetState` 越界),就抛;写入操作把「这次不写」当作正常结果、调用方本来就要分支(如 `Board::Place` 返回 `Cell::PlaceResult::kOccupied`,`CheckPlaceable` 返回具体原因),就用返回值。动作可能因为多个前置条件失败、而调用方需要区分原因时,返回 `enum class` 而不是 `bool` 加输出参数。两者并存时,公共头必须把「拒绝」与「抛」的边界各写一行;没有这行注释才算混用。
20
+ 判断是否算「混用」,看函数合同而不是看返回类型:查询必须产出一个值、失败时产不出(如 `GetSquare` 坐标越界),就抛;写入操作把「这次不写」当作正常结果、调用方本来就要分支(如 `Board::Place` 返回 `Square::PlaceResult`,`Square::Place` 返回 `kOccupied`),就用返回值。动作可能因为多个前置条件失败、而调用方需要区分原因时,返回 `enum class` 而不是 `bool` 加输出参数。两者并存时,公共头必须把「拒绝」与「抛」的边界各写一行;没有这行注释才算混用。
19
21
 
20
22
  ## 什么时候抛
21
23
 
@@ -32,27 +34,61 @@ E.2:函数不能完成它被分配的任务时,就抛异常;构造函数
32
34
  3. 下层自己的检查:坐标范围、格子是否为空由 `Board::Place` 判断;不要为了提前抛出而在 `Game` 里重复 `Board::IsInRange`。
33
35
  4. 执行操作并返回结果。
34
36
 
35
- 本层负责的抛出检查必须排在「返回正常失败」之前。否则终局会先返回 `kFinished`,把「不是当前玩家」一起吞掉,调用方拿不到真正的调用错误。属于下层的检查不在此列:对局已结束时 `Game` 不再进入棋盘,越界坐标随 `kFinished` 一起返回,这是正常的短路,不是遗漏。
37
+ 本层负责的抛出检查必须排在「返回正常失败」之前。否则终局会先返回 `kDenied`,把「不是当前玩家」一起吞掉,调用方拿不到真正的调用错误。属于下层的检查不在此列:对局已结束时 `Game` 不再进入棋盘,越界坐标随 `kDenied` 一起返回,这是正常的短路,不是遗漏。
36
38
 
37
39
  参数之间按固定的语义顺序排,不要求与声明顺序一致;关键是同一函数内不来回换,注释、测试和消息都按这个次序。
38
40
 
39
41
  ```cpp
40
- Game::MoveResult Game::Place(Move move) {
41
- if (move.player != current_player_) {
42
+ Game::PlaceResult Game::Place(Move move) {
43
+ if (current_player_ != move.player) {
42
44
  throw std::logic_error{std::format(
43
- "Game::Place: move player is not the current player (move={}, current={})",
44
- move, current_player_)};
45
+ "Game::Place: current player {} cannot accept move {}", current_player_, move)};
45
46
  }
46
- if (board_.GetState() != BoardState::kInProgress) {
47
- return MoveResult::kFinished;
47
+ if (board_.GetStatus() != BoardStatus::kInProgress) {
48
+ return PlaceResult::kDenied;
48
49
  }
49
- // 坐标范围与占用由 Board::Place 检查。
50
- const Cell::PlaceResult result = board_.Place(move.position, PlayerToCellState(move.player));
51
- // 按 result 写历史、返回占用或抛出棋盘拒绝。
50
+ // 坐标范围由 Board 抛出;占用与空值由 Board::Place 返回。
51
+ const Square::PlaceResult result =
52
+ board_.Place(move.position, PlayerToSquareValue(move.player));
53
+ if (result == Square::PlaceResult::kOccupied) {
54
+ return PlaceResult::kRejected;
55
+ }
56
+ if (result == Square::PlaceResult::kIgnored) {
57
+ throw std::logic_error{"Game::Place: board rejected an empty value"};
58
+ }
59
+ // 记录这一步并轮到对方。
60
+ }
61
+ ```
62
+
63
+ ### 重复的入口检查
64
+
65
+ 同一层有多个入口重复同一个 `if (!IsInRange(...)) throw ...` 时,把检查收进私有函数。函数名由调用方传入,`std::source_location` 只在默认参数里取调用点:
66
+
67
+ ```cpp
68
+ void Board::CheckInRange(Position pos, std::string_view function,
69
+ std::source_location location) {
70
+ if (IsInRange(pos)) {
71
+ return;
72
+ }
73
+
74
+ // 不同编译器给出的路径形态不同,只保留文件名,保证消息一致。
75
+ std::string_view file = location.file_name();
76
+ if (const std::size_t separator = file.find_last_of("/\\");
77
+ separator != std::string_view::npos) {
78
+ file.remove_prefix(separator + 1);
79
+ }
80
+
81
+ throw std::out_of_range{std::format(
82
+ "{}: position {} is out of range for board {}x{} ({}:{})", function, pos, kRowCount,
83
+ kColumnCount, file, location.line())};
52
84
  }
53
85
  ```
54
86
 
55
- 依据:[Core Guidelines I.5](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Ri-pre)、[I.6](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Ri-expects) 把前置条件写进接口;标准库的 `vector::at` 与 `std::stoi` 在访问前检查边界,.NET 的 [CA1062](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1062) 也要求先验证再使用。[P2053R0](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p2053r0.pdf) 区分输入校验与防御性检查:外部输入必须校验,内部前置条件可用断言或 contract 表达。这里只把 `Game` 自己拥有的状态前置条件写成异常;`Player` 的取值由适配层保证,坐标范围由 `Board` 拥有,重复检查会让同一规则出现两份,消息与异常类型还可能分叉。参数非法时返回一个业务结果,会让调用方以为「只是不能下这里」,而不是「这手棋本身不成立」。
87
+ 声明写成 `static void CheckInRange(Position pos, std::string_view function, std::source_location location = std::source_location::current());`。`source_location::current()` 作为默认参数在调用点求值,所以不把 `source_location` 加进公共接口;标签仍由调用点显式传入,如 `CheckInRange(pos, "Board::Place")`。
88
+
89
+ `source_location` 是 C++20 设施,GCC、Clang 与 MSVC 都提供;差异在 `file_name()` 与 `function_name()` 的内容。`function_name()` 可能给出完整签名或限定名,所以消息标签仍显式传入;`file_name()` 可能是相对或绝对路径,先取 basename。Clang 16 起 `line()` 才稳定指向调用点,MSVC 18.4 起才修复默认参数里直接调用 `.function_name()` 返回空串的问题;传整个 `source_location` 对象可避开后者。这样三个平台都输出 `board.cpp:行号`,测试只断言文件名与函数名,不断言具体行号。
90
+
91
+ 依据:[Core Guidelines I.5](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#ri-pre)、[I.6](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#ri-expects) 把前置条件写进接口;标准库的 `vector::at` 与 `std::stoi` 在访问前检查边界,.NET 的 [CA1062](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1062) 也要求先验证再使用。[P2053R0](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p2053r0.pdf) 区分输入校验与防御性检查:外部输入必须校验,内部前置条件可用断言或 contract 表达。这里只把 `Game` 自己拥有的状态前置条件写成异常;`Player` 的取值由适配层保证,坐标范围由 `Board` 拥有,重复检查会让同一规则出现两份,消息与异常类型还可能分叉。参数非法时返回一个业务结果,会让调用方以为「只是不能下这里」,而不是「这手棋本身不成立」。
56
92
 
57
93
  ## 异常类型
58
94
 
@@ -89,7 +125,7 @@ E.31:`catch` 子句从派生到基类、从具体到通用排序,`catch (...
89
125
 
90
126
  ## 不抛的边界
91
127
 
92
- E.12:在不可能或不可接受抛异常的地方用 `noexcept`。声明前先读实现:函数体里出现 `at()` 或其他可能抛出的调用,就去掉 `noexcept`;不要再包一层只转发下标的私有函数来维持 `noexcept`,直接写 `cells_.at(row).at(col)` 并去掉 `noexcept`。正例是只比较两个整数的 `IsInRange`;反例是用 `at()` 读取并会抛 `std::out_of_range` 的 `GetState`。普通查询和返回结果枚举的函数,不因为当前函数体不抛就补 `noexcept`;`noexcept` 留给移动、交换、析构,以及已经标上的纯谓词。E.13:直接拥有资源的函数不要抛,资源释放应交给 RAII。E.16:析构、`deallocate`、`swap` 以及异常类型的拷贝/移动构造不得失败;析构和 `swap` 应当是 `noexcept`。
128
+ E.12:在不可能或不可接受抛异常的地方用 `noexcept`。声明前先读实现:函数体里出现 `at()` 或其他可能抛出的调用,就去掉 `noexcept`;不要再包一层只转发下标的私有函数来维持 `noexcept`,直接写 `squares_.at(row).at(col)` 并去掉 `noexcept`。正例是只比较两个整数的 `IsInRange`;反例是用 `at()` 读取并会抛 `std::out_of_range` 的 `GetSquare`。普通查询和返回结果枚举的函数,不因为当前函数体不抛就补 `noexcept`;`noexcept` 留给移动、交换、析构,以及已经标上的纯谓词。E.13:直接拥有资源的函数不要抛,资源释放应交给 RAII。E.16:析构、`deallocate`、`swap` 以及异常类型的拷贝/移动构造不得失败;析构和 `swap` 应当是 `noexcept`。
93
129
 
94
130
  E.30:不要写异常规范(`throw(...)` 之类);C++11 之后用 `noexcept` 表达「不抛」。需要无异常构建时,由项目单独决定,不要把这套异常指导改写成两面都对的模糊话。
95
131
 
@@ -112,7 +148,7 @@ int main() {
112
148
 
113
149
  ## 消息
114
150
 
115
- `what()` 是给程序员看的诊断文本,句式是「标签: 完整句」。标签写明来源,句子主语是失败的值或对象,谓语说明哪里不对,实际值直接插进句子,限度用 `for` 接在句末。标签是标识符,成员写 `Type::Function`,自由函数只写函数名,冒号后一个空格;默认参数用 `std::source_location::current()` 在调用点求值时,标签写 `function_name()`。先写哪个值失败,再写失败条件,最后写限度;语言由项目约定,下面的示例用英文。句子要完整:主语和谓语都在,不写 `board position is out of range` 这种重复标签类型的主语,也不把值塞进括号,不写 Error / Failed 或空泛的「失败」。实际值用 `{}` 直接放进句子,不再写 `(name=value)`;没有实际值就不写值,没有限度就不写 `for`。需要原因时用 `because`,不要用分号堆叠。
151
+ `what()` 是给程序员看的诊断文本,句式是「标签: 完整句」。标签写明来源,句子主语是失败的值或对象,谓语说明哪里不对,实际值直接插进句子,限度用 `for` 接在句末。标签是标识符,成员写 `Type::Function`,自由函数只写函数名,冒号后一个空格;默认参数用 `std::source_location::current()` 在调用点求值时,标签写调用方显式传入的 `Type::Function`,不解析 `function_name()` 的完整签名。先写哪个值失败,再写失败条件,最后写限度;语言由项目约定,下面的示例用英文。句子要完整:主语和谓语都在,不写 `board position is out of range` 这种重复标签类型的主语,也不把值塞进括号,不写 Error / Failed 或空泛的「失败」。实际值用 `{}` 直接放进句子,不再写 `(name=value)`;没有实际值就不写值,没有限度就不写 `for`。需要原因时用 `because`,不要用分号堆叠。
116
152
 
117
153
  英文消息只用 `:`、`()`、`,`、`=` 四个 ASCII 符号:冒号紧贴标签、后接一个空格;括号只放次要值或补充说明;逗号前不空、后一个空格;等号两侧不空。不用第二个冒号、句号、问号、感叹号、破折号或 `×`;句子小写开头,句末不加句号;尺寸写 `3x3`,`out of range` 不加连字符。中文消息按中文标点,但句式与「值在前、限度在后」的顺序不变。
118
154
 
@@ -122,17 +158,33 @@ int main() {
122
158
 
123
159
  `what()` 不是分支接口;调用方要数字就放进类型成员,不解析字符串。入口可以在消息前加面向用户的前缀,库代码不打印日志。
124
160
 
161
+ 写进 `throw` 时,直接把 `std::format(...)` 放在异常类型后面,不先建消息变量:
162
+
163
+ ```cpp
164
+ throw std::out_of_range{std::format(
165
+ "Board::GetSquare: position {} is out of range for board {}x{}", pos, kRowCount,
166
+ kColumnCount)};
167
+ ```
168
+
169
+ 跨行时只把格式串与参数续到下一行:`throw`、异常类型与 `std::format(` 留在同一行,读者先看到抛什么,再看到消息。消息没有插值时写一行 `throw std::logic_error{"ChooseSquare: game is already over"};`。
170
+
171
+ 常用库把消息的构造放进异常类型或辅助函数:spdlog 的 `spdlog_ex` 在构造函数里写 `_msg`,CLI11 的 `Error` 族在静态工厂里拼好句子,POCO 的 `Exception` 在构造函数里存消息;调用处因此只有一行 `throw ...(...)`。本工作流不额外引入消息变量,也不为消息的 `const` 与 `std::move` 单独做决定。
172
+
173
+ Rust 把失败分成两类:`Result` 表示可预期的失败,用 `?` 传播;`panic!` 只表示程序里的 bug(invariant 已被破坏),消息同样在调用点构造,长消息由格式化器折行。C++ 的异常同时承担这两种职责,本工作流因此把「调用方无法继续」写成异常,把「可恢复的拒绝」写成返回值。
174
+
125
175
  ```cpp
126
176
  throw std::out_of_range{std::format(
127
- "Board::GetCell: position {} is out of range for board {}x{}",
128
- pos, kRowCount, kColumnCount)};
177
+ "Board::GetSquare: position {} is out of range for board {}x{}", pos, kRowCount,
178
+ kColumnCount)};
179
+
129
180
  throw std::logic_error{std::format(
130
181
  "Game::Place: board rejected move {} because the cell state is empty", move)};
131
- throw std::logic_error{"ChooseCell: game is already over"};
132
- throw std::logic_error{"EmptyCellsOrThrow: there is no empty cell left to place"};
182
+
183
+ throw std::logic_error{"ChooseSquare: game is already over"};
184
+ throw std::logic_error{"ChooseSquare: there is no empty square left to place"};
133
185
  ```
134
186
 
135
- 反例:`Board::GetCell: board position is out of range (position={}); board is {}x{}`。标签已经说明是 `Board::GetCell`,句子又用 `board position` 当主语,实际值还放在括号里;读者要在标签、主语和括号之间来回拼。
187
+ 反例:`Board::GetSquare: board position is out of range (position={}); board is {}x{}`。标签已经说明是 `Board::GetSquare`,句子又用 `board position` 当主语,实际值还放在括号里;读者要在标签、主语和括号之间来回拼。
136
188
 
137
189
  ## 声明注释
138
190
 
@@ -150,10 +202,11 @@ Config ReadConfig(const std::filesystem::path& path);
150
202
  - [ ] 错误策略在接口设计时已确定,同一层没有混用错误码和异常。
151
203
  - [ ] 构造函数失败时抛异常,没有留下半初始化对象。
152
204
  - [ ] 异常类型有语义,没有抛内建类型、字符串或裸指针。
205
+ - [ ] `throw` 直接接收 `std::format(...)`;跨行时只把格式串与参数续行,不为消息单独建变量。
153
206
  - [ ] 按值抛出、按 `const` 引用捕获;重抛用 `throw;`。
154
207
  - [ ] 析构、`swap`、异常类型的拷贝/移动不抛。
155
208
  - [ ] 没有用异常做正常分支,也没有在每个函数里捕获所有异常。
156
- - [ ] 本层负责的参数校验与状态检查排在正常结果返回之前;非法参数没有被 `kFinished` 之类的业务结果掩盖。
209
+ - [ ] 本层负责的参数校验与状态检查排在正常结果返回之前;非法参数没有被 `kDenied` 之类的业务结果掩盖。
157
210
  - [ ] 入口有统一捕获,消息可定位,退出码符合项目合同。
158
211
  - [ ] 公共声明的 `@throws` 与实际抛出类型一致,且 Debug/Release 的抛出条件相同。
159
212
  - [ ] 每个 `noexcept` 函数的函数体不调用 `at()` 或其他会抛的接口。
@@ -1,6 +1,6 @@
1
1
  # 术语
2
2
 
3
- 代码标识符用英文;注释和说明用中文时,同一个概念只用一种译法。下表是本项目的默认译法,项目技能可以增补,但不要改已有词的含义。标识符怎么拼(大小写、前缀、语义角色)见 `source/06-naming/01-identifier-names.md`;本表只管术语译法与近义词辨析。
3
+ 代码标识符用英文;注释和说明用中文时,同一个概念只用一种译法。下表是本项目的默认译法,项目技能可以增补,但不要改已有词的含义。标识符怎么拼(大小写、前缀、语义角色)见 `references/naming/identifier-names.md`;本表只管术语译法与近义词辨析。
4
4
 
5
5
  | 英文 | 中文 | 说明 |
6
6
  | --- | --- | --- |
@@ -84,5 +84,5 @@
84
84
  - **声明 vs 定义**:头文件里多是声明;函数体、全局变量初始化是定义。
85
85
  - **参数 vs 实参**:声明里叫参数(parameter),调用点叫实参(argument);中文口语都叫参数时,写文档要区分。
86
86
  - **接口 vs 实现**:公共头是接口,`.cpp` 是实现;不要用「接口」指整个类。
87
- - **异常 vs 错误**:异常是语言机制,错误是结果;是否用异常表达错误由接口合同和 invariant 决定,不能把两者当同义词。规则见 `source/08-exceptions/01-throw-rules.md`。
87
+ - **异常 vs 错误**:异常是语言机制,错误是结果;是否用异常表达错误由接口合同和 invariant 决定,不能把两者当同义词。规则见 `references/exceptions/throw-rules.md`。
88
88
  - **格式化 vs 排版**:`std::format` 组合消息;clang-format 排版源码。不要混用「格式化」指代两者。
@@ -4,6 +4,12 @@
4
4
 
5
5
  - 类型决定允许的操作和表示;对象是某类型的一块存储;值是可以复制、比较、传递的内容。
6
6
  - 变量有名字、类型、存储期和作用域。局部变量在离开作用域时销毁;`static` 局部变量在首次执行到声明时初始化,程序结束时销毁。
7
- - 声明让名字可见,定义分配存储或给出函数体。一个实体可以声明多次,但只能定义一次;头文件只放声明,定义放 `.cpp`。
7
+ - 声明让名字可见,定义分配存储或给出函数体。非 `inline` 的变量与函数一个程序只能定义一次;`inline` 函数/变量与模板可以出现在多个翻译单元,但定义必须一致。头文件只放声明,定义放 `.cpp`,例外是 `inline` 与模板。
8
8
  - 引用是已有对象的别名,必须初始化,不能重新绑定;指针可以为空,也可以改指向。接口优先用引用表达“必须存在”,用指针表达“可以不存在”。
9
9
  - `*this` 不用于复制当前对象:要一份副本就直说语义(如“推演落子”只读原数据,或显式写一个复制函数)。`return *this` 仍只属于赋值运算符,井字棋没有这种运算符。
10
+
11
+ ## 依据
12
+
13
+ - [ISO C++ `[basic.def.odr]`](https://eel.is/c++draft/basic.def.odr):一个实体允许的声明与定义次数,以及 ODR 的一致要求。
14
+ - [cppreference: Definitions and ODR](https://en.cppreference.com/w/cpp/language/definition):声明、定义、`inline` 与模板的多翻译单元规则。
15
+ - [C++ Core Guidelines: P.1](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#s-philosophy):用类型与对象表达意图,不靠注释补语义。
@@ -6,14 +6,21 @@
6
6
  int count{}; // 值初始化,0
7
7
  std::vector<int> values{1, 2, 3}; // 列表初始化
8
8
  std::string name = "demo"; // 拷贝初始化,类型明确
9
- auto total = 0.0; // auto 只能用 =,不能用 {}
9
+ auto total = 0.0; // 本工作流统一用 = 推导
10
10
  ```
11
11
 
12
12
  - `kLines` 这类多行聚合常量表写 `name = {{`;单个对象和空初始化仍写 `count{}`,不写 `=`;不把其他花括号初始化改成 `=`。
13
13
  - `T value{}` 做值初始化:内置类型得 0,类类型调用默认构造函数。
14
+ - `auto x{1}` 在 C++17 起合法,但 `{}` 与 `initializer_list` 的重载容易误读;本工作流统一写 `auto x = 1`。
14
15
  - 花括号禁止窄化转换:`int x{3.5}` 编译失败,`int x = 3.5` 静默截断。需要截断时显式 `static_cast<int>(3.5)`。
15
16
  - `std::vector<int> values(3)` 是三个 0;`values{3}` 是一个元素 3。两种括号语义不同,不要凭习惯混用。
16
17
  - 类成员在声明处写默认成员初始化(NSDMI),一律用花括号:标量 `count_{0}`,数组 `items_{}`,容器与类类型成员写空 `{}`(如 `std::vector<int> values_{};`、`Board board_{};`)。
17
18
  - `=` 只用于花括号不可用或会改变含义的例外:引用成员不写 NSDMI,在构造函数初始化列表绑定;容器需要 `std::vector<int>(n)` 的容量构造时写 `= std::vector<int>(n)`,或在构造函数初始化列表写 `v_(n)`;`std::initializer_list` 成员避免使用,确实需要时写 `= {..}` 并保证底层数组的生命周期长于对象;花括号无法构造的类型才回退到 `= value` 或构造函数初始化列表。`=` 仍然是初始化,不是赋值。
18
19
  - `static` 与 `static constexpr` 数据成员不是对象成员,继续写 `= 3`;`auto` 不能做非静态数据成员。
19
20
  - 构造函数初始化列表按成员声明顺序执行,不按书写顺序;初始化列表顺序与声明顺序保持一致,避免依赖未初始化成员。
21
+
22
+ ## 依据
23
+
24
+ - [ISO C++ `[dcl.init]`](https://eel.is/c++draft/dcl.init):值初始化、列表初始化、拷贝初始化与窄化转换。
25
+ - [cppreference: Initialization](https://en.cppreference.com/w/cpp/language/initialization):各种初始化语法与重载选择。
26
+ - [C++ Core Guidelines: ES.23](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#res-list):优先 `{}` 初始化;本工作流按此执行,并只为拷贝/`auto` 保留 `=`。
@@ -4,3 +4,9 @@
4
4
  - 公共接口、类成员、函数返回类型写显式类型;返回值需要隐藏实现细节时才可以写 `auto`,且头文件里要能看出契约。
5
5
  - `auto` 会丢掉顶层 `const` 和引用:`auto value = const_ref` 得到副本;需要引用时写 `const auto&`。
6
6
  - 结构化绑定 `auto [key, value] : map` 中,`key` 是副本;要避免复制时写 `const auto& [key, value]`。
7
+
8
+ ## 依据
9
+
10
+ - [ISO C++ `[dcl.spec.auto]`](https://eel.is/c++draft/dcl.spec.auto):`auto` 的推导规则与占位符约束。
11
+ - [cppreference: `auto`](https://en.cppreference.com/w/cpp/language/auto):顶层 `const`/引用丢失、`decltype(auto)` 与结构化绑定。
12
+ - [Google C++ Style Guide: `auto`](https://google.github.io/styleguide/cppguide.html#auto):只在类型显然的局部变量使用,公共接口写显式类型。
@@ -0,0 +1,25 @@
1
+ # `const`、`constexpr`、`consteval`、`constinit`
2
+
3
+ | 关键字 | 承诺 | 典型位置 |
4
+ | --- | --- | --- |
5
+ | `const` | 初始化后不能通过这个名字修改 | 局部变量、参数、成员函数尾部 |
6
+ | `constexpr` | 能在编译期求值(满足条件时) | 常量、函数、构造函数 |
7
+ | `consteval` | 必须在编译期求值 | 编译期校验函数 |
8
+ | `constinit` | 静态存储期变量在编译期完成初始化 | 全局/静态变量 |
9
+ | `inline constexpr` | 头文件里可共享的编译期常量,避免 ODR 问题 | 命名空间作用域常量 |
10
+
11
+ - 共享常量写在头文件的命名空间作用域:`inline constexpr int kMaxRows = 3;`,不要用 `#define`。`.cpp` 里非 `inline`、非 `extern` 的命名空间作用域 `constexpr` 已有内部链接。
12
+ - `constexpr` 函数的定义要放在头文件里,否则其他翻译单元无法在编译期调用。
13
+ - 不要给所有函数加 `constexpr`;只有确实需要在编译期使用、且实现满足约束时才加。
14
+ - `constinit` 只保证初始化时机,不保证不可修改;需要不可修改时同时写 `const`。
15
+ - C++20 起 `constexpr` 函数可以包含循环、局部变量和部分动态分配,但不要借此把运行期逻辑塞进编译期。
16
+ - 局部变量默认不加 `const`:短生命周期的局部值只读一次,`const` 不改变存储与运行行为。把 `const` 留给接口契约——`const` 引用参数、`const` 成员函数、指向只读数据的指针与视图,以及确实要防止后续误写的变量。Core Guidelines 的 Con.1 主张默认不可变;本仓取 Google 风格的做法,只在接口层要求 `const`。
17
+
18
+ ## 依据
19
+
20
+ - [ISO C++ `[dcl.constexpr]`](https://eel.is/c++draft/dcl.constexpr):`constexpr`、`consteval` 的求值时机与函数约束。
21
+ - [cppreference: `constexpr`](https://en.cppreference.com/w/cpp/language/constexpr):常量表达式、`constinit` 与 `consteval` 的对照。
22
+ - [cppreference: `constinit`](https://en.cppreference.com/w/cpp/language/constinit):静态存储期初始化时机,不改变可修改性。
23
+ - [Google C++ Style Guide: Use of const](https://google.github.io/styleguide/cppguide.html#Use_of_const):「We strongly recommend using `const` in APIs (i.e., on function parameters, methods, and non-local variables) wherever it is meaningful and accurate.」局部变量不在要求之内。
24
+ - [Chromium C++ style](https://chromium.googlesource.com/chromium/src/+/main/styleguide/c++/c++.md) 与 [LLVM Coding Standards](https://llvm.org/docs/CodingStandards.html):同样没有要求局部变量加 `const`;LLVM 只对静态与全局常量用 `constexpr`。
25
+ - [C++ Core Guidelines: Con.1](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#rconst-immutable):主张默认不可变;本仓只在接口处采用这一主张,局部变量按上一条处理。
@@ -0,0 +1,23 @@
1
+ # 表达式与比较
2
+
3
+ - 比较写成“变量在左、常量在右”:`if (id < kTaskCount)`,不写 Yoda 条件 `if (kTaskCount > id)`;常量与枚举值总在右边。成员与参数比较时,成员状态在左、参数在右,如 `if (current_player_ != move.player)`:判断「轮到谁」以对象自己的状态为起点,参数是被核对的一次输入;成员与常量比较时成员在左,如 `if (value_ != SquareValue::kEmpty)`;参数与常量比较时参数在左。分不清主次时按“读起来的主语”决定,同一段代码保持同一种顺序。
4
+ - 同一个值对多个离散取值分派时用 `switch`,不用一串 `if`:值来自枚举或整数常量、分支 3 个以上、分支体短。枚举覆盖全部取值时不写 `default`,新增取值由 `-Wswitch` 指出;编译器需要出口时在 `switch` 后写 `std::unreachable()`,不写 `throw` 兜底。
5
+ - 条件是比较范围、布尔组合,或只有 1–2 个分支,或「失败提前返回、成功落到末尾」的守卫时用 `if`,末尾直接写成功路径。
6
+ - 不依赖隐式穿透;确实需要穿透时写 `[[fallthrough]]`。
7
+ - 混合优先级的算术表达式加括号:`(a + b) * c`,不依赖读者记忆优先级。`readability-math-missing-parentheses` 会检查。
8
+ - 区间用半开区间 `[low, high)`;判空用 `empty()`,不要用 `size() == 0`。
9
+ - 逻辑表达式按语义分组,一组一个括号;组内的单个比较不再加括号,`&&` 的优先级已经清楚,多加一层反而难核对。
10
+ - 整行在列宽内时写一行;放不下时按组换行,各组对齐。不要把多个否定串成一行。
11
+ - 自增自减单独成句;不要在同一个表达式里对同一对象多次修改,那是未定义行为。
12
+
13
+ ## 依据
14
+
15
+ - [ISO C++ `[expr]`](https://eel.is/c++draft/expr):运算符优先级、求值顺序与未定义行为。
16
+ - [cppreference: Expressions](https://en.cppreference.com/w/cpp/language/expressions):运算符分类、短路与序列点规则。
17
+ - [C++ Core Guidelines: ES.41](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#s-expr):避免容易误读的表达式,必要时加括号。
18
+ - [Style Guide for Writing Comparisons in Conditions](https://www.enyo.de/fw/notes/style-guide-conditions.html):变化最快、最不确定的一方写在比较左边;常量前置已无必要,且常掩盖本意。
19
+ - [clang-tidy `readability-constant-operand-order`](https://github.com/llvm/llvm-project/pull/167158):现代代码库倾向统一写 `x == 0`,不写 `0 == x`。
20
+ - [LLVM Coding Standards](https://llvm.org/docs/CodingStandards.html) 与 [Linux kernel coding style](https://www.kernel.org/doc/html/latest/process/coding-style.html):示例都用自然语序(`I != E`、`system_state == SYSTEM_RUNNING`),不把常量前置。
21
+ - [C++ Core Guidelines: ES.70、ES.78、ES.79](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#s-expr):有选择时优先 `switch`;对常量比较,通常比 `if`-`then`-`else` 链更容易优化,并能提示枚举是否覆盖全;不依赖隐式穿透,`default` 只处理常见情况。
22
+ - [LLVM Coding Standards: Don't use default labels in fully covered switches over enumerations](https://llvm.org/docs/CodingStandards.html#don-t-use-default-labels-in-fully-covered-switches-over-enumerations):全覆盖枚举 `switch` 不写 `default`;需要给编译器出口时在 `switch` 后用 `llvm_unreachable`(对应本项目的 `std::unreachable`)。
23
+ - [Google C++ Style Guide: switch statements](https://google.github.io/styleguide/cppguide.html#switch-statements):非枚举条件的 `switch` 必须有 `default`;枚举由编译器提醒未覆盖值;`[[fallthrough]]` 必须标注。
@@ -1,9 +1,15 @@
1
1
  # 函数、参数与返回值
2
2
 
3
- - 命名规则以 `source/06-naming/01-identifier-names.md` 为准:函数 PascalCase、查询 `Is` / `Has` / `Can` / `Get` / `Find`、转换 `SourceToTarget`。
3
+ - 命名规则以 `references/naming/identifier-names.md` 为准:函数 PascalCase、查询 `Is` / `Has` / `Can` / `Get` / `Find`、转换 `SourceToTarget`。
4
4
  - 输入参数默认按值传小对象、按 `const&` 传大对象;需要修改调用方对象时用引用参数,并在名字或注释里标 `[out]`。
5
5
  - 优先返回值而不是输出参数;需要返回多个值时用 `struct` 聚合,不用多个 `bool` 输出参数。
6
6
  - 返回容器或大对象时按值返回,依赖 RVO / 移动;不要返回局部对象的引用或指针。
7
7
  - 单参数构造函数加 `explicit`,除非确实需要隐式转换;转换运算符也加 `explicit`。
8
8
  - 不在虚函数上写默认参数;默认参数在静态类型处绑定,派生类改默认值会造成意外。
9
9
  - 函数只做一件事;超过一屏时先找可命名的子步骤,分阶段与注释写法见 `steps/04-implementation.md`。
10
+
11
+ ## 依据
12
+
13
+ - [ISO C++ `[dcl.fct]`](https://eel.is/c++draft/dcl.fct):函数声明、参数与返回类型。
14
+ - [cppreference: Functions](https://en.cppreference.com/w/cpp/language/functions):重载、默认参数与返回类型推导。
15
+ - [Google C++ Style Guide: Functions](https://google.github.io/styleguide/cppguide.html#Functions):短小、单一职责,参数与返回值写清契约。
@@ -3,7 +3,13 @@
3
3
  - `struct` 用于公开数据的聚合;`class` 用于维护不变量、隐藏表示。两者只差默认访问权限,按语义选。
4
4
  - 数据成员默认私有;需要暴露只读视图时提供 `const` 成员函数,不返回可变内部引用。
5
5
  - 单参数构造函数用 `explicit`;构造函数失败时抛异常,不留下半初始化对象。
6
- - 特殊成员(构造、析构、拷贝、移动)在头文件声明,在 `.cpp` 定义;只有默认实现足够且零参数时才写 `= default`。
6
+ - 特殊成员(构造、析构、拷贝、移动)在头文件声明,在 `.cpp` 定义;只有默认实现足够时才写 `= default`,默认构造要无参数,拷贝/移动构造按各自的签名。
7
7
  - 定义了析构函数、拷贝构造或拷贝赋值之一时,认真考虑其余特殊成员;用 `= delete` 明确禁止不需要的操作。
8
8
  - 多态基类析构函数必须是 `virtual` 或 `protected`;否则通过基类指针删除派生对象是未定义行为。
9
9
  - 不在构造函数里调用虚函数;此时派生部分还没构造完。
10
+
11
+ ## 依据
12
+
13
+ - [ISO C++ `[class]`](https://eel.is/c++draft/class):类成员、访问控制与特殊成员的声明规则。
14
+ - [cppreference: Classes](https://en.cppreference.com/w/cpp/language/classes):构造/析构、拷贝/移动与 `= default`/`= delete`。
15
+ - [Google C++ Style Guide: Classes](https://google.github.io/styleguide/cppguide.html#Classes):struct 与 class 的分工、显式构造函数与虚析构。
@@ -0,0 +1,15 @@
1
+ # RAII、所有权与移动
2
+
3
+ - 资源获取即初始化:用对象生存期管理内存、文件、锁、套接字;析构函数负责释放。
4
+ - 默认用 `std::unique_ptr<T>` 表达独占所有权;需要共享所有权时才用 `std::shared_ptr<T>`,并用 `std::weak_ptr` 打破循环。
5
+ - 不写裸 `new` / `delete`;需要动态数组时用 `std::vector`,需要单个对象时用 `std::make_unique`。
6
+ - 移动把资源从源对象转走;`std::move` 只是 `static_cast<T&&>`,本身不移动任何东西。标准只保证移动后的对象处于有效但未指定状态,智能指针通常为空,但不要依赖具体值。
7
+ - 移动后的对象只能重新赋值或销毁,不要读取它的值。
8
+ - 移动构造函数和移动赋值标记 `noexcept`,否则 `std::vector` 等容器在扩容时可能选择拷贝。
9
+ - 返回值直接 `return value;`,不要 `return std::move(value);`;后者阻止 RVO。
10
+
11
+ ## 依据
12
+
13
+ - [ISO C++ `[class.copy.ctor]`](https://eel.is/c++draft/class.copy.ctor):移动构造与移动赋值的语义、`noexcept` 与隐式生成。
14
+ - [cppreference: RAII](https://en.cppreference.com/w/cpp/language/raii):资源生存期与异常安全。
15
+ - [C++ Core Guidelines: R.1](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#s-resource):用 RAII 管理资源,避免裸 `new`/`delete`。
@@ -18,3 +18,9 @@ T Clamp(T value, T low, T high) {
18
18
  - 概念只表达语法和语义约束,不替代文档;错误消息要能让调用者知道违反了哪条约束。
19
19
  - CTAD(类模板实参推导)用于局部变量和明显场景:`std::pair pair{1, 2.0};`;公共接口写显式模板实参,避免推导规则变化影响契约。
20
20
  - 不用模板做运行期分支;需要运行期多态时用虚函数或 `std::variant`。
21
+
22
+ ## 依据
23
+
24
+ - [ISO C++ `[temp]`](https://eel.is/c++draft/temp):模板定义、实例化与两阶段名字查找。
25
+ - [cppreference: Templates](https://en.cppreference.com/w/cpp/language/templates):模板参数、特化与依赖名。
26
+ - [cppreference: Constraints and concepts](https://en.cppreference.com/w/cpp/language/constraints):概念、`requires` 与约束归约。
@@ -3,7 +3,8 @@
3
3
  - [ ] 初始化方式统一,窄化转换被编译器拦住。
4
4
  - [ ] `auto` 只出现在类型显然的局部变量;公共接口有显式类型。
5
5
  - [ ] 共享常量用 `inline constexpr`,没有宏常量。
6
- - [ ] 比较不是 Yoda 形式,混合优先级有括号。
6
+ - [ ] 局部变量不加 `const`;`const` 用在参数、成员函数与只读视图上。
7
+ - [ ] 比较不是 Yoda 形式;成员与参数比较时成员状态在左、参数在右;混合优先级有括号。
7
8
  - [ ] 资源由 RAII 管理,没有裸 `new` / `delete`。
8
9
  - [ ] 特殊成员声明与定义位置正确,移动操作 `noexcept`。
9
10
  - [ ] 模板约束用概念表达,定义在头文件。
@@ -0,0 +1,22 @@
1
+ # 测量顺序
2
+
3
+ 先证明慢在哪里,再改代码。没有测量的“优化”通常只是把复杂度搬了个位置。
4
+
5
+ 本组条文只管性能判断与测量;语言规则见 `references/language/language-basics/01-objects.md`,容器与算法选择见 `references/language/standard-library/01-containers.md`,静态检查开关见 `engineering` 的 `references/formatting/03-clang-tidy.md`。
6
+
7
+ 1. 写清可观察的性能目标:延迟、吞吐、内存上限,或“输入规模翻倍时耗时如何增长”。
8
+ 2. 用真实规模的输入测量;玩具数据上的常数差异没有意义。
9
+ 3. 先看算法复杂度,再看内存访问,再看常数因子。
10
+ 4. 用 profiler 找热点;不要凭代码长短猜。
11
+ 5. 改一处,复测同一指标;记录测量条件和结果。
12
+
13
+ - 发布构建(`-O2` / `-O3`)测性能,调试构建测正确性。
14
+ - 微基准要防止编译器把结果优化掉;使用输入、累加结果并输出。
15
+ - 单次测量受缓存、频率、调度影响;取多次的中位数或分位数,不只报最好的一次。
16
+ - 采样 profiler(Linux `perf record`、macOS Instruments、Windows ETW)给出热点分布;调用次数与单次耗时一起看,避免被高频小函数带偏。
17
+
18
+ ## 依据
19
+
20
+ - [ISO C++ `[intro.abstract]`](https://eel.is/c++draft/intro.abstract):as-if 规则允许编译器重排,性能结论只能来自测量。
21
+ - [C++ Core Guidelines: Per.1](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#s-performance):没有理由不要优化;先测量再改。
22
+ - [C++ Core Guidelines: Per.6](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#s-performance):没有测量就没有性能结论。
@@ -5,3 +5,9 @@
5
5
  - 频繁按 key 查找时,`unordered_map` 通常比 `map` 快,但迭代顺序不稳定;需要有序输出时不要选它。
6
6
  - 小规模数据(几十个元素)上,连续 `vector` 的线性扫描常比哈希表更快;不要机械套用复杂度。
7
7
  - 递归深度可能随输入增长时,改成迭代或显式栈;栈溢出不是可捕获的异常。
8
+
9
+ ## 依据
10
+
11
+ - [ISO C++ `[container.requirements]`](https://eel.is/c++draft/container.requirements):各容器的复杂度保证。
12
+ - [cppreference: Containers library](https://en.cppreference.com/w/cpp/container):查找、插入、遍历的复杂度对照。
13
+ - [C++ Core Guidelines: Per.4](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#s-performance):理解算法与数据结构的成本,再谈常数因子。
@@ -3,6 +3,12 @@
3
3
  - 连续存储对缓存友好;`vector<struct>` 比 `vector<unique_ptr<struct>>` 更容易预取。
4
4
  - 只遍历部分字段时,AoS(数组的结构)可能浪费带宽;数据量大且按列访问时考虑 SoA(结构的数组),但先用 profiler 证明。
5
5
  - 小对象按值传递通常比堆分配快;不要为了“避免拷贝”给所有参数加指针。
6
- - `reserve` 减少 `vector` 扩容和搬移;只有已知上界或能准确估算时才用。
6
+ - `reserve` 减少 `vector` 扩容和搬移;只有已知上界或能准确估算时才用。`reserve` 只改容量、不改 size;`shrink_to_fit` 不保证真正释放。
7
7
  - 移动大对象比拷贝便宜,但移动不是免费;不要把 `std::move` 当成优化开关。
8
8
  - 返回局部对象直接 `return value;`,依赖 RVO / NRVO;`return std::move(value);` 会阻止优化。
9
+
10
+ ## 依据
11
+
12
+ - [ISO C++ `[vector]`](https://eel.is/c++draft/vector):容量、扩容与 `reserve` 的保证。
13
+ - [cppreference: `std::vector`](https://en.cppreference.com/w/cpp/container/vector):容量接口与迭代器失效。
14
+ - [C++ Core Guidelines: Per.16](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#s-performance):用紧凑的连续数据结构提升缓存命中。