@chengzhao-dev/dsh-cpp-workflow 0.6.16 → 0.6.20

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 (156) hide show
  1. package/README.en.md +7 -7
  2. package/README.md +7 -7
  3. package/lib/client.js +1 -1
  4. package/lib/comment-policy.js +1 -1
  5. package/lib/index.js +1 -1
  6. package/package.json +3 -3
  7. package/skills/building-cpp/SKILL.md +29 -16
  8. package/skills/building-cpp/project/vscode/01-extensions.md +22 -0
  9. package/skills/building-cpp/project/vscode/02-settings.md +33 -0
  10. package/skills/building-cpp/references/build-script.md +6 -6
  11. package/skills/building-cpp/references/environment.md +1 -1
  12. package/skills/building-cpp/steps/01-build.md +6 -6
  13. package/skills/building-cpp/steps/02-tooling.md +16 -8
  14. package/skills/building-cpp/steps/03-check.md +6 -6
  15. package/skills/developing-cpp/SKILL.md +37 -13
  16. package/skills/developing-cpp/scripts/checking-comments.mjs +33 -6
  17. package/skills/developing-cpp/{references/change-model.md → source/01-editing/01-edit-operations.md} +27 -9
  18. package/skills/developing-cpp/{references/priority.md → source/01-editing/02-rule-priority.md} +1 -1
  19. package/skills/developing-cpp/{references/google-cpp-style.md → source/02-style/01-google-cpp-style.md} +10 -9
  20. package/skills/developing-cpp/{references/project-design.md → source/03-design/01-project-design.md} +12 -5
  21. package/skills/developing-cpp/source/03-design/examples/behavior-sentence.md +14 -0
  22. package/skills/developing-cpp/source/03-design/examples/coordinate-table.md +31 -0
  23. package/skills/developing-cpp/source/03-design/examples/header-place.md +26 -0
  24. package/skills/developing-cpp/source/03-design/examples/library-boundary.md +30 -0
  25. package/skills/developing-cpp/source/03-design/examples/library-split.md +7 -0
  26. package/skills/developing-cpp/source/03-design/examples/move-result.md +39 -0
  27. package/skills/developing-cpp/source/03-design/examples/preview-read.md +40 -0
  28. package/skills/developing-cpp/{references/layout.md → source/04-layout/01-source-layout.md} +4 -4
  29. package/skills/developing-cpp/{references/file-template.md → source/04-layout/02-file-template.md} +2 -2
  30. package/skills/developing-cpp/{references/declaration-order.md → source/04-layout/03-declaration-order.md} +4 -4
  31. package/skills/developing-cpp/source/05-language/01-language-basics/01-objects.md +9 -0
  32. package/skills/developing-cpp/source/05-language/01-language-basics/02-initialization.md +19 -0
  33. package/skills/developing-cpp/source/05-language/01-language-basics/03-deduction.md +6 -0
  34. package/skills/developing-cpp/source/05-language/01-language-basics/04-const.md +15 -0
  35. package/skills/developing-cpp/source/05-language/01-language-basics/05-expressions.md +8 -0
  36. package/skills/developing-cpp/source/05-language/01-language-basics/06-functions.md +9 -0
  37. package/skills/developing-cpp/source/05-language/01-language-basics/07-classes.md +9 -0
  38. package/skills/developing-cpp/source/05-language/01-language-basics/08-raii.md +9 -0
  39. package/skills/developing-cpp/source/05-language/01-language-basics/09-templates.md +20 -0
  40. package/skills/developing-cpp/source/05-language/01-language-basics/10-checklist.md +9 -0
  41. package/skills/developing-cpp/source/05-language/02-value-types/01-width.md +11 -0
  42. package/skills/developing-cpp/source/05-language/02-value-types/02-signed.md +20 -0
  43. package/skills/developing-cpp/source/05-language/02-value-types/03-integer-roles.md +23 -0
  44. package/skills/developing-cpp/source/05-language/02-value-types/04-enum.md +17 -0
  45. package/skills/developing-cpp/source/05-language/02-value-types/05-strong-type.md +17 -0
  46. package/skills/developing-cpp/source/05-language/02-value-types/06-const-method.md +17 -0
  47. package/skills/developing-cpp/source/05-language/02-value-types/07-nodiscard.md +9 -0
  48. package/skills/developing-cpp/source/05-language/02-value-types/08-comparison.md +4 -0
  49. package/skills/developing-cpp/source/05-language/02-value-types/09-subscript.md +14 -0
  50. package/skills/developing-cpp/source/05-language/02-value-types/10-float-bool.md +7 -0
  51. package/skills/developing-cpp/source/05-language/02-value-types/11-checklist.md +8 -0
  52. package/skills/developing-cpp/source/05-language/03-standard-library/01-containers.md +23 -0
  53. package/skills/developing-cpp/source/05-language/03-standard-library/02-access.md +8 -0
  54. package/skills/developing-cpp/source/05-language/03-standard-library/03-iteration.md +12 -0
  55. package/skills/developing-cpp/source/05-language/03-standard-library/04-algorithms.md +20 -0
  56. package/skills/developing-cpp/source/05-language/03-standard-library/05-string.md +7 -0
  57. package/skills/developing-cpp/source/05-language/03-standard-library/06-format.md +44 -0
  58. package/skills/developing-cpp/source/05-language/03-standard-library/07-time.md +3 -0
  59. package/skills/developing-cpp/source/05-language/03-standard-library/08-random.md +3 -0
  60. package/skills/developing-cpp/source/05-language/03-standard-library/09-files.md +4 -0
  61. package/skills/developing-cpp/source/05-language/03-standard-library/10-checklist.md +9 -0
  62. package/skills/developing-cpp/source/05-language/04-performance/01-measure.md +15 -0
  63. package/skills/developing-cpp/source/05-language/04-performance/02-complexity.md +7 -0
  64. package/skills/developing-cpp/source/05-language/04-performance/03-memory.md +8 -0
  65. package/skills/developing-cpp/source/05-language/04-performance/04-undefined-behavior.md +20 -0
  66. package/skills/developing-cpp/source/05-language/04-performance/05-pitfalls.md +11 -0
  67. package/skills/developing-cpp/source/05-language/04-performance/06-false-optimizations.md +6 -0
  68. package/skills/developing-cpp/source/05-language/04-performance/07-checklist.md +8 -0
  69. package/skills/developing-cpp/{references/glossary.md → source/05-language/glossary/language-terms.md} +2 -2
  70. package/skills/developing-cpp/{references/naming.md → source/06-naming/01-identifier-names.md} +8 -3
  71. package/skills/developing-cpp/source/06-naming/examples/naming-accessor.md +17 -0
  72. package/skills/developing-cpp/source/06-naming/examples/naming-basic.md +3 -0
  73. package/skills/developing-cpp/source/06-naming/examples/naming-current-prefix.md +19 -0
  74. package/skills/developing-cpp/source/06-naming/examples/naming-lookup.md +18 -0
  75. package/skills/developing-cpp/source/06-naming/examples/naming-lower-layer.md +39 -0
  76. package/skills/developing-cpp/source/06-naming/examples/naming-multiple-return.md +20 -0
  77. package/skills/developing-cpp/source/06-naming/examples/naming-player-mark.md +17 -0
  78. package/skills/developing-cpp/source/06-naming/examples/naming-predicate.md +18 -0
  79. package/skills/developing-cpp/source/06-naming/examples/naming-rename.md +5 -0
  80. package/skills/developing-cpp/source/06-naming/examples/naming-responsibility.md +19 -0
  81. package/skills/developing-cpp/source/06-naming/examples/naming-role.md +21 -0
  82. package/skills/developing-cpp/source/06-naming/examples/naming-state-change.md +57 -0
  83. package/skills/developing-cpp/{references/comments.md → source/07-comments/01-comment-rules.md} +55 -32
  84. package/skills/developing-cpp/source/07-comments/examples/declaration-class.md +44 -0
  85. package/skills/developing-cpp/source/07-comments/examples/declaration-constructor.md +22 -0
  86. package/skills/developing-cpp/source/07-comments/examples/declaration-enum.md +24 -0
  87. package/skills/developing-cpp/source/07-comments/examples/declaration-file.md +66 -0
  88. package/skills/developing-cpp/source/07-comments/examples/declaration-function.md +100 -0
  89. package/skills/developing-cpp/source/07-comments/examples/declaration-member.md +25 -0
  90. package/skills/developing-cpp/source/07-comments/examples/declaration-predicate.md +13 -0
  91. package/skills/developing-cpp/source/07-comments/examples/declaration-struct.md +18 -0
  92. package/skills/developing-cpp/source/07-comments/examples/implementation-body.md +42 -0
  93. package/skills/developing-cpp/source/07-comments/examples/implementation-todo.md +16 -0
  94. package/skills/developing-cpp/source/07-comments/glossary/comment-terms.md +75 -0
  95. package/skills/developing-cpp/{references/exceptions.md → source/08-exceptions/01-throw-rules.md} +46 -8
  96. package/skills/developing-cpp/source/08-exceptions/examples/throw-boundary.md +28 -0
  97. package/skills/developing-cpp/source/08-exceptions/examples/throw-build-mode.md +38 -0
  98. package/skills/developing-cpp/source/08-exceptions/examples/throw-catch.md +21 -0
  99. package/skills/developing-cpp/source/08-exceptions/examples/throw-destructor.md +21 -0
  100. package/skills/developing-cpp/source/08-exceptions/examples/throw-format.md +13 -0
  101. package/skills/developing-cpp/source/08-exceptions/examples/throw-message.md +17 -0
  102. package/skills/developing-cpp/source/08-exceptions/examples/throw-when.md +27 -0
  103. package/skills/developing-cpp/steps/01-behavior.md +5 -5
  104. package/skills/developing-cpp/steps/02-boundary.md +9 -9
  105. package/skills/developing-cpp/steps/03-declaration.md +11 -10
  106. package/skills/developing-cpp/steps/04-implementation.md +15 -13
  107. package/skills/engineering/SKILL.md +84 -0
  108. package/skills/{configuring-cmake/references/cmake.md → engineering/cmake/01-targets/01-rules.md} +3 -3
  109. package/skills/engineering/cmake/01-targets/examples/glob.md +22 -0
  110. package/skills/engineering/cmake/01-targets/examples/rpath.md +24 -0
  111. package/skills/{configuring-cmake/examples/cmake.md → engineering/cmake/01-targets/examples/target.md} +1 -69
  112. package/skills/{configuring-cmake/references/modules.md → engineering/cmake/02-modules/01-functions.md} +2 -2
  113. package/skills/{configuring-cmake/references/cxx-standard.md → engineering/cmake/03-standard/01-cxx-standard.md} +1 -1
  114. package/skills/engineering/cmake/03-standard/examples/standard.md +19 -0
  115. package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/README.md +6 -6
  116. package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++20/README.md +1 -1
  117. package/skills/{building-cpp/references/linking.md → engineering/cmake/linking/01-libraries.md} +1 -48
  118. package/skills/engineering/cmake/linking/02-rpath.md +17 -0
  119. package/skills/engineering/cmake/linking/03-undefined-reference.md +14 -0
  120. package/skills/engineering/cmake/linking/04-inspect.md +15 -0
  121. package/skills/{configuring-cmake → engineering/cmake}/steps/01-targets.md +3 -3
  122. package/skills/{configuring-cmake → engineering/cmake}/steps/02-modules.md +2 -2
  123. package/skills/{configuring-cmake → engineering/cmake}/steps/03-check.md +2 -2
  124. package/skills/{building-cpp/references/testing.md → engineering/cmake/testing/ctest.md} +3 -3
  125. package/skills/engineering/formatting/02-clang-format.md +14 -0
  126. package/skills/engineering/formatting/03-clang-tidy.md +51 -0
  127. package/skills/engineering/formatting/04-clangd.md +83 -0
  128. package/skills/engineering/formatting/examples/clang-format.md +17 -0
  129. package/skills/engineering/formatting/examples/clang-tidy.md +64 -0
  130. package/skills/{building-cpp/examples/tooling.md → engineering/formatting/examples/clangd.md} +1 -80
  131. package/templates/cpp-workflow-project/SKILL.md +34 -4
  132. package/skills/building-cpp/references/static-analysis.md +0 -148
  133. package/skills/configuring-cmake/SKILL.md +0 -68
  134. package/skills/developing-cpp/examples/comments-declaration.md +0 -313
  135. package/skills/developing-cpp/examples/comments-implementation.md +0 -66
  136. package/skills/developing-cpp/examples/exceptions.md +0 -173
  137. package/skills/developing-cpp/examples/naming.md +0 -150
  138. package/skills/developing-cpp/examples/structure.md +0 -155
  139. package/skills/developing-cpp/references/comment-terms.md +0 -65
  140. package/skills/developing-cpp/references/language.md +0 -121
  141. package/skills/developing-cpp/references/performance.md +0 -83
  142. package/skills/developing-cpp/references/standard-library.md +0 -101
  143. package/skills/developing-cpp/references/types.md +0 -154
  144. /package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++11/README.md +0 -0
  145. /package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++11/cmake-snippet.cmake +0 -0
  146. /package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++14/README.md +0 -0
  147. /package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++14/cmake-snippet.cmake +0 -0
  148. /package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++17/README.md +0 -0
  149. /package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++17/cmake-snippet.cmake +0 -0
  150. /package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++20/cmake-snippet.cmake +0 -0
  151. /package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++23/README.md +0 -0
  152. /package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++23/cmake-snippet.cmake +0 -0
  153. /package/skills/{configuring-cmake/assets/cmake → engineering/cmake/assets}/CompileSettings.cmake +0 -0
  154. /package/skills/{configuring-cmake/assets/cmake → engineering/cmake/assets}/LinkSettings.cmake +0 -0
  155. /package/skills/{building-cpp/references/encoding.md → engineering/formatting/01-encoding.md} +0 -0
  156. /package/skills/{building-cpp → engineering/formatting}/scripts/checking-encoding.mjs +0 -0
@@ -1,150 +0,0 @@
1
- # 正反例:命名
2
-
3
- ## 基本规则
4
-
5
- 大小写、前缀与成员尾随 `_` 的规则表见 `references/naming.md`;下面只收需要语境才能判断的例子。
6
-
7
- ## 访问器
8
-
9
- **差**:
10
-
11
- ```cpp
12
- Order current_order() const; // 成员名直接当函数名
13
- void set_current_order(Order order);
14
- ```
15
-
16
- **好**:
17
-
18
- ```cpp
19
- [[nodiscard]] Order GetCurrentOrder() const;
20
- void AdvanceOrder(); // 领域动作,不是 setter
21
- ```
22
-
23
- 查询统一 `Get` + PascalCase;修改用领域动词。只有真正需要替换值时,才考虑 `Set` 形式,并说明为什么不是动作。
24
-
25
- ## 语义角色
26
-
27
- **差**:
28
-
29
- ```cpp
30
- bool Check(int id) const; // Check 什么?
31
- Result DoSomething(Task task); // 做什么?
32
- TaskState To(TaskState state); // 从什么到什么?
33
- ```
34
-
35
- **好**:
36
-
37
- ```cpp
38
- [[nodiscard]] bool Contains(int id) const;
39
- [[nodiscard]] ParseResult TryParse(std::string_view text);
40
- [[nodiscard]] TaskState StatusCodeToTaskState(int code);
41
- ```
42
-
43
- - 判断用 `Is` / `Has`。
44
- - 可能失败但不抛用 `Try`。
45
- - 映射名字含两端角色。
46
-
47
- ## `current_` 前缀
48
-
49
- **差**:
50
-
51
- ```cpp
52
- Order current_order_; // 只有一个订单时,current 是噪音
53
- ```
54
-
55
- **好**:
56
-
57
- ```cpp
58
- Order order_;
59
- ```
60
-
61
- **该用**:
62
-
63
- ```cpp
64
- Order current_order_; // 有 current_order_ 与 next_order_,不写会混
65
- ```
66
-
67
- ## 名字与职责
68
-
69
- **差**:
70
-
71
- ```cpp
72
- class DataManager {}; // 管什么?
73
- class StringHelper {}; // 为什么是类?
74
- ```
75
-
76
- **好**:
77
-
78
- ```cpp
79
- class Warehouse {}; // 持有库存,维持不变量
80
- namespace string_utils { // 一组自由函数
81
- [[nodiscard]] std::string Trim(std::string_view text);
82
- }
83
- ```
84
-
85
- 避免 `Manager`、`Helper`、`Util`;名字要能回答「它负责什么」。一组无状态函数用命名空间,不要造类。
86
-
87
- ## 查找与读取
88
-
89
- **差**:
90
-
91
- ```cpp
92
- [[nodiscard]] Task GetTask(std::string_view name) const; // 可能找不到时返回什么?
93
- [[nodiscard]] bool FindTask(std::string_view name) const; // 返回 bool 却叫 Find
94
- ```
95
-
96
- **好**:
97
-
98
- ```cpp
99
- [[nodiscard]] const Task& GetTask(std::string_view name) const; // 找不到抛 TaskNotFoundError
100
- [[nodiscard]] const Task* FindTask(std::string_view name) const; // 找不到返回 nullptr
101
- [[nodiscard]] bool HasTask(std::string_view name) const; // 只回答存在性
102
- ```
103
-
104
- `Get` 表示结果一定存在(否则抛出声明过的领域异常),`Find` 表示可能找不到,`Has` / `Contains` 只回答是否存在。不要用 `Find` 返回 `bool`。
105
-
106
- ## 谓词与 Lambda
107
-
108
- **差**:
109
-
110
- ```cpp
111
- bool valid(int value);
112
- auto check = [](const Task& task) { return task.done; };
113
- ```
114
-
115
- **好**:
116
-
117
- ```cpp
118
- [[nodiscard]] bool IsValid(int value);
119
- auto is_done = [](const Task& task) { return task.done; };
120
- auto has_owner = [](const Task& task) { return !task.owner.empty(); };
121
- ```
122
-
123
- 返回 `bool` 的自由函数用 `Is` / `Has` / `Can` / `Should` 开头;Lambda 用 snake_case 的 `is_` / `has_` / `can_`,读起来像条件。
124
-
125
- ## 多返回值
126
-
127
- **差**:
128
-
129
- ```cpp
130
- bool Parse(std::string_view text, int* out_value); // 出参 + 返回 bool
131
- ```
132
-
133
- **好**:
134
-
135
- ```cpp
136
- struct ParseResult {
137
- bool ok{false};
138
- int value{0};
139
- };
140
-
141
- [[nodiscard]] ParseResult TryParse(std::string_view text);
142
- ```
143
-
144
- 需要同时表达成功与结果时,用带字段的结构体或 `std::optional`,不要用输出参数加布尔返回。
145
-
146
- ## 改名检查
147
-
148
- - 搜全部调用点:声明、定义、测试、注释、文档。
149
- - 先改声明,再改定义,再改调用点;每一步都能编译。
150
- - 公共 API 改名保留旧名转发时,注释写弃用期限;不要静默删除。
@@ -1,155 +0,0 @@
1
- # 正反例:行为与边界
2
-
3
- ## 行为句
4
-
5
- **差**:实现一个 `TaskList` 类,用 `std::vector` 保存任务,支持添加、删除、按优先级排序,并在终端打印表格。
6
-
7
- 问题:把类型、容器、实现步骤混进行为;无法验收「表格」长什么样。
8
-
9
- **好**:
10
-
11
- ```text
12
- 给定一组任务(每行「优先级 名称」),程序按优先级从高到低输出名称;
13
- 输入为空时输出「没有任务」并返回 0;某行格式非法时输出行号并返回 1。
14
- ```
15
-
16
- 可以写成测试:输入三行,断言输出顺序;输入空,断言提示与退出码;输入坏行,断言行号。
17
-
18
- ## 边界
19
-
20
- **差**:库函数直接打印,程序只调用它。
21
-
22
- ```cpp
23
- // task_list.cpp(库)
24
- void TaskList::Print() const {
25
- for (const auto& task : tasks_) {
26
- std::cout << task.priority << " " << task.name << "\n";
27
- }
28
- }
29
- ```
30
-
31
- 问题:库依赖终端,测试要捕获 `std::cout`,GUI 或文件输出无法复用。
32
-
33
- **好**:库返回数据,程序决定怎么显示。
34
-
35
- ```cpp
36
- // task_list.h(库)
37
- [[nodiscard]] std::vector<Task> SortedByPriority() const;
38
-
39
- // app/cli.cpp(程序)
40
- void PrintTasks(const std::vector<Task>& tasks, std::ostream& out) {
41
- for (const auto& task : tasks) {
42
- out << std::format("{} {}\n", task.priority, task.name);
43
- }
44
- }
45
- ```
46
-
47
- 测试直接断言 `SortedByPriority()`;输出格式由程序测试负责。
48
-
49
- ## 头文件位置
50
-
51
- **差**:
52
-
53
- ```text
54
- src/task_list.h // 公共头藏在 src,测试无法只依赖公共接口
55
- src/detail.h // 私有头被测试直接包含
56
- ```
57
-
58
- ```cpp
59
- #include "src/task_list.h" // 从仓库根写到了实现目录
60
- ```
61
-
62
- **好**:
63
-
64
- ```text
65
- task_list/include/task_list.h // 公共头
66
- task_list/src/task_list.cpp // 实现
67
- task_list/src/detail.h // 私有头,只被本组件包含
68
- ```
69
-
70
- ```cpp
71
- #include "task_list.h" // 公共头按裸文件名包含
72
- ```
73
-
74
- 构建时把仓库根加入 include 搜索路径;不要用 `#include "../task_list/include/task_list.h"` 这种随文件位置漂移的写法。
75
-
76
- ## 单文件何时拆库
77
-
78
- **先不拆**:一个 `main.cpp`,辅助函数在匿名命名空间,只有程序自己用。
79
-
80
- **该拆**:测试需要直接调用排序逻辑;或第二个程序要复用同一逻辑;或逻辑已经超过一个屏幕。
81
-
82
- 拆的时候先定公共头(只暴露 `SortedByPriority`),把实现移到 `src/`,行为句不变,测试先绿再改构建。
83
-
84
- ## 固定坐标用表
85
-
86
- **差**:每次判断成线,都用循环把八条线的坐标重新拼一遍;行、列、对角线混在一个循环里,读者要跟着跑两遍才知道哪八条。
87
-
88
- ```cpp
89
- // 差:用循环现写八条线,坐标藏在控制流里。
90
- std::vector<std::vector<Position>> BuildLines() {
91
- std::vector<std::vector<Position>> lines;
92
- for (int row = 0; row < kRows; ++row) { /* ... */ }
93
- // 列与两条对角线再各写一段
94
- return lines;
95
- }
96
- ```
97
-
98
- **好**:固定坐标写成具名命名空间里的 `constexpr` 常量表,`HasLine` 与 `PreviewWin` 共用,所以不放进任何一个函数体;格子用 `at()`,整条线 `all_of`、八条线 `any_of`,函数不标 `noexcept`。
99
-
100
- ```cpp
101
- // board.cpp 的 namespace tictactoe:行、列、对角线三组,坐标固定。
102
- constexpr std::array<std::array<Position, 3>, 8> kLines = {{
103
- {{{0, 0}, {0, 1}, {0, 2}}}, // 行 0
104
- {{{1, 0}, {1, 1}, {1, 2}}}, // 行 1
105
- {{{2, 0}, {2, 1}, {2, 2}}}, // 行 2
106
- {{{0, 0}, {1, 0}, {2, 0}}}, // 列 0
107
- {{{0, 1}, {1, 1}, {2, 1}}}, // 列 1
108
- {{{0, 2}, {1, 2}, {2, 2}}}, // 列 2
109
- {{{0, 0}, {1, 1}, {2, 2}}}, // 对角线
110
- {{{0, 2}, {1, 1}, {2, 0}}}, // 反对角线
111
- }};
112
- ```
113
-
114
- 每个格子要分别处理时仍写 `row` / `col` 循环,例如收集空格、终端打印。
115
-
116
- ## 推演一步读原数据
117
-
118
- **差**:先复制整个棋盘,再在副本上落子,最后判断成线;调用方看不出副本只是中间量,还要重复处理落子失败。
119
-
120
- ```cpp
121
- Board hypothetical = *this;
122
- const Cell::PlaceResult placed = hypothetical.Place(position, state);
123
- if (placed != Cell::PlaceResult::kPlaced) {
124
- return WinPreviewResult::kInvalidPlace;
125
- }
126
- return hypothetical.HasLine(state) ? WinPreviewResult::kWouldWin
127
- : WinPreviewResult::kWouldNotWin;
128
- ```
129
-
130
- **好**:判断「这一格换成某种棋子后会不会成线」不需要第二份棋盘。越界、空格、已占用交给 `CheckPlaceable` 统一返回原因;通过之后只在包含该坐标的线上把这一格算作 `state`,其余格子读原棋盘。
131
-
132
- ```cpp
133
- WinPreviewResult Board::PreviewWin(Position position, CellState state) const {
134
- if (CheckPlaceable(position, state) != Cell::PlaceResult::kPlaced) {
135
- return WinPreviewResult::kInvalidPlace;
136
- }
137
-
138
- const auto matches = [&](Position cell) {
139
- if (cell.row == position.row && cell.col == position.col) {
140
- return true;
141
- }
142
- return cells_.at(cell.row).at(cell.col) == state;
143
- };
144
-
145
- const bool completes = std::ranges::any_of(kLines, [&](const auto& line) {
146
- const bool contains = std::ranges::find_if(line, [&](Position cell) {
147
- return cell.row == position.row && cell.col == position.col;
148
- }) != line.end();
149
- return contains && std::ranges::all_of(line, matches);
150
- });
151
- return completes ? WinPreviewResult::kWouldWin : WinPreviewResult::kWouldNotWin;
152
- }
153
- ```
154
-
155
- 先用 `IsInRange` 拒绝越界,再用 `at()` 读取;失败路径不碰数组,`noexcept` 也就不再成立。
@@ -1,65 +0,0 @@
1
- # 注释术语与用词
2
-
3
- 本文件是 `comments.md` 的附录,只在判不准用词、改写类型注释或核对书稿摘录时打开;它不随 `comments.md` 一起加载,也不重复句式、位置和长度规则。
4
-
5
- ## 一条概念只认一个词
6
-
7
- | 概念 | 首选 | 避免 | 说明 |
8
- | --- | --- | --- | --- |
9
- | `Position` 类型 | 行列坐标 | 位置类型、坐标点 | 类型名和参数名保持 `Position`、`position` |
10
- | 棋盘上的点 | 位置 | 坐标点、格子坐标 | 说语义位置时用「位置」 |
11
- | `Cell` | 格子 | 单元、方格、棋子 | 「棋子」只指对局中的 X 或 O |
12
- | `Board` | 棋盘 | 盘面、棋局 | 「棋局」只指一局对局 |
13
- | `CellState` 类型 | 格子状态 | 格子的状态、格子状态值 | 类型注释写「格子状态。」 |
14
- | `CellState` 的取值 | 格子为空、格子为 X、格子为 O | 状态为空、尚未设置、状态为 X | 每行都以「格子」开头,写完整状态 |
15
- | `Place` 的动作 | 更新状态 | 写入格子、放置、落子 | `Board::Place` 也用「更新」 |
16
- | `PlaceResult` 类型 | 格子状态变更结果 | 格子状态的变更结果、放置结果 | 类型注释写「格子状态变更结果。」 |
17
- | `PlaceResult` 的取值 | 格子状态已变更/未变更:原因 | 已变更、未变更、成功、失败 | 先写主体与变更,再写原因 |
18
- | `GameResult` 类型 | 对局结果 | 对局的结果、游戏结果、一局的结果 | 类型注释写「对局结果。」 |
19
- | `GameResult` 的取值 | 对局尚未结束、对局由 X 获胜、对局由 O 获胜、对局以平局结束 | 尚未结束、X 成线、O 成线、棋盘已满 | 每行都以「对局」开头,写完整结果 |
20
- | `Clear` 的动作 | 重置为空、清空 | 清空值、收回 `kEmpty` | 逐格重置,不叫「清空值」 |
21
- | `GetCell` 的返回 | 格子副本 | 格子、引用 | 按值返回必须说「副本」 |
22
- | 坐标越界 | 坐标越界时抛出 `std::out_of_range` | 检查越界 | 只用于会抛的校验函数 |
23
- | `GetGameResult` | 当前结果 | 现在的结果、最新结果 | 结果是棋盘的快照 |
24
- | `row`、`col` | 行号、列号 | 行、列 | 成员注释写「从 0 起算」,类注释不重复 |
25
-
26
- ## 位置与坐标
27
-
28
- - `Position` 是类型名,中文叫「行列坐标」;参数名用 `position`,不改成 `pos`。
29
- - 「棋盘上的一个位置」指语义位置;「一行一列的坐标」指具体值。两者不混成「坐标点」。
30
- - 成员注释写「行号,从 0 起算」「列号,从 0 起算」;类注释不再重复「都从 0 起算」,否则和成员注释冲突。
31
-
32
- ## 格子与棋盘
33
-
34
- - `Cell` 只说「格子」;「棋盘上的格子」只在需要强调归属时用,类型注释本身不写棋盘。
35
- - `Board` 只说「棋盘」;`cells_` 说「格子数组」,不写「存储格子的数组」这种复述。
36
- - 「棋子」只指对局中的 X 或 O;格子的内部值叫「状态」,不叫「棋子」。
37
- - `PlaceResult` 的三种结果说「格子状态已变更」「格子状态未变更」,不说「成功」「失败」;拒绝覆盖说「已有状态」,不说「占据」。
38
-
39
- ## 更新与写入
40
-
41
- - `Place` 的动作统一写「更新状态」:`Cell::Place` 更新一格,`Board::Place` 把坐标转给对应格子。
42
- - 「写入」只用于数组、文件、流这类输出目标;不写「写入一个格子」「把状态写入格子」。
43
- - 「设置」只表示有没有状态;「更新」表示这次动作;「已有状态」表示拒绝覆盖。三者不互换。
44
- - `Clear` 说「重置为空」或「重置为 `kEmpty`」,不写「清空值」「删除状态」。
45
-
46
- ## 返回值与副本
47
-
48
- - `GetCell` 按值返回:第一句和 `@return` 都写「格子副本」,不写「该位置的格子」,否则读者会以为拿到引用。
49
- - `GetState` 返回枚举值,写「当前状态」,不写「状态值」。
50
- - `GetGameResult` 写「当前结果」,不写「现在的结果」。
51
- - 会抛的查询先写抛出条件,再写 `@throws`;`@param` 写「待检查/读取/更新的坐标」,不把「棋盘内」当前置条件。
52
-
53
- ## 初始状态与特殊成员
54
-
55
- - 类注释拥有初始状态与生命周期;默认构造函数只写「建立初始状态」,不再重复具体状态值。
56
- - 数据成员只写成员自身:`row`、`col` 写行号/列号与从 0 起算;`state_` 写「格子当前的状态」,不重复类注释的初始值。
57
- - 文件头只写 `@file`、`@brief` 与文件组成;不写作者、版权年份、变更史。
58
-
59
- ## 依据
60
-
61
- - [Google 中文 C++ 风格指南·注释](https://zh-google-styleguide.readthedocs.io/en/stable/google-cpp-styleguide/comments.html):类讲用途,函数讲输入输出,不翻译代码。
62
- - [LeviLamina C++ 风格指南](https://docs.levimc.org/LeviLamina/zh/maintainer_guides/cpp_style_guide/):类注释至少简介与公开成员,函数注释含参数、返回值与异常。
63
- - [OpenHarmony C++ 语言编程规范·注释](https://www.bookstack.cn/read/openharmony-1.0-zh-cn/contribute-OpenHarmony-cpp-coding-style-guide.md):按需、简洁、无二义性,不重复代码信息。
64
-
65
- 判不准时先查本表,再查 `comments.md` 的句式与位置;改完运行 `node "<skill-dir>/scripts/checking-comments.mjs" --root <项目根>`。
@@ -1,121 +0,0 @@
1
- # 语言基础与对象语义
2
-
3
- 这里只写“换一台机器、换一个编译器仍然成立”的 C++11–C++23 语言规则;示例默认 C++23,C++11/14/17/20 项目按声明降级。项目专属类型、协议字段和构建目录不在本文件。
4
-
5
- ## 值与对象
6
-
7
- - 类型决定允许的操作和表示;对象是某类型的一块存储;值是可以复制、比较、传递的内容。
8
- - 变量有名字、类型、存储期和作用域。局部变量在离开作用域时销毁;`static` 局部变量在首次执行到声明时初始化,程序结束时销毁。
9
- - 声明让名字可见,定义分配存储或给出函数体。一个实体可以声明多次,但只能定义一次;头文件只放声明,定义放 `.cpp`。
10
- - 引用是已有对象的别名,必须初始化,不能重新绑定;指针可以为空,也可以改指向。接口优先用引用表达“必须存在”,用指针表达“可以不存在”。
11
- - `*this` 不用于复制当前对象:要一份副本就直说语义(如“推演落子”只读原数据,或显式写一个复制函数)。`return *this` 仍只属于赋值运算符,井字棋没有这种运算符。
12
-
13
- ## 初始化与列表初始化
14
-
15
- 默认用花括号初始化,只在需要对比、`auto` 推导或赋值语义时用 `=`;`name = {{` 只用于多行聚合常量表:
16
-
17
- ```cpp
18
- int count{}; // 值初始化,0
19
- std::vector<int> values{1, 2, 3}; // 列表初始化
20
- std::string name = "demo"; // 拷贝初始化,类型明确
21
- auto total = 0.0; // auto 只能用 =,不能用 {}
22
- ```
23
-
24
- - `kLines` 这类多行聚合常量表写 `name = {{`;单个对象和空初始化仍写 `count{}`,不写 `=`;不把其他花括号初始化改成 `=`。
25
- - `T value{}` 做值初始化:内置类型得 0,类类型调用默认构造函数。
26
- - 花括号禁止窄化转换:`int x{3.5}` 编译失败,`int x = 3.5` 静默截断。需要截断时显式 `static_cast<int>(3.5)`。
27
- - `std::vector<int> values(3)` 是三个 0;`values{3}` 是一个元素 3。两种括号语义不同,不要凭习惯混用。
28
- - 类成员在声明处写默认成员初始化(NSDMI):标量 `count_{0}`,数组 `items_{}`,容器和类类型成员不写空 `{}`。
29
- - 构造函数初始化列表按成员声明顺序执行,不按书写顺序;初始化列表顺序与声明顺序保持一致,避免依赖未初始化成员。
30
-
31
- ## `auto` 与类型推导
32
-
33
- - `auto` 适合“右侧类型显然且无关紧要”的局部变量,例如迭代器、`make_unique` 结果、范围 `for`。
34
- - 公共接口、类成员、函数返回类型写显式类型;返回值需要隐藏实现细节时才可以写 `auto`,且头文件里要能看出契约。
35
- - `auto` 会丢掉顶层 `const` 和引用:`auto value = const_ref` 得到副本;需要引用时写 `const auto&`。
36
- - 结构化绑定 `auto [key, value] : map` 中,`key` 是副本;要避免复制时写 `const auto& [key, value]`。
37
-
38
- ## `const`、`constexpr`、`consteval`、`constinit`
39
-
40
- | 关键字 | 承诺 | 典型位置 |
41
- | --- | --- | --- |
42
- | `const` | 初始化后不能通过这个名字修改 | 局部变量、参数、成员函数尾部 |
43
- | `constexpr` | 能在编译期求值(满足条件时) | 常量、函数、构造函数 |
44
- | `consteval` | 必须在编译期求值 | 编译期校验函数 |
45
- | `constinit` | 静态存储期变量在编译期完成初始化 | 全局/静态变量 |
46
- | `inline constexpr` | 头文件里可共享的编译期常量,避免 ODR 问题 | 命名空间作用域常量 |
47
-
48
- - 共享常量写在头文件的命名空间作用域:`inline constexpr int kMaxRows = 3;`,不要用 `#define`。`.cpp` 里非 `inline`、非 `extern` 的命名空间作用域 `constexpr` 已有内部链接。
49
- - `constexpr` 函数的定义要放在头文件里,否则其他翻译单元无法在编译期调用。
50
- - 不要给所有函数加 `constexpr`;只有确实需要在编译期使用、且实现满足约束时才加。
51
- - `constinit` 只保证初始化时机,不保证不可修改;需要不可修改时同时写 `const`。
52
- - C++20 起 `constexpr` 函数可以包含循环、局部变量和部分动态分配,但不要借此把运行期逻辑塞进编译期。
53
-
54
- ## 表达式与比较
55
-
56
- - 比较写成“变量在左、常量在右”:`if (id < kTaskCount)`,不写 Yoda 条件 `if (kTaskCount > id)`。编译器会在漏写 `=` 时给出警告,可读性也更好。
57
- - 混合优先级的算术表达式加括号:`(a + b) * c`,不依赖读者记忆优先级。`readability-math-missing-parentheses` 会检查。
58
- - 区间用半开区间 `[low, high)`;判空用 `empty()`,不要用 `size() == 0`。
59
- - 逻辑表达式按语义分组,一组一个括号;不要把多个否定串成一行。
60
- - 自增自减单独成句;不要在同一个表达式里对同一对象多次修改,那是未定义行为。
61
-
62
- ## 函数、参数与返回值
63
-
64
- - 命名规则以 `references/naming.md` 为准:函数 PascalCase、查询 `Is` / `Has` / `Can` / `Get` / `Find`、转换 `SourceToTarget`。
65
- - 输入参数默认按值传小对象、按 `const&` 传大对象;需要修改调用方对象时用引用参数,并在名字或注释里标 `[out]`。
66
- - 优先返回值而不是输出参数;需要返回多个值时用 `struct` 聚合,不用多个 `bool` 输出参数。
67
- - 返回容器或大对象时按值返回,依赖 RVO / 移动;不要返回局部对象的引用或指针。
68
- - 单参数构造函数加 `explicit`,除非确实需要隐式转换;转换运算符也加 `explicit`。
69
- - 不在虚函数上写默认参数;默认参数在静态类型处绑定,派生类改默认值会造成意外。
70
- - 函数只做一件事;超过一屏时先找可命名的子步骤,分阶段与注释写法见 `steps/04-implementation.md`。
71
-
72
- ## 类、结构体与特殊成员
73
-
74
- - `struct` 用于公开数据的聚合;`class` 用于维护不变量、隐藏表示。两者只差默认访问权限,按语义选。
75
- - 数据成员默认私有;需要暴露只读视图时提供 `const` 成员函数,不返回可变内部引用。
76
- - 单参数构造函数用 `explicit`;构造函数失败时抛异常,不留下半初始化对象。
77
- - 特殊成员(构造、析构、拷贝、移动)在头文件声明,在 `.cpp` 定义;只有默认实现足够且零参数时才写 `= default`。
78
- - 定义了析构函数、拷贝构造或拷贝赋值之一时,认真考虑其余特殊成员;用 `= delete` 明确禁止不需要的操作。
79
- - 多态基类析构函数必须是 `virtual` 或 `protected`;否则通过基类指针删除派生对象是未定义行为。
80
- - 不在构造函数里调用虚函数;此时派生部分还没构造完。
81
-
82
- ## RAII、所有权与移动
83
-
84
- - 资源获取即初始化:用对象生存期管理内存、文件、锁、套接字;析构函数负责释放。
85
- - 默认用 `std::unique_ptr<T>` 表达独占所有权;需要共享所有权时才用 `std::shared_ptr<T>`,并用 `std::weak_ptr` 打破循环。
86
- - 不写裸 `new` / `delete`;需要动态数组时用 `std::vector`,需要单个对象时用 `std::make_unique`。
87
- - 移动是“把资源指针转走并置空源对象”的转换;`std::move` 只是 `static_cast<T&&>`,本身不移动任何东西。
88
- - 移动后的对象处于有效但未指定状态;只能重新赋值或销毁,不要读取它的值。
89
- - 移动构造函数和移动赋值标记 `noexcept`,否则 `std::vector` 等容器在扩容时可能选择拷贝。
90
- - 返回值直接 `return value;`,不要 `return std::move(value);`;后者阻止 RVO。
91
-
92
- ## 模板、概念与 CTAD
93
-
94
- - 模板定义放头文件;`.cpp` 里的模板实例化只对当前翻译单元可见。
95
- - C++20 用概念约束模板参数,把错误从实例化深处提前到调用点:
96
-
97
- ```cpp
98
- template <typename T>
99
- concept Numeric = std::is_arithmetic_v<T>;
100
-
101
- template <Numeric T>
102
- T Clamp(T value, T low, T high) {
103
- if (value < low) return low;
104
- if (value > high) return high;
105
- return value;
106
- }
107
- ```
108
-
109
- - 概念只表达语法和语义约束,不替代文档;错误消息要能让调用者知道违反了哪条约束。
110
- - CTAD(类模板实参推导)用于局部变量和明显场景:`std::pair pair{1, 2.0};`;公共接口写显式模板实参,避免推导规则变化影响契约。
111
- - 不用模板做运行期分支;需要运行期多态时用虚函数或 `std::variant`。
112
-
113
- ## 检查清单
114
-
115
- - [ ] 初始化方式统一,窄化转换被编译器拦住。
116
- - [ ] `auto` 只出现在类型显然的局部变量;公共接口有显式类型。
117
- - [ ] 共享常量用 `inline constexpr`,没有宏常量。
118
- - [ ] 比较不是 Yoda 形式,混合优先级有括号。
119
- - [ ] 资源由 RAII 管理,没有裸 `new` / `delete`。
120
- - [ ] 特殊成员声明与定义位置正确,移动操作 `noexcept`。
121
- - [ ] 模板约束用概念表达,定义在头文件。
@@ -1,83 +0,0 @@
1
- # 性能与常见陷阱
2
-
3
- 先证明慢在哪里,再改代码。没有测量的“优化”通常只是把复杂度搬了个位置。
4
-
5
- 本文件只管性能判断与测量;语言规则见 `references/language.md`,容器与算法选择见 `references/standard-library.md`,静态检查开关见 `building-cpp` 的 `references/static-analysis.md`。
6
-
7
- ## 测量顺序
8
-
9
- 1. 写清可观察的性能目标:延迟、吞吐、内存上限,或“输入规模翻倍时耗时如何增长”。
10
- 2. 用真实规模的输入测量;玩具数据上的常数差异没有意义。
11
- 3. 先看算法复杂度,再看内存访问,再看常数因子。
12
- 4. 用 profiler 找热点;不要凭代码长短猜。
13
- 5. 改一处,复测同一指标;记录测量条件和结果。
14
-
15
- - 发布构建(`-O2` / `-O3`)测性能,调试构建测正确性。
16
- - 微基准要防止编译器把结果优化掉;使用输入、累加结果并输出。
17
- - 单次测量受缓存、频率、调度影响;取多次的中位数或分位数,不只报最好的一次。
18
-
19
- ## 复杂度与数据结构
20
-
21
- - 先问操作的渐进复杂度:`vector` 尾部追加摊还 O(1),中间插入 O(n);`map` 查找 O(log n);`unordered_map` 平均 O(1)、最坏 O(n)。
22
- - 嵌套循环里做线性查找通常是把 O(n) 变成 O(n²);先建索引或排序。
23
- - 频繁按 key 查找时,`unordered_map` 通常比 `map` 快,但迭代顺序不稳定;需要有序输出时不要选它。
24
- - 小规模数据(几十个元素)上,连续 `vector` 的线性扫描常比哈希表更快;不要机械套用复杂度。
25
- - 递归深度可能随输入增长时,改成迭代或显式栈;栈溢出不是可捕获的异常。
26
-
27
- ## 内存与缓存
28
-
29
- - 连续存储对缓存友好;`vector<struct>` 比 `vector<unique_ptr<struct>>` 更容易预取。
30
- - 只遍历部分字段时,AoS(数组的结构)可能浪费带宽;数据量大且按列访问时考虑 SoA(结构的数组),但先用 profiler 证明。
31
- - 小对象按值传递通常比堆分配快;不要为了“避免拷贝”给所有参数加指针。
32
- - `reserve` 减少 `vector` 扩容和搬移;只有已知上界或能准确估算时才用。
33
- - 移动大对象比拷贝便宜,但移动不是免费;不要把 `std::move` 当成优化开关。
34
- - 返回局部对象直接 `return value;`,依赖 RVO / NRVO;`return std::move(value);` 会阻止优化。
35
-
36
- ## 未定义行为与安全
37
-
38
- 以下行为没有标准结果,优化后可能表现为“有时对”:
39
-
40
- - 数组/容器越界访问(`operator[]`、指针算术)。
41
- - 解引用空指针或已释放对象(use-after-free、double free)。
42
- - 读取未初始化变量。
43
- - 有符号整数溢出。
44
- - 数据竞争:多个线程无同步地读写同一对象。
45
- - 用 `reinterpret_cast` 把对象当无关类型访问,违反严格别名规则。
46
- - 对象生存期结束后继续使用指针或引用。
47
-
48
- 工具:
49
-
50
- - AddressSanitizer(ASan)查越界、use-after-free、double free。
51
- - UndefinedBehaviorSanitizer(UBSan)查有符号溢出、空指针、对齐等问题。
52
- - ThreadSanitizer(TSan)查数据竞争。
53
- - `-Wall -Wextra -Wpedantic` 打开常用警告;不要靠关闭警告“修复”问题。
54
-
55
- ASan/UBSan 有运行期开销,只在测试和排查时开;发布构建仍要保留编译器警告。
56
-
57
- ## 常见性能陷阱
58
-
59
- | 现象 | 常见原因 | 先做什么 |
60
- | --- | --- | --- |
61
- | 循环里反复分配 | 每次迭代构造容器/字符串 | 移到循环外,或 `reserve` |
62
- | 大量拷贝 | 范围 `for` 按值、参数按值传大对象 | 改 `const&`,确认所有权 |
63
- | 哈希表慢 | 哈希冲突、频繁 rehash | 预留桶数,检查哈希质量 |
64
- | 字符串拼接慢 | 反复 `+` 产生临时对象 | `reserve` + `+=`,或 `std::format` 一次成型 |
65
- | 多线程没有变快 | 锁竞争、伪共享 | 缩小临界区,按缓存行对齐 |
66
- | 递归慢/栈溢出 | 重复子问题、深度大 | 记忆化或改迭代 |
67
- | 输出慢 | 每行 flush、频繁 `std::endl` | 用 `'\n'`,需要时再 flush |
68
-
69
- ## 不要做的“优化”
70
-
71
- - 不要用全局可变状态换速度;正确性和可测试性先于常数因子。
72
- - 不要为未测量的热点写复杂的手工内存管理。
73
- - 不要用 `reinterpret_cast` 绕过类型系统;需要二进制布局时用 `std::bit_cast` 并确认可平凡复制。
74
- - 不要把 `std::endl` 当换行符;它每次都会 flush。
75
-
76
- ## 检查清单
77
-
78
- - [ ] 性能目标可测量,测量条件已记录。
79
- - [ ] 先看复杂度,再看缓存,再看常数。
80
- - [ ] 没有未测量的“优化”。
81
- - [ ] 大对象传递和返回没有多余拷贝。
82
- - [ ] ASan/UBSan 在测试中跑过,警告没有被关闭。
83
- - [ ] 没有用未定义行为换速度。