@chengzhao-dev/dsh-cpp-workflow 0.6.19 → 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 (155) hide show
  1. package/README.en.md +6 -6
  2. package/README.md +6 -6
  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 +26 -13
  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 +27 -11
  16. package/skills/developing-cpp/scripts/checking-comments.mjs +3 -3
  17. package/skills/developing-cpp/{references/change-model.md → source/01-editing/01-edit-operations.md} +3 -3
  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} +9 -9
  20. package/skills/developing-cpp/{references/project-design.md → source/03-design/01-project-design.md} +4 -4
  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 -6
  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} +40 -24
  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/{references → source/07-comments/glossary}/comment-terms.md +17 -17
  95. package/skills/developing-cpp/{references/exceptions.md → source/08-exceptions/01-throw-rules.md} +13 -11
  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 +8 -8
  105. package/skills/developing-cpp/steps/03-declaration.md +11 -11
  106. package/skills/developing-cpp/steps/04-implementation.md +14 -14
  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 +1 -1
  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 -168
  138. package/skills/developing-cpp/examples/structure.md +0 -194
  139. package/skills/developing-cpp/references/language.md +0 -123
  140. package/skills/developing-cpp/references/performance.md +0 -83
  141. package/skills/developing-cpp/references/standard-library.md +0 -138
  142. package/skills/developing-cpp/references/types.md +0 -154
  143. /package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++11/README.md +0 -0
  144. /package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++11/cmake-snippet.cmake +0 -0
  145. /package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++14/README.md +0 -0
  146. /package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++14/cmake-snippet.cmake +0 -0
  147. /package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++17/README.md +0 -0
  148. /package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++17/cmake-snippet.cmake +0 -0
  149. /package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++20/cmake-snippet.cmake +0 -0
  150. /package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++23/README.md +0 -0
  151. /package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++23/cmake-snippet.cmake +0 -0
  152. /package/skills/{configuring-cmake/assets/cmake → engineering/cmake/assets}/CompileSettings.cmake +0 -0
  153. /package/skills/{configuring-cmake/assets/cmake → engineering/cmake/assets}/LinkSettings.cmake +0 -0
  154. /package/skills/{building-cpp/references/encoding.md → engineering/formatting/01-encoding.md} +0 -0
  155. /package/skills/{building-cpp → engineering/formatting}/scripts/checking-encoding.mjs +0 -0
@@ -1,168 +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
- Player OtherPlayer(Player player); // 另一端是谁不明显
31
- CellState CellStateOf(Player player); // 从什么换到什么没写全
32
- ```
33
-
34
- **好**:
35
-
36
- ```cpp
37
- [[nodiscard]] constexpr Player GetOpponent(Player player) noexcept;
38
- [[nodiscard]] constexpr CellState PlayerToCellState(Player player) noexcept;
39
- ```
40
-
41
- 玩家互换用 `GetOpponent`;玩家换成格子取值用 `PlayerToCellState`。查询名与返回类型一致:返回 `Player` 写 `GetCurrentPlayer`,返回 `CellState` 写 `GetCurrentMark`。
42
-
43
- ## 语义角色
44
-
45
- **差**:
46
-
47
- ```cpp
48
- bool Check(int id) const; // Check 什么?
49
- Result DoSomething(Task task); // 做什么?
50
- TaskState To(TaskState state); // 从什么到什么?
51
- ```
52
-
53
- **好**:
54
-
55
- ```cpp
56
- [[nodiscard]] bool Contains(int id) const;
57
- [[nodiscard]] ParseResult TryParse(std::string_view text);
58
- [[nodiscard]] TaskState StatusCodeToTaskState(int code);
59
- ```
60
-
61
- - 判断用 `Is` / `Has`。
62
- - 可能失败但不抛用 `Try`。
63
- - 映射名字含两端角色。
64
-
65
- ## `current_` 前缀
66
-
67
- **差**:
68
-
69
- ```cpp
70
- Order current_order_; // 只有一个订单时,current 是噪音
71
- ```
72
-
73
- **好**:
74
-
75
- ```cpp
76
- Order order_;
77
- ```
78
-
79
- **该用**:
80
-
81
- ```cpp
82
- Order current_order_; // 有 current_order_ 与 next_order_,不写会混
83
- ```
84
-
85
- ## 名字与职责
86
-
87
- **差**:
88
-
89
- ```cpp
90
- class DataManager {}; // 管什么?
91
- class StringHelper {}; // 为什么是类?
92
- ```
93
-
94
- **好**:
95
-
96
- ```cpp
97
- class Warehouse {}; // 持有库存,维持不变量
98
- namespace string_utils { // 一组自由函数
99
- [[nodiscard]] std::string Trim(std::string_view text);
100
- }
101
- ```
102
-
103
- 避免 `Manager`、`Helper`、`Util`;名字要能回答「它负责什么」。一组无状态函数用命名空间,不要造类。
104
-
105
- ## 查找与读取
106
-
107
- **差**:
108
-
109
- ```cpp
110
- [[nodiscard]] Task GetTask(std::string_view name) const; // 可能找不到时返回什么?
111
- [[nodiscard]] bool FindTask(std::string_view name) const; // 返回 bool 却叫 Find
112
- ```
113
-
114
- **好**:
115
-
116
- ```cpp
117
- [[nodiscard]] const Task& GetTask(std::string_view name) const; // 找不到抛 TaskNotFoundError
118
- [[nodiscard]] const Task* FindTask(std::string_view name) const; // 找不到返回 nullptr
119
- [[nodiscard]] bool HasTask(std::string_view name) const; // 只回答存在性
120
- ```
121
-
122
- `Get` 表示结果一定存在(否则抛出声明过的领域异常),`Find` 表示可能找不到,`Has` / `Contains` 只回答是否存在。不要用 `Find` 返回 `bool`。
123
-
124
- ## 谓词与 Lambda
125
-
126
- **差**:
127
-
128
- ```cpp
129
- bool valid(int value);
130
- auto check = [](const Task& task) { return task.done; };
131
- ```
132
-
133
- **好**:
134
-
135
- ```cpp
136
- [[nodiscard]] bool IsValid(int value);
137
- auto is_done = [](const Task& task) { return task.done; };
138
- auto has_owner = [](const Task& task) { return !task.owner.empty(); };
139
- ```
140
-
141
- 返回 `bool` 的自由函数用 `Is` / `Has` / `Can` / `Should` 开头;Lambda 用 snake_case 的 `is_` / `has_` / `can_`,读起来像条件。
142
-
143
- ## 多返回值
144
-
145
- **差**:
146
-
147
- ```cpp
148
- bool Parse(std::string_view text, int* out_value); // 出参 + 返回 bool
149
- ```
150
-
151
- **好**:
152
-
153
- ```cpp
154
- struct ParseResult {
155
- bool ok{false};
156
- int value{0};
157
- };
158
-
159
- [[nodiscard]] ParseResult TryParse(std::string_view text);
160
- ```
161
-
162
- 需要同时表达成功与结果时,用带字段的结构体或 `std::optional`,不要用输出参数加布尔返回。
163
-
164
- ## 改名检查
165
-
166
- - 搜全部调用点:声明、定义、测试、注释、文档。
167
- - 先改声明,再改定义,再改调用点;每一步都能编译。
168
- - 公共 API 改名保留旧名转发时,注释写弃用期限;不要静默删除。
@@ -1,194 +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
- **差**:调用方已经拿着一手,`Game` 又用 `GetCurrentPlayer()` 按位置重建一手;`Board::Place` 的 `[[nodiscard]]` 结果还被 `static_cast<void>` 丢掉。重建的玩家与调用方给的不一致时,历史会记下棋盘没有接受的落子。
119
-
120
- ```cpp
121
- if (!board_.GetCell(move.position).IsEmpty()) {
122
- return MoveResult::kOccupied;
123
- }
124
- const Move rebuilt{move.position, GetCurrentPlayer()};
125
- static_cast<void>(board_.Place(rebuilt.position, PlayerToCellState(rebuilt.player)));
126
- history_.push_back(rebuilt);
127
- return MoveResult::kPlaced;
128
- ```
129
-
130
- **好**:`Game::Place` 直接接收一手,先确认是否轮到该玩家,然后判终局,最后交给 `Board::Place`;坐标范围由棋盘检查。按 `Cell::SetStateResult` 分支写历史并换方。`kNoInput` 说明棋盘收到了空取值,抛 `std::logic_error`;`kFinished` 仍由 `Game` 判,因为棋盘不知道整局已经结束。
131
-
132
- ```cpp
133
- Game::MoveResult Game::Place(Move move) {
134
- // 先确认轮到谁,再判终局;坐标范围由 Board::Place 检查。
135
- if (move.player != current_player_) {
136
- throw std::logic_error{"Game::Place: move player is not the current player"};
137
- }
138
- if (board_.GetGameResult() != GameResult::kInProgress) {
139
- return MoveResult::kFinished;
140
- }
141
- switch (board_.Place(move.position, PlayerToCellState(move.player))) {
142
- case Cell::SetStateResult::kUpdated:
143
- history_.push_back(move);
144
- current_player_ = GetOpponent(current_player_);
145
- return MoveResult::kPlaced;
146
- case Cell::SetStateResult::kOccupied:
147
- return MoveResult::kOccupied;
148
- case Cell::SetStateResult::kNoInput:
149
- throw std::logic_error{"Game::Place: board rejected the move"};
150
- }
151
- throw std::logic_error{"Game::Place: unhandled Cell::SetStateResult"};
152
- }
153
- ```
154
-
155
- ## 推演一步读原数据
156
-
157
- **差**:先复制整个棋盘,再在副本上落子,最后判断成线;调用方看不出副本只是中间量,还要重复处理落子失败。
158
-
159
- ```cpp
160
- Board hypothetical = *this;
161
- const Cell::SetStateResult placed = hypothetical.Place(position, state);
162
- if (placed != Cell::SetStateResult::kUpdated) {
163
- return WinPreviewResult::kInvalidPlace;
164
- }
165
- return hypothetical.HasLine(state) ? WinPreviewResult::kWouldWin
166
- : WinPreviewResult::kWouldNotWin;
167
- ```
168
-
169
- **好**:判断「这一格换成某种棋子后会不会成线」不需要第二份棋盘。越界、空格、已占用交给 `CheckPlaceable` 统一返回原因;通过之后只在包含该坐标的线上把这一格算作 `state`,其余格子读原棋盘。
170
-
171
- ```cpp
172
- WinPreviewResult Board::PreviewWin(Position position, CellState state) const {
173
- if (CheckPlaceable(position, state) != Cell::SetStateResult::kUpdated) {
174
- return WinPreviewResult::kInvalidPlace;
175
- }
176
-
177
- const auto matches = [&](Position cell) {
178
- if (cell.row == position.row && cell.col == position.col) {
179
- return true;
180
- }
181
- return cells_.at(cell.row).at(cell.col) == state;
182
- };
183
-
184
- const bool completes = std::ranges::any_of(kLines, [&](const auto& line) {
185
- const bool contains = std::ranges::find_if(line, [&](Position cell) {
186
- return cell.row == position.row && cell.col == position.col;
187
- }) != line.end();
188
- return contains && std::ranges::all_of(line, matches);
189
- });
190
- return completes ? WinPreviewResult::kWouldWin : WinPreviewResult::kWouldNotWin;
191
- }
192
- ```
193
-
194
- 先用 `IsInRange` 拒绝越界,再用 `at()` 读取;失败路径不碰数组,`noexcept` 也就不再成立。
@@ -1,123 +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_{}`,容器与类类型成员写空 `{}`(如 `std::vector<int> values_{};`、`Board board_{};`)。
29
- - `=` 只用于花括号不可用或会改变含义的例外:引用成员不写 NSDMI,在构造函数初始化列表绑定;容器需要 `std::vector<int>(n)` 的容量构造时写 `= std::vector<int>(n)`,或在构造函数初始化列表写 `v_(n)`;`std::initializer_list` 成员避免使用,确实需要时写 `= {..}` 并保证底层数组的生命周期长于对象;花括号无法构造的类型才回退到 `= value` 或构造函数初始化列表。`=` 仍然是初始化,不是赋值。
30
- - `static` 与 `static constexpr` 数据成员不是对象成员,继续写 `= 3`;`auto` 不能做非静态数据成员。
31
- - 构造函数初始化列表按成员声明顺序执行,不按书写顺序;初始化列表顺序与声明顺序保持一致,避免依赖未初始化成员。
32
-
33
- ## `auto` 与类型推导
34
-
35
- - `auto` 适合“右侧类型显然且无关紧要”的局部变量,例如迭代器、`make_unique` 结果、范围 `for`。
36
- - 公共接口、类成员、函数返回类型写显式类型;返回值需要隐藏实现细节时才可以写 `auto`,且头文件里要能看出契约。
37
- - `auto` 会丢掉顶层 `const` 和引用:`auto value = const_ref` 得到副本;需要引用时写 `const auto&`。
38
- - 结构化绑定 `auto [key, value] : map` 中,`key` 是副本;要避免复制时写 `const auto& [key, value]`。
39
-
40
- ## `const`、`constexpr`、`consteval`、`constinit`
41
-
42
- | 关键字 | 承诺 | 典型位置 |
43
- | --- | --- | --- |
44
- | `const` | 初始化后不能通过这个名字修改 | 局部变量、参数、成员函数尾部 |
45
- | `constexpr` | 能在编译期求值(满足条件时) | 常量、函数、构造函数 |
46
- | `consteval` | 必须在编译期求值 | 编译期校验函数 |
47
- | `constinit` | 静态存储期变量在编译期完成初始化 | 全局/静态变量 |
48
- | `inline constexpr` | 头文件里可共享的编译期常量,避免 ODR 问题 | 命名空间作用域常量 |
49
-
50
- - 共享常量写在头文件的命名空间作用域:`inline constexpr int kMaxRows = 3;`,不要用 `#define`。`.cpp` 里非 `inline`、非 `extern` 的命名空间作用域 `constexpr` 已有内部链接。
51
- - `constexpr` 函数的定义要放在头文件里,否则其他翻译单元无法在编译期调用。
52
- - 不要给所有函数加 `constexpr`;只有确实需要在编译期使用、且实现满足约束时才加。
53
- - `constinit` 只保证初始化时机,不保证不可修改;需要不可修改时同时写 `const`。
54
- - C++20 起 `constexpr` 函数可以包含循环、局部变量和部分动态分配,但不要借此把运行期逻辑塞进编译期。
55
-
56
- ## 表达式与比较
57
-
58
- - 比较写成“变量在左、常量在右”:`if (id < kTaskCount)`,不写 Yoda 条件 `if (kTaskCount > id)`。编译器会在漏写 `=` 时给出警告,可读性也更好。
59
- - 混合优先级的算术表达式加括号:`(a + b) * c`,不依赖读者记忆优先级。`readability-math-missing-parentheses` 会检查。
60
- - 区间用半开区间 `[low, high)`;判空用 `empty()`,不要用 `size() == 0`。
61
- - 逻辑表达式按语义分组,一组一个括号;不要把多个否定串成一行。
62
- - 自增自减单独成句;不要在同一个表达式里对同一对象多次修改,那是未定义行为。
63
-
64
- ## 函数、参数与返回值
65
-
66
- - 命名规则以 `references/naming.md` 为准:函数 PascalCase、查询 `Is` / `Has` / `Can` / `Get` / `Find`、转换 `SourceToTarget`。
67
- - 输入参数默认按值传小对象、按 `const&` 传大对象;需要修改调用方对象时用引用参数,并在名字或注释里标 `[out]`。
68
- - 优先返回值而不是输出参数;需要返回多个值时用 `struct` 聚合,不用多个 `bool` 输出参数。
69
- - 返回容器或大对象时按值返回,依赖 RVO / 移动;不要返回局部对象的引用或指针。
70
- - 单参数构造函数加 `explicit`,除非确实需要隐式转换;转换运算符也加 `explicit`。
71
- - 不在虚函数上写默认参数;默认参数在静态类型处绑定,派生类改默认值会造成意外。
72
- - 函数只做一件事;超过一屏时先找可命名的子步骤,分阶段与注释写法见 `steps/04-implementation.md`。
73
-
74
- ## 类、结构体与特殊成员
75
-
76
- - `struct` 用于公开数据的聚合;`class` 用于维护不变量、隐藏表示。两者只差默认访问权限,按语义选。
77
- - 数据成员默认私有;需要暴露只读视图时提供 `const` 成员函数,不返回可变内部引用。
78
- - 单参数构造函数用 `explicit`;构造函数失败时抛异常,不留下半初始化对象。
79
- - 特殊成员(构造、析构、拷贝、移动)在头文件声明,在 `.cpp` 定义;只有默认实现足够且零参数时才写 `= default`。
80
- - 定义了析构函数、拷贝构造或拷贝赋值之一时,认真考虑其余特殊成员;用 `= delete` 明确禁止不需要的操作。
81
- - 多态基类析构函数必须是 `virtual` 或 `protected`;否则通过基类指针删除派生对象是未定义行为。
82
- - 不在构造函数里调用虚函数;此时派生部分还没构造完。
83
-
84
- ## RAII、所有权与移动
85
-
86
- - 资源获取即初始化:用对象生存期管理内存、文件、锁、套接字;析构函数负责释放。
87
- - 默认用 `std::unique_ptr<T>` 表达独占所有权;需要共享所有权时才用 `std::shared_ptr<T>`,并用 `std::weak_ptr` 打破循环。
88
- - 不写裸 `new` / `delete`;需要动态数组时用 `std::vector`,需要单个对象时用 `std::make_unique`。
89
- - 移动是“把资源指针转走并置空源对象”的转换;`std::move` 只是 `static_cast<T&&>`,本身不移动任何东西。
90
- - 移动后的对象处于有效但未指定状态;只能重新赋值或销毁,不要读取它的值。
91
- - 移动构造函数和移动赋值标记 `noexcept`,否则 `std::vector` 等容器在扩容时可能选择拷贝。
92
- - 返回值直接 `return value;`,不要 `return std::move(value);`;后者阻止 RVO。
93
-
94
- ## 模板、概念与 CTAD
95
-
96
- - 模板定义放头文件;`.cpp` 里的模板实例化只对当前翻译单元可见。
97
- - C++20 用概念约束模板参数,把错误从实例化深处提前到调用点:
98
-
99
- ```cpp
100
- template <typename T>
101
- concept Numeric = std::is_arithmetic_v<T>;
102
-
103
- template <Numeric T>
104
- T Clamp(T value, T low, T high) {
105
- if (value < low) return low;
106
- if (value > high) return high;
107
- return value;
108
- }
109
- ```
110
-
111
- - 概念只表达语法和语义约束,不替代文档;错误消息要能让调用者知道违反了哪条约束。
112
- - CTAD(类模板实参推导)用于局部变量和明显场景:`std::pair pair{1, 2.0};`;公共接口写显式模板实参,避免推导规则变化影响契约。
113
- - 不用模板做运行期分支;需要运行期多态时用虚函数或 `std::variant`。
114
-
115
- ## 检查清单
116
-
117
- - [ ] 初始化方式统一,窄化转换被编译器拦住。
118
- - [ ] `auto` 只出现在类型显然的局部变量;公共接口有显式类型。
119
- - [ ] 共享常量用 `inline constexpr`,没有宏常量。
120
- - [ ] 比较不是 Yoda 形式,混合优先级有括号。
121
- - [ ] 资源由 RAII 管理,没有裸 `new` / `delete`。
122
- - [ ] 特殊成员声明与定义位置正确,移动操作 `noexcept`。
123
- - [ ] 模板约束用概念表达,定义在头文件。
@@ -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
- - [ ] 没有用未定义行为换速度。