@chengzhao-dev/dsh-cpp-workflow 0.7.9 → 0.7.11
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 +8 -7
- package/README.md +5 -4
- package/cordis.patch.yml +2 -2
- package/icon.svg +5 -5
- package/lib/client.js +542 -538
- package/lib/comment-policy.js +37 -33
- package/lib/workspace.js +8 -10
- package/locale/en.json +4 -4
- package/locale/zh.json +4 -4
- package/package.json +100 -100
- package/skills/building-cpp/SKILL.md +5 -5
- package/skills/building-cpp/assets/config/.clang-tidy +4 -5
- package/skills/building-cpp/assets/config/.clangd +5 -7
- package/skills/building-cpp/assets/config/.vscode/extensions.json +5 -5
- package/skills/building-cpp/assets/config/.vscode/settings.json +56 -58
- package/skills/building-cpp/examples/environment.md +13 -102
- package/skills/building-cpp/references/01-build-script.md +3 -3
- package/skills/building-cpp/references/02-environment.md +8 -8
- package/skills/building-cpp/scripts/checking-environment.mjs +424 -424
- package/skills/building-cpp/steps/01-build.md +10 -10
- package/skills/building-cpp/steps/02-tooling.md +6 -6
- package/skills/building-cpp/steps/03-check.md +2 -2
- package/skills/building-cpp/steps/04-vscode.md +4 -6
- package/skills/developing-cpp/SKILL.md +8 -9
- package/skills/developing-cpp/references/comments/01-comment-rules.md +50 -26
- package/skills/developing-cpp/references/comments/02-style.md +33 -25
- package/skills/developing-cpp/references/comments/examples/03-declaration-enum.md +14 -14
- package/skills/developing-cpp/references/comments/examples/04-declaration-file.md +12 -8
- package/skills/developing-cpp/references/comments/examples/05-declaration-function.md +20 -10
- package/skills/developing-cpp/references/comments/examples/06-declaration-member.md +4 -4
- package/skills/developing-cpp/references/comments/examples/07-declaration-predicate.md +2 -2
- package/skills/developing-cpp/references/comments/examples/08-declaration-struct.md +6 -6
- package/skills/developing-cpp/references/comments/examples/09-implementation-body.md +41 -41
- package/skills/developing-cpp/references/comments/glossary/01-comment-terms.md +17 -16
- package/skills/developing-cpp/references/design/01-project-design.md +21 -10
- package/skills/developing-cpp/references/design/02-raii.md +5 -4
- package/skills/developing-cpp/references/design/03-third-party.md +2 -2
- package/skills/developing-cpp/references/design/examples/02-coordinate-table.md +12 -12
- package/skills/developing-cpp/references/design/examples/03-header-place.md +1 -1
- package/skills/developing-cpp/references/design/examples/04-library-boundary.md +6 -6
- package/skills/developing-cpp/references/design/examples/06-move-result.md +29 -28
- package/skills/developing-cpp/references/design/examples/07-preview-read.md +24 -23
- package/skills/developing-cpp/references/design/examples/08-screen-enter-exit.md +19 -18
- package/skills/developing-cpp/references/design/examples/09-option-menu.md +6 -6
- package/skills/developing-cpp/references/design/session-layer.md +13 -22
- package/skills/developing-cpp/references/exceptions/01-exception-rules.md +57 -57
- package/skills/developing-cpp/references/exceptions/examples/01-throw-boundary.md +7 -20
- package/skills/developing-cpp/references/exceptions/examples/02-throw-build-mode.md +10 -11
- package/skills/developing-cpp/references/exceptions/examples/03-throw-catch.md +4 -4
- package/skills/developing-cpp/references/exceptions/examples/04-throw-destructor.md +9 -5
- package/skills/developing-cpp/references/exceptions/examples/07-throw-when.md +2 -2
- package/skills/developing-cpp/references/index.md +32 -35
- package/skills/developing-cpp/references/language/glossary/01-language-terms.md +3 -2
- package/skills/developing-cpp/references/language/language-basics/02-initialization.md +1 -1
- package/skills/developing-cpp/references/language/language-basics/05-expressions.md +9 -2
- package/skills/developing-cpp/references/language/language-basics/06-functions.md +2 -3
- package/skills/developing-cpp/references/language/language-basics/09-templates.md +3 -3
- package/skills/developing-cpp/references/language/language-basics/10-language-checklist.md +9 -9
- package/skills/developing-cpp/references/language/performance/03-memory.md +1 -1
- package/skills/developing-cpp/references/language/performance/07-checklist.md +6 -6
- package/skills/developing-cpp/references/language/standard-library/01-containers.md +4 -5
- package/skills/developing-cpp/references/language/standard-library/02-access.md +2 -5
- package/skills/developing-cpp/references/language/standard-library/05-string.md +1 -1
- package/skills/developing-cpp/references/language/standard-library/06-format.md +22 -20
- package/skills/developing-cpp/references/language/standard-library/{11-format-ecosystem.md → 07-format-ecosystem.md} +2 -11
- package/skills/developing-cpp/references/language/standard-library/{07-time.md → 08-time.md} +1 -1
- package/skills/developing-cpp/references/language/standard-library/{09-files.md → 10-files.md} +1 -1
- package/skills/developing-cpp/references/language/standard-library/11-library-checklist.md +8 -0
- package/skills/developing-cpp/references/language/value-types/02-signed.md +7 -7
- package/skills/developing-cpp/references/language/value-types/03-integer-roles.md +14 -2
- package/skills/developing-cpp/references/language/value-types/05-strong-type.md +2 -2
- package/skills/developing-cpp/references/language/value-types/06-const-method.md +5 -5
- package/skills/developing-cpp/references/language/value-types/07-nodiscard.md +1 -1
- package/skills/developing-cpp/references/language/value-types/08-comparison.md +1 -1
- package/skills/developing-cpp/references/language/value-types/09-subscript.md +1 -1
- package/skills/developing-cpp/references/language/value-types/11-checklist.md +7 -7
- package/skills/developing-cpp/references/layout/01-source-layout.md +13 -13
- package/skills/developing-cpp/references/layout/02-file-template.md +10 -10
- package/skills/developing-cpp/references/layout/03-declaration-order.md +83 -42
- package/skills/developing-cpp/references/naming/01-identifier-names.md +32 -30
- package/skills/developing-cpp/references/naming/examples/06-naming-lower-layer.md +15 -15
- package/skills/developing-cpp/references/naming/examples/07-naming-multiple-return.md +2 -2
- package/skills/developing-cpp/references/naming/examples/13-naming-state-change.md +24 -23
- package/skills/developing-cpp/references/naming/glossary/01-naming-terms.md +3 -3
- package/skills/developing-cpp/references/naming/glossary/02-position.md +9 -9
- package/skills/developing-cpp/references/naming/glossary/03-selection.md +5 -1
- package/skills/developing-cpp/references/naming/glossary/04-square.md +3 -3
- package/skills/developing-cpp/references/naming/glossary/05-scoring.md +2 -2
- package/skills/developing-cpp/references/naming/glossary/06-difficulty.md +7 -7
- package/skills/developing-cpp/references/naming/glossary/07-stream.md +1 -1
- package/skills/developing-cpp/references/style/01-google-cpp-style.md +13 -23
- package/skills/developing-cpp/scripts/checking-comments.mjs +961 -755
- package/skills/developing-cpp/scripts/checking-declaration-order.mjs +512 -515
- package/skills/developing-cpp/steps/01-behavior.md +0 -1
- package/skills/developing-cpp/steps/02-boundary.md +2 -3
- package/skills/developing-cpp/steps/03-declaration.md +9 -10
- package/skills/developing-cpp/steps/04-implementation.md +7 -8
- package/skills/engineering/SKILL.md +9 -10
- package/skills/engineering/references/cmake/assets/LinkSettings.cmake +1 -1
- package/skills/engineering/references/cmake/linking/02-rpath.md +5 -6
- package/skills/engineering/references/cmake/modules/01-functions.md +9 -9
- package/skills/engineering/references/cmake/standards/01-cxx-standard.md +6 -6
- package/skills/engineering/references/cmake/standards/index.md +8 -29
- package/skills/engineering/references/cmake/targets/01-target-rules.md +14 -13
- package/skills/engineering/references/cmake/targets/examples/01-rpath.md +1 -1
- package/skills/engineering/references/cmake/testing/01-ctest.md +3 -3
- package/skills/engineering/references/formatting/01-encoding.md +5 -5
- package/skills/engineering/references/formatting/02-clang-format.md +2 -2
- package/skills/engineering/references/formatting/03-clang-tidy.md +5 -5
- package/skills/engineering/references/formatting/04-clangd.md +32 -39
- package/skills/engineering/references/formatting/05-config-comments.md +88 -0
- package/skills/engineering/references/formatting/examples/02-clang-tidy.md +6 -6
- package/skills/engineering/references/formatting/examples/03-clangd.md +17 -17
- package/skills/engineering/references/index.md +5 -4
- package/skills/engineering/scripts/checking-encoding.mjs +369 -369
- package/skills/engineering/steps/01-targets.md +3 -3
- package/skills/engineering/steps/02-modules.md +1 -2
- package/skills/engineering/steps/03-check.md +2 -3
- package/templates/cpp-workflow-project/SKILL.md +1 -1
- package/templates/cpp-workflow-project/environment.json +4 -4
- package/skills/developing-cpp/references/design/examples/05-library-split.md +0 -8
- package/skills/developing-cpp/references/language/standard-library/10-library-checklist.md +0 -10
- package/skills/developing-cpp/references/naming/examples/02-naming-basic.md +0 -4
- package/skills/developing-cpp/references/naming/examples/10-naming-rename.md +0 -6
- package/skills/engineering/references/formatting/examples/04-config-comments.md +0 -70
- /package/skills/developing-cpp/references/language/standard-library/{08-random.md → 09-random.md} +0 -0
package/skills/developing-cpp/references/language/standard-library/{07-time.md → 08-time.md}
RENAMED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
# 时间
|
|
3
3
|
|
|
4
4
|
- 测耗时用 `std::chrono::steady_clock`:单调、不受系统时间调整影响;`system_clock` 只用于日历时间,不用来算间隔。
|
|
5
|
-
- 两个时间点相减得到 `duration`;换算单位用 `duration_cast`,它会截断,需要舍入时用 C++
|
|
5
|
+
- 两个时间点相减得到 `duration`;换算单位用 `duration_cast`,它会截断,需要舍入时用 C++17 起的 `floor` / `ceil` / `round`。
|
|
6
6
|
- 需要毫秒、秒等具名单位时写 `std::chrono::milliseconds` 等别名,不手算 `1000`。
|
|
7
7
|
- `sleep_for` / `sleep_until` 至少睡到指定时长,调度可能让它更晚;不要用它做精确节拍。
|
|
8
8
|
- C++20 的日历与时区用 `<chrono>` 的 `year_month_day`、`zoned_time`;跨时区时间不要手工加减小时。
|
package/skills/developing-cpp/references/language/standard-library/{09-files.md → 10-files.md}
RENAMED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
- 文件用 `std::ifstream` / `std::ofstream`;构造后检查 `is_open()`,读写后检查流状态(`fail` / `bad`),不要假设成功。
|
|
5
5
|
- 文本按行读用 `std::getline`;二进制在 Windows 用 `std::ios::binary`,避免换行转换。
|
|
6
6
|
- 需要异常时先 `exceptions()` 设置掩码;默认 iostream 不抛异常。
|
|
7
|
-
- 路径用 `std::filesystem::path`,拼接用
|
|
7
|
+
- 路径用 `std::filesystem::path`,拼接用 `operator/`,例如 `path{dir} / "name"`;不要用字符串相加,也不要带 `./` 前缀。遍历/查询用 `std::filesystem`,失败用 `error_code` 重载或捕获 `filesystem_error`。
|
|
8
8
|
- 写文件先写临时文件再改名,避免半写状态;需要原子替换时用 `rename`。
|
|
9
9
|
|
|
10
10
|
## 依据
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
<!-- 读取时机:查语言细节时;读完能回答:检查清单;预算:一次只开这一份(7 行),不自动串联。 -->
|
|
2
|
+
# 检查清单
|
|
3
|
+
|
|
4
|
+
- [ ] 容器选择有明确理由,默认是 `vector`(`01-containers.md`)。
|
|
5
|
+
- [ ] 没有保存 `string_view` / `span` 指向的临时对象(`05-string.md`)。
|
|
6
|
+
- [ ] 修改容器大小前处理了迭代器失效(`03-iteration.md`)。
|
|
7
|
+
- [ ] 能用算法表达时没有手写下标循环(`04-algorithms.md`)。
|
|
8
|
+
- [ ] 自定义类型的 formatter 只接受默认格式,非法枚举有稳定回退,输出可直接用 `std::format` 断言(`06-format.md`)。
|
|
@@ -4,21 +4,21 @@
|
|
|
4
4
|
- 普通整数类型默认有符号;只有确有非负语义且理解边界时才用无符号。
|
|
5
5
|
- 无符号运算按模 `2^N` 回绕,这是有定义的行为;有符号整数溢出是未定义行为。
|
|
6
6
|
- 越界、空指针、未初始化等未定义行为的完整清单与后果见 `../performance/04-undefined-behavior.md`。
|
|
7
|
-
-
|
|
7
|
+
- 两边都是整数时,`/` 向零截断,`%` 给余数;`7 / 2` 是 `3`。整数除以零是未定义行为,不能与浮点的 `inf` / `nan` 混为一谈。
|
|
8
8
|
- 浮点转整数会截断小数;大整数转小整数可能丢高位或改变符号。先检查范围,再 `static_cast`。
|
|
9
9
|
- 禁止 C 风格强转 `(int)value`;它隐藏转换类别,`google-readability-casting` 会报。
|
|
10
10
|
|
|
11
11
|
```cpp
|
|
12
12
|
int CountWide(const std::vector<int>& values) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
int count = 0;
|
|
14
|
+
for (std::size_t index = 0; index < values.size(); ++index) {
|
|
15
|
+
if (values[index] > 0) ++count;
|
|
16
|
+
}
|
|
17
|
+
return count;
|
|
18
18
|
}
|
|
19
19
|
```
|
|
20
20
|
|
|
21
|
-
循环下标与容器大小都用 `std::size_t`(角色表见 `references/language/value-types/03-integer-roles.md
|
|
21
|
+
循环下标与容器大小都用 `std::size_t`(角色表见 `references/language/value-types/03-integer-roles.md`),避免混合比较。业务编号与坐标的约定见 `03-integer-roles.md` 与 `04-enum.md`。
|
|
22
22
|
|
|
23
23
|
## 依据
|
|
24
24
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
<!-- 读取时机:查语言细节时;读完能回答:整型角色的通用约定;预算:一次只开这一份(
|
|
1
|
+
<!-- 读取时机:查语言细节时;读完能回答:整型角色的通用约定;预算:一次只开这一份(51 行),不自动串联。 -->
|
|
2
2
|
# 整型的角色与选型
|
|
3
3
|
|
|
4
4
|
| 角色 | 类型 | 理由 |
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
| 遍历标准容器的下标 | `std::size_t` | 与 `size()` 同类型,避免混合比较 |
|
|
8
8
|
| 坐标等允许表示范围外取值的下标 | `int` | 能表达负值;越界判断交给持有尺寸的类型 |
|
|
9
9
|
| 没有上面两种理由的整数 | `int` | 宽度足够,写起来最短 |
|
|
10
|
-
|
|
|
10
|
+
| 需要固定布局、序列化或前向声明的 `enum class` 的底层类型 | `std::uint8_t` | 只表达取值集合与布局,不参与算术;普通枚举用默认底层类型,见 `04-enum.md` |
|
|
11
11
|
|
|
12
12
|
- 协议、文件格式与跨平台布局仍用 `<cstdint>` 的定宽整数(如 `std::int32_t`);工程内普通代码不必写定宽类型。
|
|
13
13
|
- 不用裸 `int` 承担跨平台协议或布局;它的宽度不可预测。
|
|
@@ -37,4 +37,16 @@ return static_cast<std::size_t>(row) * static_cast<std::size_t>(column_count) +
|
|
|
37
37
|
|
|
38
38
|
- 编译期常量走整体转换,运行期值逐个拓宽;尺寸很小时两者结果相同,规则约束的是尺寸或坐标来源变化后的行为。有符号 `int` 溢出是未定义行为,拓宽后整个表达式按无符号回绕,行为有定义。
|
|
39
39
|
- `std::size_t{row}` 不能替代 `static_cast`:`row` 可能为负且不是常量表达式,花括号初始化会因窄化编译失败。先检查范围,再转换。
|
|
40
|
+
- 判断坐标是否落在尺寸范围内时方向相反:把尺寸收成 `int` 再比,不要把坐标拓宽成 `std::size_t`。两个比较都留在 `int` 域,读者核对的是同一组整数;转成无符号后 `-1` 会变成极大的正数,只能靠前面的 `>= 0` 兜住,中间那次转换成了一个看不见的分支。
|
|
41
|
+
|
|
42
|
+
```cpp
|
|
43
|
+
// 要:上下界都在 int 域,坐标不转换
|
|
44
|
+
return (position.row >= 0 && position.row < static_cast<int>(kRows)) &&
|
|
45
|
+
(position.column >= 0 && position.column < static_cast<int>(kColumns));
|
|
46
|
+
|
|
47
|
+
// 不要:把坐标拓宽成无符号再比
|
|
48
|
+
return position.row >= 0 && static_cast<std::size_t>(position.row) < kRows;
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
前提是尺寸放得进 `int`(棋盘边长这类个位到千位的维数都满足)。容器长度可能超过 `INT_MAX` 时不能用这条,改用 `std::cmp_less`。`[]` 前的下标仍要收成 `std::size_t`,那是「已经证明合法之后取值」,见 `09-subscript.md`。
|
|
40
52
|
- 工程另开 `bugprone-implicit-widening-of-multiplication-result` 时,先算后转会被这条检查报出;项目模板默认不启用 `bugprone-*`,本规则仍要遵守。
|
|
@@ -3,12 +3,12 @@
|
|
|
3
3
|
|
|
4
4
|
```cpp
|
|
5
5
|
class TaskBoard {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
public:
|
|
7
|
+
[[nodiscard]] TaskState GetTaskState(TaskId id) const;
|
|
8
|
+
void SetTaskState(TaskId id, TaskState state);
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
private:
|
|
11
|
+
std::array<TaskState, kTaskCount> tasks_{};
|
|
12
12
|
};
|
|
13
13
|
```
|
|
14
14
|
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
- 不给所有非 `void` 函数机械添加;标注的价值来自稀缺。
|
|
6
6
|
- 写法位置:`[[nodiscard]]` 在返回类型前面,然后才是 `static` 或 `constexpr`;参数列表之后才是 `const`、`override`、`noexcept`。
|
|
7
7
|
- 类内 `friend` 不加 `[[nodiscard]]`:Clang 拒绝 `[[nodiscard]] friend`、`friend [[nodiscard]]` 和参数列表后的 `[[nodiscard]]`。不要再猜下一种写法。
|
|
8
|
-
- 运算符不加 `[[nodiscard]]`,成员与 `friend`
|
|
8
|
+
- 运算符不加 `[[nodiscard]]`,成员与 `friend` 都一样;比较运算符的签名、参数命名与 `friend` 形式见 `08-comparison.md`。
|
|
9
9
|
- 不要用 `static_cast<void>` 丢弃 `[[nodiscard]]` 的结果:先接住返回值并分支,把失败分支写出来;不能处理的失败说明接口或合同有问题,不是转换能解决的。测试里验证抛异常时,用 `[[maybe_unused]]` 的局部变量接住,不要用 `static_cast<void>` 跳过约定。
|
|
10
10
|
- 测试里验证“必须处理结果”时,同时验证忽略能被编译器发现。
|
|
11
11
|
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
# 比较运算符
|
|
3
3
|
|
|
4
4
|
- 默认比较只给调用方真的会比较的值类型:`Position`、`Move` 这类坐标与记录可以默认比较;`Board` 这类实体在正常流程里不比较,不因为成员可比较就提供 `operator==`。测试要比较实体时,逐格或逐字段比较它的可观察状态,不把比较运算符加进生产接口。
|
|
5
|
-
-
|
|
5
|
+
- 默认比较优先写成员形式:`bool operator==(const Position& other) const = default;`,另一个对象统一叫 `other`,注释补一条 `@param other`;`friend` 形式只在必须非成员时使用,写 `friend bool operator==(const T&, const T&) = default;`,两个参数都不具名,注释也不写 `@param`。运算符本身不加 `[[nodiscard]]`,见 `07-nodiscard.md`。
|
|
6
6
|
|
|
7
7
|
## 标准与推荐
|
|
8
8
|
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
| 无下标 | `set`、`unordered_set`、`multimap` | 无 `operator[]` / `at` | — |
|
|
11
11
|
|
|
12
12
|
- 默认用 `at()`,越界时抛出 `std::out_of_range`,而不是未定义行为;只有调用点已经证明下标合法、并且这条路径对性能敏感时,才用 `[]`,同时保留证明依据。坐标类型不拥有容器区间;区间判断属于持有尺寸的类型(如 `Board::IsInRange`)。`std::span` 在 C++20 没有 `at`,`span::at` 到 C++26 才进入标准。
|
|
13
|
-
- 已经证明非负的坐标在 `[]` 前收成 `std::size_t`:写 `static_cast<std::size_t>(index)`,不要先转成 `int`;默认仍用 `at()`,只有选择 `[]`
|
|
13
|
+
- 已经证明非负的坐标在 `[]` 前收成 `std::size_t`:写 `static_cast<std::size_t>(index)`,不要先转成 `int`;默认仍用 `at()`,只有选择 `[]` 时才做这一步。这一步只管「已经证明合法之后取值」;判断坐标是否落在范围内时方向相反,在 `int` 域里比上界,见 `references/language/value-types/03-integer-roles.md`。
|
|
14
14
|
- 行列下标是乘加结果时,先把各操作数拓宽再乘加,不要先乘加再转换;见 `references/language/value-types/03-integer-roles.md`。
|
|
15
15
|
- `map::operator[]` 会插入,不是“读取可能缺失的键”;只读查询用 `find` / `contains` / `at`。
|
|
16
16
|
- 契约失败的消息用 `std::format`,见 `references/exceptions/01-exception-rules.md`。
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
<!-- 读取时机:查语言细节时;读完能回答:检查清单;预算:一次只开这一份(9 行),不自动串联。 -->
|
|
2
2
|
# 检查清单
|
|
3
3
|
|
|
4
|
-
- [ ]
|
|
5
|
-
- [ ]
|
|
6
|
-
- [ ] 封闭状态集合用 `enum class
|
|
7
|
-
- [ ] 没有第二种原因的缺失用 `std::optional
|
|
8
|
-
- [ ] 查询成员标了尾部 `const`;`[[nodiscard]]` 只出现在结果型 API
|
|
9
|
-
- [ ] 下标访问前有边界证明,或使用 `at
|
|
10
|
-
- [ ] 浮点比较有容差,金额没有用 `double`
|
|
4
|
+
- [ ] 参与协议、布局或跨平台边界的整数有明确宽度(`01-width.md`)。
|
|
5
|
+
- [ ] 业务编号与容器下标没有混用有符号和无符号(`02-signed.md`)。
|
|
6
|
+
- [ ] 封闭状态集合用 `enum class`,没有裸整数状态码(`04-enum.md`)。
|
|
7
|
+
- [ ] 没有第二种原因的缺失用 `std::optional`,不用哨兵枚举项(`04-enum.md`)。
|
|
8
|
+
- [ ] 查询成员标了尾部 `const`;`[[nodiscard]]` 只出现在结果型 API,结果都有分支处理(`06-const-method.md`、`07-nodiscard.md`)。
|
|
9
|
+
- [ ] 下标访问前有边界证明,或使用 `at`;乘加得到下标前先拓宽各操作数。容器、字符串与 `span` 的访问同样适用(`09-subscript.md`)。
|
|
10
|
+
- [ ] 浮点比较有容差,金额没有用 `double` 结算(`10-float-bool.md`)。
|
|
@@ -7,16 +7,16 @@
|
|
|
7
7
|
|
|
8
8
|
```text
|
|
9
9
|
<component>/
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
10
|
+
include/
|
|
11
|
+
<name>.h 公共接口,包含时写裸文件名
|
|
12
|
+
src/
|
|
13
|
+
<name>.cpp 实现,可以包含私有头
|
|
14
|
+
<detail>.h 私有头,只被本组件 src 包含
|
|
15
|
+
tests/
|
|
16
|
+
<name>_test.cpp 通过公共接口测试
|
|
17
17
|
```
|
|
18
18
|
|
|
19
|
-
- 公共头直接放 `include/` 下,包含时写裸文件名,例如 `#include "task_list.h"`;不要写 `"task_list/include/task_list.h"` 这种从仓库根起的长路径,也不要写 `../`
|
|
19
|
+
- 公共头直接放 `include/` 下,包含时写裸文件名,例如 `#include "task_list.h"`;不要写 `"task_list/include/task_list.h"` 这种从仓库根起的长路径,也不要写 `../` 相对路径。搜索路径由 target 的 `target_include_directories(... PUBLIC .../include)` 提供,写法见 `engineering` 的 `references/cmake/targets/01-target-rules.md`。
|
|
20
20
|
- 私有头放 `src/`,不进入公共 include 根;测试不得包含私有头。
|
|
21
21
|
- 一个组件一个职责;组件名用 snake_case,和命名空间一致。
|
|
22
22
|
- 组件之间通过公共头依赖;不要用 `../other/src/private.h` 跨组件包含私有实现。
|
|
@@ -25,11 +25,11 @@
|
|
|
25
25
|
|
|
26
26
|
```text
|
|
27
27
|
app/
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
include/
|
|
29
|
+
cli.h 输入输出与参数解析的私有接口
|
|
30
|
+
src/
|
|
31
|
+
cli.cpp 终端交互
|
|
32
|
+
main.cpp 只做装配:构造依赖、调用 RunGame、翻译退出码
|
|
33
33
|
```
|
|
34
34
|
|
|
35
35
|
- `main.cpp` 不写业务规则;它只解析参数、打开流、调用库、捕获边界异常。
|
|
@@ -29,17 +29,17 @@
|
|
|
29
29
|
|
|
30
30
|
槽位顺序固定:
|
|
31
31
|
|
|
32
|
-
1.
|
|
32
|
+
1. 文件头注释(格式与作者口径见 `references/comments/02-style.md`,写哪些事实见 `01-comment-rules.md`;留在 `#pragma once` 外)
|
|
33
33
|
2. `#pragma once`
|
|
34
34
|
3. 分组包含(组间空行,组内字母序)
|
|
35
|
-
4. 命名空间内:常量 → 枚举 → 结构体 → 自由函数 →
|
|
35
|
+
4. 命名空间内:常量 → 枚举 → 结构体 → 自由函数 → 类(次序与依赖冲突见 `references/layout/03-declaration-order.md`)
|
|
36
36
|
|
|
37
37
|
```cpp
|
|
38
38
|
/// @file thing_table.h
|
|
39
39
|
///
|
|
40
|
-
/// @brief `ThingTable`
|
|
40
|
+
/// @brief `ThingTable` 类型定义。
|
|
41
41
|
///
|
|
42
|
-
///
|
|
42
|
+
/// 状态 `ThingState`、表项 `Thing` 与表 `ThingTable`。
|
|
43
43
|
|
|
44
44
|
#pragma once
|
|
45
45
|
|
|
@@ -55,15 +55,15 @@ inline constexpr int kThingCount = 3;
|
|
|
55
55
|
enum class ThingState : std::uint8_t { kIdle, kBusy };
|
|
56
56
|
|
|
57
57
|
struct Thing {
|
|
58
|
-
|
|
58
|
+
int id{};
|
|
59
59
|
};
|
|
60
60
|
|
|
61
61
|
class ThingTable {
|
|
62
|
-
|
|
63
|
-
|
|
62
|
+
public:
|
|
63
|
+
void Add(const Thing& thing);
|
|
64
64
|
|
|
65
|
-
|
|
66
|
-
|
|
65
|
+
private:
|
|
66
|
+
std::vector<Thing> things_;
|
|
67
67
|
};
|
|
68
68
|
|
|
69
69
|
} // namespace project
|
|
@@ -87,7 +87,7 @@ class ThingTable {
|
|
|
87
87
|
|
|
88
88
|
```cpp
|
|
89
89
|
/// @file thing_table.cpp
|
|
90
|
-
/// @brief
|
|
90
|
+
/// @brief `ThingTable` 成员函数实现。
|
|
91
91
|
|
|
92
92
|
#include "thing.h"
|
|
93
93
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
<!-- 读取时机:排声明顺序时;读完能回答:声明顺序与定义签名;预算:一次只开这一份(
|
|
1
|
+
<!-- 读取时机:排声明顺序时;读完能回答:声明顺序与定义签名;预算:一次只开这一份(198 行),不自动串联。 -->
|
|
2
2
|
# 声明顺序
|
|
3
3
|
|
|
4
4
|
顺序的作用是让读者先看类型和契约,再看数据。`clang-format` 不管顺序,靠人工和 review 维持。
|
|
@@ -7,36 +7,36 @@
|
|
|
7
7
|
|
|
8
8
|
```cpp
|
|
9
9
|
class TaskTable {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
10
|
+
public:
|
|
11
|
+
// 1. 类型与别名
|
|
12
|
+
using Tasks = std::array<TaskState, 16>;
|
|
13
|
+
|
|
14
|
+
// 2. 静态常量
|
|
15
|
+
static constexpr int kTaskCount = 16;
|
|
16
|
+
|
|
17
|
+
// 3. 工厂
|
|
18
|
+
static TaskTable CreateEmpty();
|
|
19
|
+
|
|
20
|
+
// 4. 构造、赋值、析构
|
|
21
|
+
TaskTable();
|
|
22
|
+
TaskTable(const TaskTable&) = default;
|
|
23
|
+
TaskTable& operator=(const TaskTable&) = default;
|
|
24
|
+
~TaskTable();
|
|
25
|
+
|
|
26
|
+
// 5. 其他成员函数
|
|
27
|
+
[[nodiscard]] TaskState GetTaskState(int id) const;
|
|
28
|
+
void SetTaskState(int id, TaskState state);
|
|
29
|
+
[[nodiscard]] bool HasTask(int id) const;
|
|
30
|
+
|
|
31
|
+
// 6. 数据成员
|
|
32
|
+
private:
|
|
33
|
+
Tasks tasks_{};
|
|
34
|
+
bool is_open_{false};
|
|
35
35
|
};
|
|
36
36
|
```
|
|
37
37
|
|
|
38
38
|
- `struct` 的数据成员紧跟类型之后;有 invariant 时改用 `class`。
|
|
39
|
-
- `public:` 在前,`private:` 在后;`private:`
|
|
39
|
+
- `public:` 在前,`private:` 在后;`private:` 前空一行,类的数据成员之间不空行,`struct` 的短字段可以空行分组(见 `references/comments/01-comment-rules.md`)。
|
|
40
40
|
- 静态常量用 `static constexpr`;跨文件共享的常量放命名空间并 `inline constexpr`。
|
|
41
41
|
- 工厂放在构造函数之前;返回 `TaskTable` 的工厂用 `Create` 开头,名字说明创建的是什么状态。
|
|
42
42
|
- 用户声明的特殊成员函数集中放第 4 组;`= default` 也写出来,读者才知道是故意默认。
|
|
@@ -58,15 +58,41 @@ Google、LLVM、OpenCV 都没有规定这一组内部的次序;本工作流补
|
|
|
58
58
|
- 有比较运算符时,开头是 `operator==`:成员默认比较和 `friend` 都放这里,然后按属性/概念分组:同一属性的 `Get`/`Set` 成对相邻,`Is` / `Has` / `Can` 放在这对之后;再放普通函数,按使用先后。不要把谓词插进 `Get`/`Set` 之间,也不要把全部谓词提前到全部 `Get` 之前。
|
|
59
59
|
- `Count` / `Size` 是集合摘要查询,放在该概念组的直接 `Get` 之前;它不属于 `Is` / `Has` / `Can` 谓词组,也不属于 `Set` / 修改器组。
|
|
60
60
|
- 不属于 `Is` / `Get` / `Set` 的流程函数按调用层次排:入口操作在前,它调用的显示 helper 紧随其后;`DisplayOptions`、`Note` 等共享 helper 放在调用它们的流程之后;终端进入、退出与清屏可集中在组首,按进入、退出、清屏排。
|
|
61
|
-
- `
|
|
62
|
-
- 没有 `Set` 配对时,谓词放在读取接口之后、修改接口之前。例:`CountOptions`、`GetOptions`、`
|
|
61
|
+
- `IsInRange` / `CheckInRange` 这类校验对不参与 `Get`/`Set` 分组,成对放在静态常量与构造函数之间,见下节「校验对的位置」;`CheckInRange` 是诊断函数,紧挨 `IsInRange` 写,见 `references/exceptions/01-exception-rules.md`。
|
|
62
|
+
- 没有 `Set` 配对时,谓词放在读取接口之后、修改接口之前。例:`CountOptions`、`GetOptions`、`GetCursorIndex` 之后是 `HasCursor`,再是 `MoveTo` / `MovePrevious` / `MoveNext`;这组接口可以放在 `private:`,组内次序不变。
|
|
63
63
|
- 多个直接返回成员的 `Get` 按对应数据成员的声明顺序排列,不按调用频率或字母序:成员依次是 `current_player_`、`board_`、`history_`,getter 就依次是 `GetCurrentPlayer()`、`GetBoard()`、`GetHistory()`;成员顺序调整时 getter 一起改,一个类里只留一套顺序。例外:`Count` / `Size` 是集合摘要,优先于该集合的直接 `Get`;其他没有独立成员的派生查询排在直接 `Get` 之后,按「先局部元素、后整体状态」阅读,不插进直接 `Get` 之间。例:`Game` 的 `GetResult()` 扫描棋盘后才得到结果,排在直接返回成员的 `GetCurrentPlayer()`、`GetBoard()`、`GetHistory()` 之后。
|
|
64
|
-
- 成对的局部/整体操作沿用同一方向:先 `ClearSquare(
|
|
64
|
+
- 成对的局部/整体操作沿用同一方向:先 `ClearSquare(position)` 清一格,再 `Clear()` 清整盘;局部操作在前,整体操作在后,不按调用频率互换。
|
|
65
65
|
- `constexpr` / `consteval` 函数不单独提前:它们属于「其他函数」组,按上面的语义次序排。`constexpr` 与 `consteval` 隐含 `inline`,这才使头文件就地定义可行;它们不决定声明位置;就地定义的短函数也留在该组,不因为写在头文件里就挪到类的最前。
|
|
66
|
-
- 比较运算符的签名与 `[[nodiscard]]` 规则见 `references/language/value-types
|
|
66
|
+
- 比较运算符的签名与 `[[nodiscard]]` 规则见 `references/language/value-types/08-comparison.md`,这里不另写一套。
|
|
67
67
|
- 同一类型上的谓词留在类内,不因为实现只转调公开函数就拆成类外自由函数。
|
|
68
68
|
- `friend` 类型仍跟其他类型放在一起,和 `friend` 函数不是同一组。
|
|
69
69
|
|
|
70
|
+
### 校验对的位置
|
|
71
|
+
|
|
72
|
+
坐标、下标、句柄这类输入合法性判断由一对函数承载:`Is*` 回答「在不在范围内」,`Check*` 在越界时抛出。这一对放在静态常量(第 2 组)与工厂(第 3 组)之后、构造/赋值/析构(第 4 组)之前;没有工厂时紧跟静态常量。
|
|
73
|
+
|
|
74
|
+
```cpp
|
|
75
|
+
class Board {
|
|
76
|
+
public:
|
|
77
|
+
static constexpr std::size_t kRows = 3;
|
|
78
|
+
static constexpr std::size_t kColumns = kRows;
|
|
79
|
+
|
|
80
|
+
[[nodiscard]] static bool IsInRange(const Position& position) noexcept;
|
|
81
|
+
static void CheckInRange(const Position& position);
|
|
82
|
+
|
|
83
|
+
Board() = default;
|
|
84
|
+
|
|
85
|
+
[[nodiscard]] SquareValue GetSquareValue(const Position& position) const;
|
|
86
|
+
void SetSquareValue(const Position& position, SquareValue value);
|
|
87
|
+
[[nodiscard]] bool IsSquareEmpty(const Position& position) const;
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
- 读者从上往下读到的是「常量 → 契约 → 生命周期 → 操作」:默认构造函数之前,棋盘有多宽、坐标怎样算合法已经说过一遍;`GetSquareValue` / `SetSquareValue` / `IsSquareEmpty` 的 `@throws std::out_of_range` 也不再是突然出现的新信息。
|
|
91
|
+
- 条件:成对出现、都是 `static`、命名为 `Is*` 与 `Check*`,只做「合法吗」与「不合法就抛」;夹带业务判断的函数不适用。同一个工程里所有类的校验对放同一个位置,不一处前置、一处留在第 5 组。
|
|
92
|
+
- 这是本工作流对 Google 与 NL.16 的**显式偏离**:两者都把 `static` 成员函数归入「其他函数」,排在构造函数之后。偏离只限这一对,其他 `static` 函数仍留在第 5 组;代价是审查者要认这条本地规则。
|
|
93
|
+
- `.cpp` 的定义同样前置,与头文件同序;`GetSquareValue` / `SetSquareValue` / `IsSquareEmpty` 里的 `CheckInRange(position);` 出现在定义之后,C++ 允许先调用后定义。
|
|
94
|
+
- `header-order` 只检查非 `static` 的 `Is` / `Has` / `Can` / `Completes` / `Would` 谓词,`Check*` 也不在谓词表内,所以这个位置没有 rule 兜底,靠人工核对。
|
|
95
|
+
|
|
70
96
|
## 命名空间作用域的头文件
|
|
71
97
|
|
|
72
98
|
文件槽位与 `#pragma once` 的写法以 `references/layout/02-file-template.md` 为准;本节只看命名空间作用域内的排列顺序。
|
|
@@ -82,21 +108,21 @@ namespace taskflow {
|
|
|
82
108
|
|
|
83
109
|
inline constexpr int kTaskCount = 16;
|
|
84
110
|
|
|
85
|
-
enum class TaskState : std::uint8_t { kPending
|
|
111
|
+
enum class TaskState : std::uint8_t { kPending, kActive, kDone };
|
|
86
112
|
|
|
87
113
|
struct TaskRecord {
|
|
88
|
-
|
|
89
|
-
|
|
114
|
+
int id{0};
|
|
115
|
+
TaskState state{TaskState::kPending};
|
|
90
116
|
};
|
|
91
117
|
|
|
92
118
|
[[nodiscard]] bool HasTask(int id);
|
|
93
119
|
[[nodiscard]] std::string TaskStateToString(TaskState state);
|
|
94
120
|
|
|
95
121
|
class TaskTable {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
122
|
+
public:
|
|
123
|
+
// 类内仍按上面的六组顺序
|
|
124
|
+
private:
|
|
125
|
+
std::array<TaskState, kTaskCount> tasks_{};
|
|
100
126
|
};
|
|
101
127
|
|
|
102
128
|
} // namespace taskflow
|
|
@@ -108,7 +134,7 @@ class TaskTable {
|
|
|
108
134
|
|
|
109
135
|
- 本工作流约定:与头文件声明同序,先类型和常量,再自由函数,再成员函数;成员函数按类内声明顺序。Google、LLVM 与 C++ Core Guidelines 都没有规定 `.cpp` 成员定义的次序;这里选同序,是为了让读者能在头文件与实现之间逐项对照。
|
|
110
136
|
- 文件头注释之后先放本文件的 `namespace taskflow {`,再放公共定义;常量仍排在具名命名空间内、成员函数之前;内部链接的写法按 `references/style/01-google-cpp-style.md`,本文不复述。
|
|
111
|
-
-
|
|
137
|
+
- 构造函数、析构函数、拷贝/移动定义通常放在成员函数组最前;如果头文件把某个成员声明在构造函数之前,仍按头文件声明顺序,不把构造函数硬提到前面。校验对就是这种情况:`IsInRange`、`CheckInRange` 定义在 `GetSquareValue` 之前,跟头文件一致。`= default` 的定义放 `.cpp` 时紧跟声明顺序。
|
|
112
138
|
- 不要为了「先编译」重排定义;C++ 不要求定义顺序,读者顺序优先。
|
|
113
139
|
|
|
114
140
|
## 声明与定义的签名
|
|
@@ -119,13 +145,27 @@ class TaskTable {
|
|
|
119
145
|
- 类内没有声明的成员定义报 `definition-undeclared`;参数类型、限定符或返回类型不一致报 `definition-signature`。模板与无法解析的声明按保守规则跳过;`auto` 返回类型不比较具体类型。
|
|
120
146
|
- 检查器检查配对头文件里的类:类成员的声明顺序与 `.cpp` 定义签名;自由函数、匿名命名空间与没有配对头文件的 `.cpp` 不在覆盖内,它们靠人工核对。
|
|
121
147
|
|
|
148
|
+
### 检查器覆盖
|
|
149
|
+
|
|
150
|
+
`checking-declaration-order.mjs` 对配对头文件里的类报告这些 rule:
|
|
151
|
+
|
|
152
|
+
| rule | 报什么 |
|
|
153
|
+
| --- | --- |
|
|
154
|
+
| `definition-undeclared` | `.cpp` 里定义了类内没有声明的成员 |
|
|
155
|
+
| `definition-signature` | 定义的参数类型、`const`/`volatile`/`noexcept`、引用限定符或返回类型与声明不一致 |
|
|
156
|
+
| `definition-order` | 成员定义顺序与头文件声明顺序不同 |
|
|
157
|
+
| `header-order` | 谓词排在读取接口之前、Get/Set 之间或修改接口之后;`static` 谓词与 `Check*` 不检查 |
|
|
158
|
+
| `header-ambiguous` | 同名头文件不唯一,无法配对 |
|
|
159
|
+
|
|
160
|
+
其余顺序规则——六个分组的先后、校验对的位置、空行、访问说明符位置、`= default` 的位置——没有 rule,靠人工核对。
|
|
161
|
+
|
|
122
162
|
## 空行
|
|
123
163
|
|
|
124
164
|
| 位置 | 空行 |
|
|
125
165
|
| --- | --- |
|
|
126
166
|
| 类内六个组之间 | 1 行 |
|
|
127
167
|
| 同组函数之间 | 1 行 |
|
|
128
|
-
|
|
|
168
|
+
| 类的数据成员之间;`struct` 的短字段可以空行分组 | 0 行 |
|
|
129
169
|
| `private:` 之前 | 1 行 |
|
|
130
170
|
| 命名空间内顶层实体之间 | 1 行 |
|
|
131
171
|
| 函数体内逻辑组之间 | 1 行,最多 1 行 |
|
|
@@ -145,8 +185,8 @@ class TaskTable {
|
|
|
145
185
|
### 头文件里的空行
|
|
146
186
|
|
|
147
187
|
- 类内六个组之间、同组函数之间、命名空间内顶层实体之间各空 1 行。
|
|
148
|
-
-
|
|
149
|
-
-
|
|
188
|
+
- 类的数据成员之间不空行,`struct` 的短字段可以空行分组;`private:` 之前空 1 行。
|
|
189
|
+
- 注释与声明之间不留空行,写法见 `references/comments/02-style.md`。
|
|
150
190
|
- Doxygen 列表项之间不插空注释行。
|
|
151
191
|
|
|
152
192
|
## 依据
|
|
@@ -156,3 +196,4 @@ class TaskTable {
|
|
|
156
196
|
- [C++ Core Guidelines: NL.16](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#rl-order):类型、构造/赋值/析构、函数、数据成员的传统次序。
|
|
157
197
|
- [cppreference: 构造函数](https://en.cppreference.com/w/cpp/language/constructor):非静态数据成员按声明顺序初始化,与 mem-initializer 列表顺序无关。
|
|
158
198
|
- [LLVM Coding Standards](https://llvm.org/docs/CodingStandards.html):只要求每个 `.cpp` 先包含定义其接口的头文件,没有规定成员定义的先后;上一条「与头文件声明同序」因此是本工作流约定,不冒充外部标准。
|
|
199
|
+
- 本工作流在「校验对的位置」上偏离第一条与第三条:把 `static` 的 `Is*` / `Check*` 校验对提到构造函数之前,其余 `static` 函数仍按上两条放在构造函数之后;偏离理由与边界见该节。
|