@chengzhao-dev/dsh-cpp-workflow 0.6.19 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +11 -26
- package/README.md +13 -28
- package/cordis.patch.yml +1 -1
- package/lib/client.js +33 -21
- package/lib/comment-policy.js +15 -6
- package/lib/index.js +1 -1
- package/lib/workspace.js +0 -15
- package/package.json +7 -7
- package/skills/building-cpp/SKILL.md +34 -21
- package/skills/building-cpp/assets/templates/projects/common/running-build.sh +5 -1
- package/skills/building-cpp/project/vscode/01-extensions.md +28 -0
- package/skills/building-cpp/project/vscode/02-settings.md +39 -0
- package/skills/building-cpp/references/build-script.md +14 -7
- package/skills/building-cpp/references/environment.md +8 -1
- package/skills/building-cpp/steps/01-build.md +8 -8
- package/skills/building-cpp/steps/02-tooling.md +17 -9
- package/skills/building-cpp/steps/03-check.md +16 -14
- package/skills/developing-cpp/SKILL.md +37 -25
- package/skills/developing-cpp/references/comments/comment-rules.md +181 -0
- package/skills/developing-cpp/references/comments/comment-style.md +80 -0
- package/skills/developing-cpp/references/comments/examples/declaration-class.md +66 -0
- package/skills/developing-cpp/references/comments/examples/declaration-constructor.md +22 -0
- package/skills/developing-cpp/references/comments/examples/declaration-enum.md +48 -0
- package/skills/developing-cpp/references/comments/examples/declaration-file.md +66 -0
- package/skills/developing-cpp/references/comments/examples/declaration-function.md +127 -0
- package/skills/developing-cpp/references/comments/examples/declaration-member.md +23 -0
- package/skills/developing-cpp/references/comments/examples/declaration-predicate.md +14 -0
- package/skills/developing-cpp/references/comments/examples/declaration-struct.md +18 -0
- package/skills/developing-cpp/references/comments/examples/implementation-body.md +64 -0
- package/skills/developing-cpp/references/comments/examples/implementation-todo.md +16 -0
- package/skills/developing-cpp/references/comments/glossary/comment-terms.md +93 -0
- package/skills/developing-cpp/references/design/examples/behavior-sentence.md +14 -0
- package/skills/developing-cpp/references/design/examples/coordinate-table.md +31 -0
- package/skills/developing-cpp/references/design/examples/header-place.md +26 -0
- package/skills/developing-cpp/references/design/examples/library-boundary.md +30 -0
- package/skills/developing-cpp/references/design/examples/library-split.md +7 -0
- package/skills/developing-cpp/references/design/examples/move-result.md +39 -0
- package/skills/developing-cpp/references/design/examples/preview-read.md +40 -0
- package/skills/developing-cpp/references/{project-design.md → design/project-design.md} +57 -5
- package/skills/developing-cpp/references/{change-model.md → editing/01-edit-operations.md} +5 -3
- package/skills/developing-cpp/references/{priority.md → editing/02-rule-priority.md} +2 -2
- package/skills/developing-cpp/references/exceptions/examples/throw-boundary.md +28 -0
- package/skills/developing-cpp/references/exceptions/examples/throw-build-mode.md +38 -0
- package/skills/developing-cpp/references/exceptions/examples/throw-catch.md +21 -0
- package/skills/developing-cpp/references/exceptions/examples/throw-destructor.md +21 -0
- package/skills/developing-cpp/references/exceptions/examples/throw-format.md +13 -0
- package/skills/developing-cpp/references/exceptions/examples/throw-message.md +16 -0
- package/skills/developing-cpp/references/exceptions/examples/throw-when.md +27 -0
- package/skills/developing-cpp/references/{exceptions.md → exceptions/throw-rules.md} +78 -23
- package/skills/developing-cpp/references/{glossary.md → language/glossary/language-terms.md} +2 -2
- package/skills/developing-cpp/references/language/language-basics/01-objects.md +15 -0
- package/skills/developing-cpp/references/language/language-basics/02-initialization.md +26 -0
- package/skills/developing-cpp/references/language/language-basics/03-deduction.md +12 -0
- package/skills/developing-cpp/references/language/language-basics/04-const.md +25 -0
- package/skills/developing-cpp/references/language/language-basics/05-expressions.md +23 -0
- package/skills/developing-cpp/references/language/language-basics/06-functions.md +15 -0
- package/skills/developing-cpp/references/language/language-basics/07-classes.md +15 -0
- package/skills/developing-cpp/references/language/language-basics/08-raii.md +15 -0
- package/skills/developing-cpp/references/language/language-basics/09-templates.md +26 -0
- package/skills/developing-cpp/references/language/language-basics/10-checklist.md +10 -0
- package/skills/developing-cpp/references/language/performance/01-measure.md +22 -0
- package/skills/developing-cpp/references/language/performance/02-complexity.md +13 -0
- package/skills/developing-cpp/references/language/performance/03-memory.md +14 -0
- package/skills/developing-cpp/references/language/performance/04-undefined-behavior.md +28 -0
- package/skills/developing-cpp/references/language/performance/05-pitfalls.md +17 -0
- package/skills/developing-cpp/references/language/performance/06-false-optimizations.md +13 -0
- package/skills/developing-cpp/references/language/performance/07-checklist.md +8 -0
- package/skills/developing-cpp/references/language/standard-library/01-containers.md +29 -0
- package/skills/developing-cpp/references/language/standard-library/02-access.md +14 -0
- package/skills/developing-cpp/references/language/standard-library/03-iteration.md +18 -0
- package/skills/developing-cpp/references/language/standard-library/04-algorithms.md +26 -0
- package/skills/developing-cpp/references/language/standard-library/05-string.md +13 -0
- package/skills/developing-cpp/references/language/standard-library/06-format.md +106 -0
- package/skills/developing-cpp/references/language/standard-library/07-time.md +13 -0
- package/skills/developing-cpp/references/language/standard-library/08-random.md +13 -0
- package/skills/developing-cpp/references/language/standard-library/09-files.md +14 -0
- package/skills/developing-cpp/references/language/standard-library/10-checklist.md +9 -0
- package/skills/developing-cpp/references/language/value-types/01-width.md +17 -0
- package/skills/developing-cpp/references/language/value-types/02-signed.md +26 -0
- package/skills/developing-cpp/references/language/value-types/03-integer-roles.md +38 -0
- package/skills/developing-cpp/references/language/value-types/04-enum.md +24 -0
- package/skills/developing-cpp/references/language/value-types/05-strong-type.md +17 -0
- package/skills/developing-cpp/references/language/value-types/06-const-method.md +22 -0
- package/skills/developing-cpp/references/language/value-types/07-nodiscard.md +15 -0
- package/skills/developing-cpp/references/language/value-types/08-comparison.md +10 -0
- package/skills/developing-cpp/references/language/value-types/09-subscript.md +15 -0
- package/skills/developing-cpp/references/language/value-types/10-float-bool.md +13 -0
- package/skills/developing-cpp/references/language/value-types/11-checklist.md +8 -0
- package/skills/developing-cpp/references/{layout.md → layout/01-source-layout.md} +10 -4
- package/skills/developing-cpp/references/{file-template.md → layout/02-file-template.md} +10 -4
- package/skills/developing-cpp/references/layout/03-declaration-order.md +138 -0
- package/skills/developing-cpp/references/naming/examples/naming-accessor.md +17 -0
- package/skills/developing-cpp/references/naming/examples/naming-basic.md +3 -0
- package/skills/developing-cpp/references/naming/examples/naming-current-prefix.md +19 -0
- package/skills/developing-cpp/references/naming/examples/naming-lookup.md +18 -0
- package/skills/developing-cpp/references/naming/examples/naming-lower-layer.md +39 -0
- package/skills/developing-cpp/references/naming/examples/naming-multiple-return.md +20 -0
- package/skills/developing-cpp/references/naming/examples/naming-player-value.md +16 -0
- package/skills/developing-cpp/references/naming/examples/naming-predicate.md +18 -0
- package/skills/developing-cpp/references/naming/examples/naming-rename.md +5 -0
- package/skills/developing-cpp/references/naming/examples/naming-responsibility.md +19 -0
- package/skills/developing-cpp/references/naming/examples/naming-role.md +21 -0
- package/skills/developing-cpp/references/naming/examples/naming-state-change.md +57 -0
- package/skills/developing-cpp/references/{naming.md → naming/identifier-names.md} +37 -9
- package/skills/developing-cpp/references/{google-cpp-style.md → style/google-cpp-style.md} +11 -10
- package/skills/developing-cpp/scripts/checking-comments.mjs +71 -14
- package/skills/developing-cpp/scripts/checking-declaration-order.mjs +357 -0
- package/skills/developing-cpp/steps/01-behavior.md +5 -5
- package/skills/developing-cpp/steps/02-boundary.md +10 -10
- package/skills/developing-cpp/steps/03-declaration.md +16 -14
- package/skills/developing-cpp/steps/04-implementation.md +21 -17
- package/skills/engineering/SKILL.md +87 -0
- package/skills/{configuring-cmake/assets/cmake → engineering/references/cmake/assets}/CompileSettings.cmake +2 -0
- package/skills/{building-cpp/references/linking.md → engineering/references/cmake/linking/01-libraries.md} +7 -48
- package/skills/engineering/references/cmake/linking/02-rpath.md +25 -0
- package/skills/engineering/references/cmake/linking/03-undefined-reference.md +20 -0
- package/skills/engineering/references/cmake/linking/04-inspect.md +21 -0
- package/skills/{configuring-cmake/references/modules.md → engineering/references/cmake/modules/functions.md} +8 -5
- package/skills/{configuring-cmake/references → engineering/references/cmake/standard}/cxx-standard.md +5 -4
- package/skills/engineering/references/cmake/standard/examples/standard.md +19 -0
- package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/README.md +9 -9
- package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++20/README.md +1 -1
- package/skills/engineering/references/cmake/targets/examples/glob.md +22 -0
- package/skills/engineering/references/cmake/targets/examples/rpath.md +24 -0
- package/skills/{configuring-cmake/examples/cmake.md → engineering/references/cmake/targets/examples/target.md} +2 -70
- package/skills/{configuring-cmake/references/cmake.md → engineering/references/cmake/targets/rules.md} +18 -11
- package/skills/{building-cpp/references/testing.md → engineering/references/cmake/testing/ctest.md} +11 -4
- package/skills/{building-cpp/references/encoding.md → engineering/references/formatting/01-encoding.md} +1 -1
- package/skills/engineering/references/formatting/02-clang-format.md +20 -0
- package/skills/engineering/references/formatting/03-clang-tidy.md +58 -0
- package/skills/engineering/references/formatting/04-clangd.md +90 -0
- package/skills/engineering/references/formatting/examples/clang-format.md +38 -0
- package/skills/engineering/references/formatting/examples/clang-tidy.md +43 -0
- package/skills/{building-cpp/examples/tooling.md → engineering/references/formatting/examples/clangd.md} +1 -80
- package/skills/{configuring-cmake → engineering}/steps/01-targets.md +6 -6
- package/skills/{configuring-cmake → engineering}/steps/02-modules.md +4 -4
- package/skills/{configuring-cmake → engineering}/steps/03-check.md +3 -2
- package/templates/cpp-workflow-project/SKILL.md +1 -1
- package/skills/building-cpp/references/static-analysis.md +0 -148
- package/skills/configuring-cmake/SKILL.md +0 -68
- package/skills/developing-cpp/examples/comments-declaration.md +0 -313
- package/skills/developing-cpp/examples/comments-implementation.md +0 -66
- package/skills/developing-cpp/examples/exceptions.md +0 -173
- package/skills/developing-cpp/examples/naming.md +0 -168
- package/skills/developing-cpp/examples/structure.md +0 -194
- package/skills/developing-cpp/references/comment-terms.md +0 -75
- package/skills/developing-cpp/references/comments.md +0 -223
- package/skills/developing-cpp/references/declaration-order.md +0 -118
- package/skills/developing-cpp/references/language.md +0 -123
- package/skills/developing-cpp/references/performance.md +0 -83
- package/skills/developing-cpp/references/standard-library.md +0 -138
- package/skills/developing-cpp/references/types.md +0 -154
- /package/skills/{configuring-cmake/assets/cmake → engineering/references/cmake/assets}/LinkSettings.cmake +0 -0
- /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++11/README.md +0 -0
- /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++11/cmake-snippet.cmake +0 -0
- /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++14/README.md +0 -0
- /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++14/cmake-snippet.cmake +0 -0
- /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++17/README.md +0 -0
- /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++17/cmake-snippet.cmake +0 -0
- /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++20/cmake-snippet.cmake +0 -0
- /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++23/README.md +0 -0
- /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++23/cmake-snippet.cmake +0 -0
- /package/skills/{building-cpp → engineering/references/formatting}/scripts/checking-encoding.mjs +0 -0
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# 声明顺序
|
|
2
|
+
|
|
3
|
+
顺序的作用是让读者先看类型和契约,再看数据。`clang-format` 不管顺序,靠人工和 review 维持。
|
|
4
|
+
|
|
5
|
+
## 类声明
|
|
6
|
+
|
|
7
|
+
```cpp
|
|
8
|
+
class TaskTable {
|
|
9
|
+
public:
|
|
10
|
+
// 1. 类型与别名
|
|
11
|
+
using Tasks = std::array<TaskState, 16>;
|
|
12
|
+
|
|
13
|
+
// 2. 静态常量
|
|
14
|
+
static constexpr int kTaskCount = 16;
|
|
15
|
+
|
|
16
|
+
// 3. 工厂
|
|
17
|
+
static TaskTable CreateEmpty();
|
|
18
|
+
|
|
19
|
+
// 4. 构造、赋值、析构
|
|
20
|
+
TaskTable();
|
|
21
|
+
TaskTable(const TaskTable&) = default;
|
|
22
|
+
TaskTable& operator=(const TaskTable&) = default;
|
|
23
|
+
~TaskTable();
|
|
24
|
+
|
|
25
|
+
// 5. 其他成员函数
|
|
26
|
+
[[nodiscard]] bool Contains(int id) const;
|
|
27
|
+
[[nodiscard]] TaskState GetTaskState(int id) const;
|
|
28
|
+
void SetTaskState(int id, TaskState state);
|
|
29
|
+
|
|
30
|
+
// 6. 数据成员
|
|
31
|
+
private:
|
|
32
|
+
Tasks tasks_{};
|
|
33
|
+
bool is_open_ = false;
|
|
34
|
+
};
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
- `struct` 的数据成员放最前;有 invariant 时改用 `class`。
|
|
38
|
+
- `public:` 在前,`private:` 在后;`private:` 前空一行,数据成员之间不空行。
|
|
39
|
+
- 静态常量用 `static constexpr`;跨文件共享的常量放命名空间并 `inline constexpr`。
|
|
40
|
+
- 工厂放在构造函数之前;返回 `TaskTable` 的工厂用 `Create` 开头,名字说明创建的是什么状态。
|
|
41
|
+
- 用户声明的特殊成员函数集中放第 4 组;`= default` 也写出来,读者才知道是故意默认。
|
|
42
|
+
- 不要把数据成员插在函数之间;数据成员永远在最后一组。
|
|
43
|
+
|
|
44
|
+
### 数据成员之间
|
|
45
|
+
|
|
46
|
+
- 数据成员按初始化依赖、逻辑分组和阅读顺序排列,不按字母序。初始化依赖是硬约束:先声明的成员先初始化,初始化器只能安全使用已经完成初始化的成员。
|
|
47
|
+
- 聚合类型的字段顺序参与聚合初始化与指定初始化器,也是标准布局的一部分;重排要同步所有 `T{a, b}` 调用点,并检查序列化或 ABI 假设。
|
|
48
|
+
- 先主值、后限定值,同类字段放一起。例如落子记录先放位置、再放玩家,读作「在哪、谁」。
|
|
49
|
+
- Google 的 Declaration Order 与 C++ Core Guidelines 的 NL.16 只规定访问级别和组间顺序,都没有字母序要求;沿用现有语义顺序即可。
|
|
50
|
+
- 团队若另定字母序,只作为语义相同时的最后兜底并写进项目规则,不和语义顺序混用。
|
|
51
|
+
|
|
52
|
+
### 其他函数的组内次序
|
|
53
|
+
|
|
54
|
+
Google、LLVM、OpenCV 都没有规定这一组内部的次序;本工作流补上:
|
|
55
|
+
|
|
56
|
+
- 有比较运算符时,开头是 `operator==`:成员默认比较和 `friend` 都放这里,然后才是谓词 `Is` / `Has` / `Can`、`Get`、普通函数按使用先后。`friend` 不再放到这一组末尾。
|
|
57
|
+
- 多个直接返回成员的 `Get` 按对应数据成员的声明顺序排列,不按调用频率或字母序:成员依次是 `current_player_`、`board_`、`history_`,getter 就依次是 `GetCurrentPlayer()`、`GetBoard()`、`GetHistory()`;成员顺序调整时 getter 一起改,一个类里只留一套顺序。
|
|
58
|
+
- 直接返回成员的 `Get` 按成员顺序;没有独立成员的派生查询排在直接 `Get` 之后,按「先局部元素、后整体状态」阅读,不插进直接 `Get` 之间。例:`Board` 只有 `squares_`,`GetSquare()` 直接取一格,先;`GetStatus()` 扫描全部格子才得到状态,后。
|
|
59
|
+
- 成对的局部/整体操作沿用同一方向:先 `ClearSquare(pos)` 清一格,再 `Clear()` 清整盘;局部操作在前,整体操作在后,不按调用频率互换。
|
|
60
|
+
- 比较运算符的签名与 `[[nodiscard]]` 规则见 `references/language/value-types/`,这里不另写一套。
|
|
61
|
+
- 同一类型上的谓词留在类内,不因为实现只转调公开函数就拆成类外自由函数。
|
|
62
|
+
- `friend` 类型仍跟其他类型放在一起,和 `friend` 函数不是同一组。
|
|
63
|
+
|
|
64
|
+
## 命名空间作用域的头文件
|
|
65
|
+
|
|
66
|
+
文件槽位与守卫名以 `references/layout/02-file-template.md` 为准;本节只看命名空间作用域内的排列顺序。
|
|
67
|
+
|
|
68
|
+
```cpp
|
|
69
|
+
#ifndef TASKFLOW_INCLUDE_TASK_TABLE_H_
|
|
70
|
+
#define TASKFLOW_INCLUDE_TASK_TABLE_H_
|
|
71
|
+
|
|
72
|
+
#include <array>
|
|
73
|
+
#include <cstdint>
|
|
74
|
+
#include <string>
|
|
75
|
+
|
|
76
|
+
namespace taskflow {
|
|
77
|
+
|
|
78
|
+
inline constexpr int kTaskCount = 16;
|
|
79
|
+
|
|
80
|
+
enum class TaskState : std::uint8_t { kPending, kActive, kDone };
|
|
81
|
+
|
|
82
|
+
struct TaskRecord {
|
|
83
|
+
int id = 0;
|
|
84
|
+
TaskState state = TaskState::kPending;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
[[nodiscard]] bool Contains(int id);
|
|
88
|
+
[[nodiscard]] std::string TaskStateToString(TaskState state);
|
|
89
|
+
|
|
90
|
+
class TaskTable {
|
|
91
|
+
public:
|
|
92
|
+
// 类内仍按上面的六组顺序
|
|
93
|
+
private:
|
|
94
|
+
std::array<TaskState, kTaskCount> tasks_{};
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
} // namespace taskflow
|
|
98
|
+
|
|
99
|
+
#endif // TASKFLOW_INCLUDE_TASK_TABLE_H_
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
顺序:包含 → 命名空间 → `inline constexpr` 常量 → `enum class` → `struct`(被依赖的在前)→ 自由函数 → 类。类的公共接口类型放在类内最前。
|
|
103
|
+
|
|
104
|
+
## `.cpp` 的定义顺序
|
|
105
|
+
|
|
106
|
+
- 本工作流约定:与头文件声明同序,先类型和常量,再自由函数,再成员函数;成员函数按类内声明顺序。Google、LLVM 与 C++ Core Guidelines 都没有规定 `.cpp` 成员定义的次序;这里选同序,是为了让读者能在头文件与实现之间逐项对照。
|
|
107
|
+
- 文件头注释之后先放本文件的 `namespace taskflow {`,再放公共定义;常量仍排在具名命名空间内、成员函数之前;内部链接的写法按 `references/style/google-cpp-style.md`,本文不复述。
|
|
108
|
+
- 构造函数、析构函数、拷贝/移动定义通常放在成员函数组最前;如果头文件把某个成员声明在构造函数之前,仍按头文件声明顺序,不把构造函数硬提到前面。`= default` 的定义放 `.cpp` 时紧跟声明顺序。
|
|
109
|
+
- 不要为了「先编译」重排定义;C++ 不要求定义顺序,读者顺序优先。
|
|
110
|
+
|
|
111
|
+
## 空行
|
|
112
|
+
|
|
113
|
+
| 位置 | 空行 |
|
|
114
|
+
| --- | --- |
|
|
115
|
+
| 类内六个组之间 | 1 行 |
|
|
116
|
+
| 同组函数之间 | 1 行 |
|
|
117
|
+
| 数据成员之间 | 0 行 |
|
|
118
|
+
| `private:` 之前 | 1 行 |
|
|
119
|
+
| 命名空间内顶层实体之间 | 1 行 |
|
|
120
|
+
| 函数体内逻辑组之间 | 1 行,最多 1 行 |
|
|
121
|
+
|
|
122
|
+
连续两个空行视为格式缺陷;`clang-format` 的 `MaxEmptyLinesToKeep: 1` 会压掉,但不要依赖它。
|
|
123
|
+
|
|
124
|
+
### 函数体内的空行
|
|
125
|
+
|
|
126
|
+
- 连续的守卫检查连成一组:都以 `return` / `throw` 结束或都是前置判断时,组内不空行;守卫组与后续处理之间空 1 行。
|
|
127
|
+
- 取回一个结果并按结果分支的语句连成一组(声明、`if`、`throw` / `return`),组内不空行;这一组与收尾(记录这一步、轮到对方、返回成功)之间空 1 行。
|
|
128
|
+
- 同一组语句不拆:三个校验之后的赋值与 `return` 之间不空行。
|
|
129
|
+
- 只有次序依赖的一对 `if`(`GetWinner`、`JudgeAfterPlace`)之间空 1 行。
|
|
130
|
+
- 最多 1 行,不连续空两行。
|
|
131
|
+
|
|
132
|
+
## 依据
|
|
133
|
+
|
|
134
|
+
- [Google C++ Style Guide: Declaration Order](https://google.github.io/styleguide/cppguide.html#Declaration_Order):类内先 public、再 protected、最后 private;同节内类型、常量、函数、数据成员的先后。
|
|
135
|
+
- [Google C++ Style Guide: Vertical Whitespace](https://google.github.io/styleguide/cppguide.html#Vertical_Whitespace):垂直留白越少越好;函数体首尾不留空行,函数体内不随意加空行;空行主要留给多重 `if-else` 这类分支块。
|
|
136
|
+
- [C++ Core Guidelines: NL.16](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#rl-order):类型、构造/赋值/析构、函数、数据成员的传统次序。
|
|
137
|
+
- [cppreference: 构造函数](https://en.cppreference.com/w/cpp/language/constructor):非静态数据成员按声明顺序初始化,与 mem-initializer 列表顺序无关。
|
|
138
|
+
- [LLVM Coding Standards](https://llvm.org/docs/CodingStandards.html):只要求每个 `.cpp` 先包含定义其接口的头文件,没有规定成员定义的先后;上一条「与头文件声明同序」因此是本工作流约定,不冒充外部标准。
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# 正反例:访问器
|
|
2
|
+
|
|
3
|
+
**差**:
|
|
4
|
+
|
|
5
|
+
```cpp
|
|
6
|
+
Order current_order() const; // 成员名直接当函数名
|
|
7
|
+
void set_current_order(Order order);
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
**好**:
|
|
11
|
+
|
|
12
|
+
```cpp
|
|
13
|
+
[[nodiscard]] Order GetCurrentOrder() const;
|
|
14
|
+
void AdvanceOrder(); // 领域动作,不是 setter
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
查询统一 `Get` + PascalCase;修改用领域动词。只有真正需要替换值时,才考虑 `Set` 形式,并说明为什么不是动作。
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# 正反例:查找与读取
|
|
2
|
+
|
|
3
|
+
**差**:
|
|
4
|
+
|
|
5
|
+
```cpp
|
|
6
|
+
[[nodiscard]] Task GetTask(std::string_view name) const; // 可能找不到时返回什么?
|
|
7
|
+
[[nodiscard]] bool FindTask(std::string_view name) const; // 返回 bool 却叫 Find
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
**好**:
|
|
11
|
+
|
|
12
|
+
```cpp
|
|
13
|
+
[[nodiscard]] const Task& GetTask(std::string_view name) const; // 找不到抛 TaskNotFoundError
|
|
14
|
+
[[nodiscard]] const Task* FindTask(std::string_view name) const; // 找不到返回 nullptr
|
|
15
|
+
[[nodiscard]] bool HasTask(std::string_view name) const; // 只回答存在性
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
`Get` 表示结果一定存在(否则抛出声明过的领域异常),`Find` 表示可能找不到,`Has` / `Contains` 只回答是否存在。不要用 `Find` 返回 `bool`。
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# 正反例:下层类型不要借用上层词汇
|
|
2
|
+
|
|
3
|
+
**差**:`Board` 定义并返回 `GameResult`。
|
|
4
|
+
|
|
5
|
+
```cpp
|
|
6
|
+
// board.h:棋盘定义了对局结果
|
|
7
|
+
enum class GameResult : std::uint8_t {
|
|
8
|
+
kInProgress = 0,
|
|
9
|
+
kXWon = 1,
|
|
10
|
+
kOWon = 2,
|
|
11
|
+
kDrawn = 3,
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
class Board {
|
|
15
|
+
public:
|
|
16
|
+
[[nodiscard]] GameResult GetGameResult() const;
|
|
17
|
+
};
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
**为什么错**:`Game` 组合 `Board`,调用方向是 `Game -> Board`;`Board` 不知道「一局对局」的生命周期,却用 `GameResult` 命名自己的返回值。读者会以为棋盘负责对局状态,`Game` 里再出现同名概念时也无法区分。**下层类型不出现上层类型名。**
|
|
21
|
+
|
|
22
|
+
**好**:
|
|
23
|
+
|
|
24
|
+
```cpp
|
|
25
|
+
// board.h:棋盘只回答棋盘自己的状态
|
|
26
|
+
enum class BoardStatus : std::uint8_t {
|
|
27
|
+
kInProgress = 0,
|
|
28
|
+
kXWon = 1,
|
|
29
|
+
kOWon = 2,
|
|
30
|
+
kDrawn = 3,
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
class Board {
|
|
34
|
+
public:
|
|
35
|
+
[[nodiscard]] BoardStatus GetStatus() const;
|
|
36
|
+
};
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
`Board` 的返回值用 `BoardStatus`;`Game` 需要「对局结果」时在 `Game` 层组合或另起名字,不把 `Game` 的词汇塞进 `Board`。`GameResult`、`GameState`、`PlayerMove` 这类「上层前缀 + 下层类型」都按反例处理。
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# 正反例:多返回值
|
|
2
|
+
|
|
3
|
+
**差**:
|
|
4
|
+
|
|
5
|
+
```cpp
|
|
6
|
+
bool Parse(std::string_view text, int* out_value); // 出参 + 返回 bool
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
**好**:
|
|
10
|
+
|
|
11
|
+
```cpp
|
|
12
|
+
struct ParseResult {
|
|
13
|
+
bool ok{false};
|
|
14
|
+
int value{0};
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
[[nodiscard]] ParseResult TryParse(std::string_view text);
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
需要同时表达成功与结果时,用带字段的结构体或 `std::optional`,不要用输出参数加布尔返回。
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# 正反例:玩家与格子取值
|
|
2
|
+
|
|
3
|
+
**差**:
|
|
4
|
+
|
|
5
|
+
```cpp
|
|
6
|
+
Player OtherPlayer(Player player); // 另一端是谁不明显
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
**好**:
|
|
10
|
+
|
|
11
|
+
```cpp
|
|
12
|
+
[[nodiscard]] constexpr Player GetOpponent(Player player) noexcept;
|
|
13
|
+
[[nodiscard]] constexpr SquareValue PlayerToSquareValue(Player player) noexcept;
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
玩家互换用 `GetOpponent`;玩家换成格子取值用 `PlayerToSquareValue`。查询名与返回类型一致:返回 `Player` 写 `GetCurrentPlayer`,返回 `SquareValue` 写 `GetCurrentValue`。
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# 正反例:谓词与 Lambda
|
|
2
|
+
|
|
3
|
+
**差**:
|
|
4
|
+
|
|
5
|
+
```cpp
|
|
6
|
+
bool valid(int value);
|
|
7
|
+
auto check = [](const Task& task) { return task.done; };
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
**好**:
|
|
11
|
+
|
|
12
|
+
```cpp
|
|
13
|
+
[[nodiscard]] bool IsValid(int value);
|
|
14
|
+
auto is_done = [](const Task& task) { return task.done; };
|
|
15
|
+
auto has_owner = [](const Task& task) { return !task.owner.empty(); };
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
返回 `bool` 的自由函数用 `Is` / `Has` / `Can` / `Should` 开头;Lambda 用 snake_case 的 `is_` / `has_` / `can_`,读起来像条件。
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# 正反例:名字与职责
|
|
2
|
+
|
|
3
|
+
**差**:
|
|
4
|
+
|
|
5
|
+
```cpp
|
|
6
|
+
class DataManager {}; // 管什么?
|
|
7
|
+
class StringHelper {}; // 为什么是类?
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
**好**:
|
|
11
|
+
|
|
12
|
+
```cpp
|
|
13
|
+
class Warehouse {}; // 持有库存,维持不变量
|
|
14
|
+
namespace string_utils { // 一组自由函数
|
|
15
|
+
[[nodiscard]] std::string Trim(std::string_view text);
|
|
16
|
+
}
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
避免 `Manager`、`Helper`、`Util`;名字要能回答「它负责什么」。一组无状态函数用命名空间,不要造类。
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# 正反例:语义角色
|
|
2
|
+
|
|
3
|
+
**差**:
|
|
4
|
+
|
|
5
|
+
```cpp
|
|
6
|
+
bool Check(int id) const; // Check 什么?
|
|
7
|
+
Result DoSomething(Task task); // 做什么?
|
|
8
|
+
TaskState To(TaskState state); // 从什么到什么?
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
**好**:
|
|
12
|
+
|
|
13
|
+
```cpp
|
|
14
|
+
[[nodiscard]] bool Contains(int id) const;
|
|
15
|
+
[[nodiscard]] ParseResult TryParse(std::string_view text);
|
|
16
|
+
[[nodiscard]] TaskState StatusCodeToTaskState(int code);
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
- 判断用 `Is` / `Has`。
|
|
20
|
+
- 可能失败但不抛用 `Try`。
|
|
21
|
+
- 映射名字含两端角色。
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# 正反例:状态变更:动作与结果枚举
|
|
2
|
+
|
|
3
|
+
**差**:
|
|
4
|
+
|
|
5
|
+
```cpp
|
|
6
|
+
enum class SetStateResult : std::uint8_t {
|
|
7
|
+
kUpdated = 0,
|
|
8
|
+
kEmptyInput = 1,
|
|
9
|
+
kOccupied = 2,
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
class Square {
|
|
13
|
+
public:
|
|
14
|
+
SetStateResult SetState(SquareValue state);
|
|
15
|
+
void ResetState();
|
|
16
|
+
};
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
**好**:
|
|
20
|
+
|
|
21
|
+
```cpp
|
|
22
|
+
enum class PlaceResult : std::uint8_t {
|
|
23
|
+
kSuccess = 0,
|
|
24
|
+
kIgnored = 1,
|
|
25
|
+
kOccupied = 2,
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
class Square {
|
|
29
|
+
public:
|
|
30
|
+
[[nodiscard]] bool IsEmpty() const;
|
|
31
|
+
[[nodiscard]] SquareValue GetValue() const;
|
|
32
|
+
[[nodiscard]] PlaceResult Place(SquareValue value);
|
|
33
|
+
void Clear();
|
|
34
|
+
};
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
`SetState` / `SetStateResult` 把存储动作写进 API 和结果名;领域动作是 `Place`,一次写入的结果叫 `PlaceResult`,取值写结果类别(成功、失败)。`Clear` 比 `ResetState` 短,且和 `Place` 成对。
|
|
38
|
+
|
|
39
|
+
改名按这个顺序做,每一步都能编译:
|
|
40
|
+
|
|
41
|
+
| 旧名 | 新名 | 理由 |
|
|
42
|
+
| --- | --- | --- |
|
|
43
|
+
| `SetStateResult` | `PlaceResult` | 结果对象 + Result,和 `Place` 放在一起 |
|
|
44
|
+
| `SetState` | `Place` | 领域动作,不暴露「设状态」 |
|
|
45
|
+
| `ResetState` | `Clear` | 领域动作,和 `Place` 成对 |
|
|
46
|
+
| `kEmptyInput` | `kIgnored` | 失败值写原因,和 `kOccupied`/`kSuccess` 同一句式 |
|
|
47
|
+
|
|
48
|
+
更早的 `kNoInput` 只说明「没有输入」,`kEmptyInput` 进一步说明是空输入;两者都在描述输入,而不是结果,最终改成 `kIgnored`。
|
|
49
|
+
|
|
50
|
+
注释也要跟着改名,但不要把「不为空」当状态:
|
|
51
|
+
|
|
52
|
+
```cpp
|
|
53
|
+
///< 失败:格子不为空 // 差
|
|
54
|
+
///< 失败:传入的是 kX 或 kO,格子已有值,没有写入 // 好
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
`kOccupied` 要从传入的值起句:先写「传入的是 kX 或 kO」,再写「格子已有值」,末尾用「没有写入」说明这次调用没有改变格子;这比只写「格子不为空」既能核对原因,也交代了结果。`kIgnored` 写「传入的是 kEmpty,没有值可写」,`kSuccess` 写「传入的是 kX 或 kO,值已写入格子」,都不重复类型注释已经交代的失败后果。
|
|
@@ -14,14 +14,26 @@
|
|
|
14
14
|
| 常量 / 枚举子 | `k` + PascalCase | `kTaskCount`, `Order::kDraft` | `TASK_COUNT`, `Draft` |
|
|
15
15
|
| 命名空间 | snake_case | `taskflow`, `warehouse` | `Taskflow`, `Warehouse` |
|
|
16
16
|
| 宏 | 全大写 + 项目前缀,尽量不用 | `CPP_WORKFLOW_ASSERT` | `ASSERT` |
|
|
17
|
+
| 模板类型参数 | PascalCase | `T`, `Item` | `t`, `item_t` |
|
|
18
|
+
| 非类型模板参数 | 按常量或变量 | `kMaxItems`, `count` | `N`, `MAX` |
|
|
19
|
+
| 概念 | PascalCase | `Sortable`, `Hashable` | `sortable`, `is_hashable` |
|
|
17
20
|
|
|
18
|
-
- 文件名用 snake_case:`order_result.h`、`task_table.cpp`;测试文件 `task_table_test.cpp`。
|
|
19
|
-
-
|
|
20
|
-
- 玩家互换用 `GetOpponent`,不写 `OtherPlayer`;玩家换成格子取值用 `
|
|
21
|
+
- 文件名用 snake_case:`order_result.h`、`task_table.cpp`;测试文件 `task_table_test.cpp`。Google 也允许 `-`,本项目统一 `_`,与 target 和测试名保持一致。
|
|
22
|
+
- 查询名与返回类型一致:返回格子取值时写 `GetCurrentValue`,返回玩家时写 `GetCurrentPlayer`;不写项目里没有对应类型的 `GetPlayer`。
|
|
23
|
+
- 玩家互换用 `GetOpponent`,不写 `OtherPlayer`;玩家换成格子取值用 `PlayerToSquareValue`,不写 `SquareValueOf`。名字要点明两端角色。
|
|
21
24
|
- 布尔变量用 `is_` / `has_` / `can_` 开头:`is_valid`、`has_stock`;不要用 `flag`、`status`。
|
|
22
25
|
- 容器用复数:`tasks`、`orders`;单个元素用单数。
|
|
23
26
|
- 缩写按单词处理:`url`、`id`、`http`,不要写 `URL` 混进 snake_case;类型名里可以 `HttpClient`。
|
|
24
|
-
-
|
|
27
|
+
- 不用单字母名:坐标参数和局部变量用 `pos`(`position` 的常见缩写),行、列用 `row`、`col`;公开数据成员仍用完整名词(`Move::position`),避免 API 缩写。`i` / `j` 只用于没有领域名字的计数(如遍历固定长度的下标)。也不要用 `data`、`info`、`tmp` 这类不说明内容的名字。
|
|
28
|
+
- 参数或局部变量的类型已在签名里写明时,用主流缩写而不是重复类型名:`Position pos`,不写 `Position position`;缩写只在含义无歧义时使用。
|
|
29
|
+
|
|
30
|
+
## 字符集与保留标识符
|
|
31
|
+
|
|
32
|
+
- 代码标识符只用 ASCII:字母、数字、下划线。标准允许 Unicode XID 标识符,但工具链、字体与跨平台编辑不一致,Google 同样要求 ASCII。
|
|
33
|
+
- 标识符大小写敏感;不要用只差大小写的两个名字区分不同实体。
|
|
34
|
+
- 以下名字保留给实现,写了也不保证有诊断:任何位置出现双下划线 `__`;以下划线加大写字母开头;在全局命名空间中以下划线开头。
|
|
35
|
+
- 不向 `std` 添加声明;只有在标准允许且依赖用户定义类型时,才特化标准模板(如 `std::formatter`、`std::hash`)。
|
|
36
|
+
- 顶层命名空间取项目或团队独有的名字,避免与已有库冲突;不要嵌套 `std`、`absl` 这类知名顶层命名空间。
|
|
25
37
|
|
|
26
38
|
## 语义角色
|
|
27
39
|
|
|
@@ -31,7 +43,7 @@
|
|
|
31
43
|
| 判断 | `Is` / `Has` / `Can` | `IsEmpty()`、`HasStock()`、`HasLine()` | 返回 `bool`,不抛异常 |
|
|
32
44
|
| 查找 | `Find` | `FindTask()` | 找不到是正常结果,返回 `optional` 或迭代器,不抛 |
|
|
33
45
|
| 计数 | `Count` | `CountPendingTasks()` | 返回数量,不修改状态 |
|
|
34
|
-
| 尝试 | `Try` / 领域动词 | `TryReserve()`、`Place()` | 可能因多个前置条件失败但不抛异常,返回 `enum class`
|
|
46
|
+
| 尝试 | `Try` / 领域动词 | `TryReserve()`、`Place()` | 可能因多个前置条件失败但不抛异常,返回 `enum class` 结果;调用方按名区分失败原因。只有一个失败原因、调用方不需要区分时返回 `bool` |
|
|
35
47
|
| 确定性动作 | 领域动词 | `CompleteTask()`, `Reset()` | 前置条件满足时一定成功;无法完成任务时抛 |
|
|
36
48
|
| 计算 | 动词开头 | `ScoreOrder()` | 从输入算出结果,不修改外部状态 |
|
|
37
49
|
| 映射 | `<来源>To<目标>` | `OrderToReceipt()` | 名字必须含两端角色 |
|
|
@@ -52,9 +64,11 @@
|
|
|
52
64
|
- 前缀集就是这四类:`Is` / `Has` / `Can`,外加与成对规则一一对应的 `Completes`、`Would` 这类限定式;`Contains` 只保留给容器成员(`std::vector::empty` 一类已有标准名),不拿它当一般谓词。
|
|
53
65
|
- `Get` 只用于取值,`Set` 只用于设值;不要用 `CheckLine()` 当布尔 getter。
|
|
54
66
|
- `Check` / `Validate` 留给会报告错误、产生诊断或可能失败的操作;它们不返回纯 `bool` 查询。
|
|
55
|
-
- 动作可能因为多个前置条件失败、而调用方需要区分原因时,返回 `enum class`(`
|
|
56
|
-
-
|
|
57
|
-
-
|
|
67
|
+
- 动作可能因为多个前置条件失败、而调用方需要区分原因时,返回 `enum class`(`Square::PlaceResult`、`Game::PlaceResult`、`WinPreviewResult`),不要用 `bool` 加输出参数,也不要拆成一串 `IsXxx` 判断再重复检查。
|
|
68
|
+
- 结果枚举的取值写结果类别,两个词以内:成功用 `kSuccess`;失败按发生原因分别取名(`kIgnored`、`kOccupied`、`kRejected`、`kDenied`),不合并成一个 `kInvalid`。行尾注释以类别开头,写成「成功:…」「失败:…」,冒号后写这次调用的结果与原因,各行按「传入的值 → 格子状态 → 写入结果」的顺序写。完整案例见 `references/naming/examples/naming-state-change.md`。
|
|
69
|
+
- 结果枚举的类型用「操作或对象 + Result」,读作「什么的 Result」:`Place` 的结果是 `PlaceResult`,格子上一次写入的结果是 `Square::PlaceResult`。各层结果嵌在各自类型里,靠 `Square::PlaceResult` 与 `Game::PlaceResult` 区分,不要合成一个跨层的名字。
|
|
70
|
+
- 枚举的取值写成短句,与类型名对齐:`BoardStatus` 用 `kInProgress`、`kXWon`、`kOWon`、`kDrawn`;`SquareValue` 用 `kEmpty`、`kX`、`kO`,行尾写「空格子」「X 标记」「O 标记」;不写 `kXWins` 这类主谓句,也不掺动作名。
|
|
71
|
+
- 假设性查询在名字里写明条件:`PreviewWin(pos, cell)` 读作「这一次落子会成线吗」,返回 `WinPreviewResult` 区分「不能落子」与「能落但不赢」;只有成功与不报错的纯判断才用 `bool`。同一 API 只选一种,改名时同步声明、定义、调用点、测试和注释。
|
|
58
72
|
- 私有谓词同样用肯定式;不要用 `flag`、`status` 或 `check` 这类不说明判据的名字。
|
|
59
73
|
|
|
60
74
|
## 类型命名的判断顺序
|
|
@@ -62,6 +76,7 @@
|
|
|
62
76
|
1. 纯数据、无 invariant、可以直接聚合初始化 → `struct`,名字是名词:`Order`, `TaskRecord`。
|
|
63
77
|
2. 有 invariant、需要构造函数维持 → `class`,名字是名词:`TaskTable`, `Warehouse`。
|
|
64
78
|
3. 封闭的状态集合 → `enum class`,名字是单数名词:`TaskState`, `OrderResult`;枚举子用 `k` 前缀。
|
|
79
|
+
- 类型名用拥有它的那一层的词汇:`Board` 的返回值是 `BoardStatus`,不写 `GameResult`;`Game` 组合 `Board`,`Board` 不应出现 `Game` 前缀。反例见 `references/naming/examples/naming-lower-layer.md`。
|
|
65
80
|
4. 只有行为、没有状态,且调用方需要替换实现 → 抽象基类;只有一个实现时先用普通类。
|
|
66
81
|
5. 一组自由函数 → 命名空间 + 动词;不要为了「像 Java」造一个 `XxxUtil` 类。
|
|
67
82
|
|
|
@@ -71,6 +86,19 @@
|
|
|
71
86
|
- 不要把改名和改行为混在一个提交里;改名后测试必须仍然绿色且行为句不变。
|
|
72
87
|
- 公共 API 改名视为破坏性变更;需要兼容时保留旧名转发,并在注释里写弃用期限。
|
|
73
88
|
|
|
89
|
+
## 与 Google、Core Guidelines 的关系
|
|
90
|
+
|
|
91
|
+
- 本工作流的命名基线是 Google C++ Style:类型与函数 PascalCase,变量 snake_case,常量与枚举子 `k` 前缀。项目内不再另立一套。
|
|
92
|
+
- C++ Core Guidelines 的 NL 系列是“没有更好想法时的建议”。NL.10 偏好全 `underscore_style`,与本基线不同;这里采用它的 NL.5(名字不编码类型)、NL.7(名字长度与作用域成比例)、NL.8(风格一致)、NL.9(全大写只给宏)与 NL.19(避免易误读),不整体切换到 `underscore_style`。
|
|
93
|
+
- 两者冲突时以本文件的明确规则为准;本文件没写的,按 Google 基线处理。
|
|
94
|
+
|
|
95
|
+
## 依据
|
|
96
|
+
|
|
97
|
+
- ISO C++ `[lex.name]`:标识符字符集、大小写敏感与保留标识符。https://eel.is/c++draft/lex.name
|
|
98
|
+
- ISO C++ `[namespace.std]`:向 `std` 添加声明与特化的限制。https://eel.is/c++draft/namespace.std
|
|
99
|
+
- Google C++ Style Guide, Naming:https://google.github.io/styleguide/cppguide.html#Naming
|
|
100
|
+
- C++ Core Guidelines, NL: Naming and layout:https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#s-naming
|
|
101
|
+
|
|
74
102
|
## 术语
|
|
75
103
|
|
|
76
|
-
中文术语与英文标识符的对应见 `references/glossary.md`。代码标识符用英文;注释可以用中文,但同一个概念只用一种译法。
|
|
104
|
+
中文术语与英文标识符的对应见 `references/language/glossary/language-terms.md`。代码标识符用英文;注释可以用中文,但同一个概念只用一种译法。
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Google C++ 风格:本技能采用的版本
|
|
2
2
|
|
|
3
|
-
以 [Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html)
|
|
3
|
+
以 [Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html) 为基线。除「异常政策」「新工程默认 C++23」和「注释格式」三处明确偏离外,排版、命名、头文件、包含顺序和类型都按 Google 原文执行;本地习惯不能悄悄覆盖它。
|
|
4
4
|
|
|
5
5
|
## 语言版本
|
|
6
6
|
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
|
|
13
13
|
## 头文件
|
|
14
14
|
|
|
15
|
-
- 每个头文件用 `#define` 守卫,符号由项目源码树全路径生成:`<PROJECT>_<PATH>_<FILE>_H_`,例如 `FOO_BAR_BAZ_H_`。不要用 `#pragma once`,也不要两者并存;模板见 `references/file-template.md`。
|
|
15
|
+
- 每个头文件用 `#define` 守卫,符号由项目源码树全路径生成:`<PROJECT>_<PATH>_<FILE>_H_`,例如 `FOO_BAR_BAZ_H_`。不要用 `#pragma once`,也不要两者并存;模板见 `references/layout/02-file-template.md`。
|
|
16
16
|
- 头文件必须自包含:自己用到的类型、宏、标准库头都直接包含,不依赖调用方的包含顺序。
|
|
17
17
|
- `.cpp` 第一个包含自己的配对头,然后按下面的顺序包含。
|
|
18
18
|
- 包含顺序:配对头 → C 系统头 → C++ 标准库头 → 其他库头 → 本项目头。每组之间空一行,组内按字母序;不要依赖 `clang-format` 的 `IncludeCategories` 自定义分组,本项目不再覆盖 Google 预设。
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
|
|
25
25
|
## 命名
|
|
26
26
|
|
|
27
|
-
大小写、前缀、成员尾随 `_`、常量与枚举子的 `k` 前缀、命名空间与文件名的完整规则表,以及访问器、谓词、`Try`、映射函数、`current_` 前缀的语义角色,见 `references/naming.md`。本文只保留两处 Google 原文允许选择、而本项目需要统一的点:
|
|
27
|
+
大小写、前缀、成员尾随 `_`、常量与枚举子的 `k` 前缀、命名空间与文件名的完整规则表,以及访问器、谓词、`Try`、映射函数、`current_` 前缀的语义角色,见 `references/naming/identifier-names.md`。本文只保留两处 Google 原文允许选择、而本项目需要统一的点:
|
|
28
28
|
|
|
29
29
|
- 访问器、修改器 Google 允许 PascalCase 或 snake_case;本项目统一 PascalCase,同一 API 不混用两套。
|
|
30
30
|
- 测试名用 PascalCase 描述行为,例如 `RejectsOutOfRangeTask`;测试文件用 snake_case。
|
|
@@ -34,10 +34,10 @@
|
|
|
34
34
|
- 纯数据聚合用 `struct`;有不变量的类型用 `class`,不变量在构造函数和公共成员函数里维持。
|
|
35
35
|
- 单参数构造函数写 `explicit`;零参数构造函数不写。
|
|
36
36
|
- 默认成员初始化写在声明处(NSDMI),不要在构造函数初始化列表里重复。
|
|
37
|
-
- 默认用花括号初始化:`int count{};`、`std::vector<int> values{1, 2, 3};`。成员声明处一律写 `{}`;只有需要拷贝初始化或 `auto` 推导时用 `=`;`name = {{` 只用于多行聚合常量表,单个对象和空初始化仍写 `count{}`;成员写 `=` 的例外见 `references/language.md
|
|
37
|
+
- 默认用花括号初始化:`int count{};`、`std::vector<int> values{1, 2, 3};`。成员声明处一律写 `{}`;只有需要拷贝初始化或 `auto` 推导时用 `=`;`name = {{` 只用于多行聚合常量表,单个对象和空初始化仍写 `count{}`;成员写 `=` 的例外见 `references/language/language-basics/02-initialization.md`;不要用 `()` 初始化普通对象。
|
|
38
38
|
- 类声明顺序:public → protected → private;类型与别名 → 静态常量 → 工厂 → 构造/赋值/析构 → 其他函数 → 数据成员。
|
|
39
39
|
- 用户声明的拷贝/移动/析构函数在头文件声明、在 `.cpp` 定义;只有零参数且默认实现足够时才写 `= default`。
|
|
40
|
-
- 查询成员函数标尾部 `const`;返回结果对象的 API 加 `[[nodiscard]]`,不要给所有非 `void` 函数机械添加。标注位置与 `friend` 的例外见 `references/types.md
|
|
40
|
+
- 查询成员函数标尾部 `const`;返回结果对象的 API 加 `[[nodiscard]]`,不要给所有非 `void` 函数机械添加。标注位置与 `friend` 的例外见 `references/language/value-types/07-nodiscard.md`。
|
|
41
41
|
- 所有权优先 `std::unique_ptr`;没有共享所有权的理由时不用 `std::shared_ptr`。禁止裸 `new` / `delete`。
|
|
42
42
|
- 禁止 C 风格强制转换;用 `static_cast`、`const_cast`、`reinterpret_cast`、`std::bit_cast`,并让 `clang-tidy` 的 `google-readability-casting` 兜底。
|
|
43
43
|
- `.cpp` 里不需要外部引用的 `constexpr` 直接放具名命名空间;仅一个函数使用时才用函数内 `static constexpr`;函数和类型仍放匿名命名空间。
|
|
@@ -61,15 +61,15 @@
|
|
|
61
61
|
- 混合优先级算术加显式括号,消除 `readability-math-missing-parentheses`。
|
|
62
62
|
- 内建整数默认用 `int`;需要确定宽度时用 `<cstdint>` 的 `std::int32_t`、`std::int64_t` 等(可省略 `std::` 前缀),可能到 2^31 的值用 64 位;`std::size_t` / `std::ptrdiff_t` 只用于大小和指针差。不要用无符号类型表达「这个值不会为负」。
|
|
63
63
|
- 封闭状态码用 `enum class`;底层类型默认 `int`,只有确实需要缩小存储时才写固定底层类型并说明理由。枚举值总是带作用域:`OrderResult::kSuccess`。
|
|
64
|
-
- 枚举的尾随注释要么全写要么全不写;两种句式与禁止项见 `references/comments.md`「枚举项与 `struct` 短字段:两种句式」。
|
|
64
|
+
- 枚举的尾随注释要么全写要么全不写;两种句式与禁止项见 `references/comments/comment-rules.md`「枚举项与 `struct` 短字段:两种句式」。
|
|
65
65
|
- 浮点不用 `==` 比较计算结果;按量纲给容差,金额用最小货币单位的整数。
|
|
66
|
-
- 数值宽度、符号、枚举、强类型、`const` 成员和下标访问的完整规则见 `references/types.md`;初始化、RAII、移动和模板见 `references/language
|
|
66
|
+
- 数值宽度、符号、枚举、强类型、`const` 成员和下标访问的完整规则见 `references/language/value-types/07-nodiscard.md`;初始化、RAII、移动和模板见 `references/language/language-basics/`。
|
|
67
67
|
|
|
68
68
|
## 异常
|
|
69
69
|
|
|
70
|
-
Google 原文不使用 C++ 异常;本工作流只在开发阶段的源码指导中按 `references/exceptions.md` 采用 C++ Core Guidelines 的 Error handling,插件运行时不编译、不执行这套策略。其余条目仍以 Google 原文为准。
|
|
70
|
+
Google 原文不使用 C++ 异常;本工作流只在开发阶段的源码指导中按 `references/exceptions/throw-rules.md` 采用 C++ Core Guidelines 的 Error handling,插件运行时不编译、不执行这套策略。其余条目仍以 Google 原文为准。
|
|
71
71
|
|
|
72
|
-
异常消息的组织方式见 `references/exceptions.md` 的「消息」节。
|
|
72
|
+
异常消息的组织方式见 `references/exceptions/throw-rules.md` 的「消息」节。
|
|
73
73
|
|
|
74
74
|
## 输出与格式化
|
|
75
75
|
|
|
@@ -82,5 +82,6 @@ Google 原文不使用 C++ 异常;本工作流只在开发阶段的源码指
|
|
|
82
82
|
## 注释
|
|
83
83
|
|
|
84
84
|
- 注释写意图、契约、取舍,不复述代码。
|
|
85
|
-
-
|
|
85
|
+
- 注释格式是本项目的第三处偏离:用 Doxygen 的 `///` 加 `@file`、`@brief`、`@param` 组织,Google 原文用 `//` 散文、文件头放 license boilerplate;本项目源码不带 license 头,分发或开源前补上。
|
|
86
|
+
- 文件头、声明注释、实现注释与配置注释的格式见 `references/comments/comment-style.md`,写什么见 `comment-rules.md`。
|
|
86
87
|
- TODO 统一写 `// TODO: <阶段或 issue> - <要做什么>`;不要写 `@todo`、`FIXME` 或只有「参考」没有动作的占位。
|