@chengzhao-dev/dsh-cpp-workflow 0.7.6 → 0.7.8
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 -8
- package/README.md +10 -10
- package/lib/client.js +7 -3
- package/lib/comment-policy.js +7 -3
- package/lib/index.js +2 -1
- package/lib/workspace.js +17 -3
- package/package.json +1 -1
- package/skills/building-cpp/SKILL.md +6 -6
- package/skills/building-cpp/assets/config/.clang-format +8 -8
- package/skills/building-cpp/assets/config/.clang-tidy +0 -4
- package/skills/building-cpp/assets/config/.vscode/settings.json +11 -0
- package/skills/building-cpp/assets/templates/projects/common/running-build.sh +50 -43
- package/skills/building-cpp/assets/templates/projects/multi/CMakeLists.txt +6 -10
- package/skills/building-cpp/assets/templates/projects/multi/include/greeting.h +1 -4
- package/skills/building-cpp/assets/templates/projects/multi/src/greeting.cpp +2 -2
- package/skills/building-cpp/assets/templates/projects/multi/src/main.cpp +2 -2
- package/skills/building-cpp/assets/templates/projects/shared-library/CMakeLists.txt +6 -10
- package/skills/building-cpp/assets/templates/projects/shared-library/greeting/CMakeLists.txt +3 -10
- package/skills/building-cpp/assets/templates/projects/shared-library/greeting/include/greeting.h +1 -4
- package/skills/building-cpp/assets/templates/projects/shared-library/greeting/src/greeting.cpp +2 -2
- package/skills/building-cpp/assets/templates/projects/shared-library/src/main.cpp +2 -2
- package/skills/building-cpp/assets/templates/projects/single/CMakeLists.txt +4 -8
- package/skills/building-cpp/assets/templates/projects/single/src/main.cpp +4 -4
- package/skills/building-cpp/assets/templates/projects/static-library/CMakeLists.txt +4 -8
- package/skills/building-cpp/assets/templates/projects/static-library/greeting/CMakeLists.txt +3 -10
- package/skills/building-cpp/assets/templates/projects/static-library/greeting/include/greeting.h +1 -4
- package/skills/building-cpp/assets/templates/projects/static-library/greeting/src/greeting.cpp +2 -2
- package/skills/building-cpp/assets/templates/projects/static-library/src/main.cpp +2 -2
- package/skills/building-cpp/references/01-build-script.md +8 -23
- package/skills/building-cpp/references/02-environment.md +9 -8
- package/skills/building-cpp/scripts/checking-environment.mjs +45 -13
- package/skills/building-cpp/steps/01-build.md +5 -4
- package/skills/building-cpp/steps/02-tooling.md +6 -6
- package/skills/building-cpp/steps/03-check.md +8 -6
- package/skills/building-cpp/steps/04-vscode.md +4 -4
- package/skills/developing-cpp/SKILL.md +4 -5
- package/skills/developing-cpp/references/comments/01-comment-rules.md +6 -6
- package/skills/developing-cpp/references/comments/02-style.md +20 -7
- package/skills/developing-cpp/references/comments/examples/01-declaration-class.md +2 -2
- package/skills/developing-cpp/references/comments/examples/04-declaration-file.md +5 -3
- package/skills/developing-cpp/references/comments/examples/05-declaration-function.md +1 -1
- package/skills/developing-cpp/references/comments/glossary/01-comment-terms.md +5 -5
- package/skills/developing-cpp/references/design/01-project-design.md +1 -1
- package/skills/developing-cpp/references/design/session-layer.md +10 -10
- package/skills/developing-cpp/references/exceptions/01-exception-rules.md +1 -1
- package/skills/developing-cpp/references/index.md +12 -11
- package/skills/developing-cpp/references/language/glossary/01-language-terms.md +2 -2
- package/skills/developing-cpp/references/language/language-basics/02-initialization.md +2 -2
- package/skills/developing-cpp/references/language/language-basics/05-expressions.md +2 -2
- package/skills/developing-cpp/references/language/language-basics/06-functions.md +4 -3
- package/skills/developing-cpp/references/language/language-basics/07-classes.md +1 -1
- package/skills/developing-cpp/references/language/performance/02-complexity.md +2 -3
- package/skills/developing-cpp/references/language/performance/03-memory.md +1 -1
- package/skills/developing-cpp/references/language/performance/05-pitfalls.md +1 -1
- package/skills/developing-cpp/references/language/standard-library/04-algorithms.md +1 -1
- package/skills/developing-cpp/references/language/standard-library/06-format.md +1 -31
- package/skills/developing-cpp/references/language/standard-library/11-format-ecosystem.md +32 -0
- package/skills/developing-cpp/references/language/value-types/02-signed.md +2 -2
- package/skills/developing-cpp/references/language/value-types/03-integer-roles.md +3 -2
- package/skills/developing-cpp/references/language/value-types/04-enum.md +4 -4
- package/skills/developing-cpp/references/layout/01-source-layout.md +1 -1
- package/skills/developing-cpp/references/layout/02-file-template.md +30 -43
- package/skills/developing-cpp/references/layout/03-declaration-order.md +8 -10
- package/skills/developing-cpp/references/naming/01-identifier-names.md +9 -3
- package/skills/developing-cpp/references/naming/glossary/04-square.md +1 -1
- package/skills/developing-cpp/references/style/01-google-cpp-style.md +10 -10
- package/skills/developing-cpp/scripts/checking-comments.mjs +33 -10
- package/skills/developing-cpp/scripts/checking-declaration-order.mjs +77 -10
- package/skills/developing-cpp/steps/03-declaration.md +5 -5
- package/skills/developing-cpp/steps/04-implementation.md +3 -3
- package/skills/engineering/SKILL.md +7 -6
- package/skills/engineering/references/cmake/assets/CompileSettings.cmake +100 -100
- package/skills/engineering/references/cmake/assets/LinkSettings.cmake +25 -25
- package/skills/engineering/references/cmake/assets/OutputSettings.cmake +38 -0
- package/skills/engineering/references/cmake/linking/01-libraries.md +1 -1
- package/skills/engineering/references/cmake/modules/01-functions.md +11 -10
- package/skills/engineering/references/cmake/standards/c++11/cmake-snippet.cmake +3 -3
- package/skills/engineering/references/cmake/standards/c++14/cmake-snippet.cmake +3 -3
- package/skills/engineering/references/cmake/standards/c++17/cmake-snippet.cmake +3 -3
- package/skills/engineering/references/cmake/standards/c++20/cmake-snippet.cmake +3 -3
- package/skills/engineering/references/cmake/standards/c++23/cmake-snippet.cmake +3 -3
- package/skills/engineering/references/cmake/standards/index.md +6 -6
- package/skills/engineering/references/cmake/standards/versions.md +1 -1
- package/skills/engineering/references/cmake/targets/01-target-rules.md +8 -18
- package/skills/engineering/references/formatting/01-encoding.md +6 -3
- package/skills/engineering/references/formatting/02-clang-format.md +3 -3
- package/skills/engineering/references/formatting/03-clang-tidy.md +2 -2
- package/skills/engineering/references/formatting/04-clangd.md +4 -4
- package/skills/engineering/references/formatting/examples/01-clang-format.md +2 -2
- package/skills/engineering/references/formatting/examples/04-config-comments.md +43 -0
- package/skills/engineering/references/index.md +4 -3
- package/skills/engineering/scripts/checking-encoding.mjs +2 -1
- package/skills/engineering/steps/01-targets.md +3 -3
- package/skills/engineering/steps/02-modules.md +3 -9
- package/skills/engineering/steps/03-check.md +1 -1
- package/skills/shared/operations.md +1 -1
- package/skills/shared/rule-priority.md +1 -1
- package/templates/cpp-workflow-project/SKILL.md +5 -3
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
<!-- 读取时机:查语言细节时;读完能回答:`std::format` 与输出;预算:一次只开这一份(
|
|
1
|
+
<!-- 读取时机:查语言细节时;读完能回答:`std::format` 与输出;预算:一次只开这一份(86 行),不自动串联。 -->
|
|
2
2
|
# `std::format` 与输出
|
|
3
3
|
|
|
4
4
|
- 组合消息优先 `std::format`;它返回 `std::string`,不直接写流。
|
|
@@ -85,33 +85,3 @@ return std::format_to(ctx.out(), "Player({:d})", std::to_underlying(player));
|
|
|
85
85
|
return std::format_to(ctx.out(), "Player({:d})",
|
|
86
86
|
static_cast<std::underlying_type_t<game::Player>>(player));
|
|
87
87
|
```
|
|
88
|
-
|
|
89
|
-
## 标准、提案与生态
|
|
90
|
-
|
|
91
|
-
- C++20 引入 `std::format` 与 `std::formatter`;标准库为字符、字符串、整数、浮点、指针与 `nullptr` 等提供 enabled 特化,没有枚举。`formatter<T>` 默认 disabled;自定义类型必须特化。
|
|
92
|
-
- C++23 增加 `std::to_underlying`(`<utility>`,等价 `static_cast<std::underlying_type_t<Enum>>`);字符串与字符的格式说明增加 `?` 调试表示;仍然没有枚举的内建 formatter。
|
|
93
|
-
- P3070R2 提议 `format_as` 作为枚举的 ADL 扩展点:`auto format_as(film f) { return std::to_underlying(f); }`,也可返回 `std::string_view`;在 type erasure 前转换,提案给出的测量约为 2× 快。尚未进入标准;fmt 已支持。
|
|
94
|
-
- P4267R0 用 C++26 reflection/annotations 提议 `[[=std::format_as_underlying]]` 与 `[[=std::format_as_identifier]]`,替代 `format_as`;尚未进入标准。
|
|
95
|
-
- fmt:`format_as` 或特化 `formatter`;`fmt::underlying(enum)`;推荐复用已有 formatter(继承/组合)。
|
|
96
|
-
- Abseil `StrFormat`:`%d`/`%i` 直接接受枚举,`%v` 用 `AbslStringify`。
|
|
97
|
-
- LLVM `formatv`:自定义用 `format_provider<T>` 特化;文档没有给出枚举专用入口。
|
|
98
|
-
- 结论:C++23 项目继续用 `std::formatter` 特化;数值回退优先 `static_cast<int>`(值域小)或 `std::to_underlying` + `{:d}`(值域大或底层不定);不要为枚举继承 `formatter<std::string_view>`。
|
|
99
|
-
|
|
100
|
-
## fmt 与 `std::format` 的关系
|
|
101
|
-
|
|
102
|
-
- `std::format` 的接口来自 fmt 作者主导的 P0645,格式串语法与格式化语义同源;读 fmt 文档能直接解释标准库的行为。
|
|
103
|
-
- fmt 是超集:C++23 之前就提供 `print`/`println`、`memory_buffer`、命名参数与运行期格式串;标准库到 C++23 才补上 `std::print`,其余仍没有。
|
|
104
|
-
- 本项目是 C++23,clang 21 与 libc++ 21 的 `std::format` 已可用,不引入第三方依赖;fmt 只作为参考实现出现在阅读清单里。
|
|
105
|
-
- 只有工具链停在 C++20 及更早、或确实要用 fmt 专有接口时才换成 `fmt::format`。
|
|
106
|
-
- 换 fmt 要同时改构建(`target_link_libraries` 或 FetchContent)与编译数据库;只放头文件不接构建,clangd 仍会报未声明。
|
|
107
|
-
|
|
108
|
-
## 是否继承 `std::formatter<std::string_view>`
|
|
109
|
-
|
|
110
|
-
- 不需要。启用的特化只要满足 BasicFormatter:本身可默认构造、拷贝、赋值、析构、交换,并提供 `parse` 与 `format`;标准不要求继承任何 formatter。
|
|
111
|
-
- 继承或组合标准 formatter 的用途是复用格式说明符的解析。输出本质是字符串、并且确实要支持 `{:>10}`、`{:.3}` 这类字符串语法时才值得;`Position` 是最底层的坐标类型,把两个整数拼成 `(row, col)`,输出不是字符串值,继承 `formatter<std::string_view>` 只会让字符串说明符被接受,而 `format` 仍要自己拼文本。
|
|
112
|
-
- `Player` 与 `Move` 也不需要继承:`Player` 输出 `X`/`O` 符号,继承 `formatter<int>` 只会放宽可接受的说明符,输出仍由本类的 `format` 决定;`Move` 用 `"{}{}"` 组合 `Player` 与 `Position` 的 formatter。
|
|
113
|
-
- 现实中继承也确实在用:fmt 文档的 `formatter<color> : formatter<string_view>` 让 `parse` 转调基类、`format` 复用基类输出,只用于「输出是字符串」的类型,不是通用模板。
|
|
114
|
-
- 需要复用时,组合与继承都能工作。组合把 `std::formatter<std::string_view> base;` 作为成员,`parse` 转调 `base.parse(ctx)`,`format` 先拼临时字符串再 `base.format(temp, ctx)`,不改变本类型的继承关系;标准草案 [format.formatter.spec] 的示例则直接继承 `formatter<const char*>`。继承标准库类型只当薄适配层,不把它当多态基类。
|
|
115
|
-
- `format` 写成 `const`;LWG 3636 之后这是要求。
|
|
116
|
-
|
|
117
|
-
依据:[cppreference: 标准格式说明](https://en.cppreference.com/w/cpp/utility/format/spec)、[cppreference: std::formatter](https://en.cppreference.com/w/cpp/utility/format/formatter)、[cppreference: BasicFormatter](https://en.cppreference.com/w/cpp/named_req/BasicFormatter)、[formatter.requirements](https://eel.is/c++draft/formatter.requirements)、[format.parse.ctx](https://eel.is/c++draft/format.parse.ctx)、[format.context](https://eel.is/c++draft/format.context)、[format.formatter.spec](https://eel.is/c++draft/format.formatter.spec)、[std::to_underlying](https://en.cppreference.com/w/cpp/utility/to_underlying)、[Google Casting](https://google.github.io/styleguide/cppguide.html#Casting)、[P3070R2](https://wg21.link/P3070R2)、[P4267R0](https://wg21.link/P4267R0)、[fmt API](https://fmt.dev/latest/api/#formatting-user-defined-types)、[Abseil StrFormat](https://abseil.io/docs/cpp/guides/format)、[LLVM formatv](https://llvm.org/docs/ProgrammersManual.html#formatting-strings)、[std::basic_format_string](https://en.cppreference.com/w/cpp/utility/format/basic_format_string)、[LWG 3636](https://cplusplus.github.io/LWG/issue3636)。
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
<!-- 读取时机:查语言细节时;读完能回答:std::format 的生态、提案与 fmt 关系;预算:一次只开这一份(31 行),不自动串联。 -->
|
|
2
|
+
# 格式化的标准与生态
|
|
3
|
+
|
|
4
|
+
## 标准与提案
|
|
5
|
+
|
|
6
|
+
- C++20 引入 `std::format` 与 `std::formatter`;标准库为字符、字符串、整数、浮点、指针与 `nullptr` 等提供 enabled 特化,没有枚举。`formatter<T>` 默认 disabled;自定义类型必须特化。
|
|
7
|
+
- C++23 增加 `std::to_underlying`(`<utility>`,等价 `static_cast<std::underlying_type_t<Enum>>`);字符串与字符的格式说明增加 `?` 调试表示;仍然没有枚举的内建 formatter。
|
|
8
|
+
- P3070R2 提议 `format_as` 作为枚举的 ADL 扩展点:`auto format_as(film f) { return std::to_underlying(f); }`,也可返回 `std::string_view`;在 type erasure 前转换,提案给出的测量约为 2× 快。尚未进入标准;fmt 已支持。
|
|
9
|
+
- P4267R0 用 C++26 reflection/annotations 提议 `[[=std::format_as_underlying]]` 与 `[[=std::format_as_identifier]]`,替代 `format_as`;尚未进入标准。
|
|
10
|
+
- fmt:`format_as` 或特化 `formatter`;`fmt::underlying(enum)`;推荐复用已有 formatter(继承/组合)。
|
|
11
|
+
- Abseil `StrFormat`:`%d`/`%i` 直接接受枚举,`%v` 用 `AbslStringify`。
|
|
12
|
+
- LLVM `formatv`:自定义用 `format_provider<T>` 特化;文档没有给出枚举专用入口。
|
|
13
|
+
- 结论:C++23 项目继续用 `std::formatter` 特化;数值回退优先 `static_cast<int>`(值域小)或 `std::to_underlying` + `{:d}`(值域大或底层不定);不要为枚举继承 `formatter<std::string_view>`。
|
|
14
|
+
|
|
15
|
+
## fmt 与 `std::format` 的关系
|
|
16
|
+
|
|
17
|
+
- `std::format` 的接口来自 fmt 作者主导的 P0645,格式串语法与格式化语义同源;读 fmt 文档能直接解释标准库的行为。
|
|
18
|
+
- fmt 是超集:C++23 之前就提供 `print`/`println`、`memory_buffer`、命名参数与运行期格式串;标准库到 C++23 才补上 `std::print`,其余仍没有。
|
|
19
|
+
- 环境声明的标准是 C++23、工具链的 `std::format` 可用时,不引入第三方依赖;fmt 只作为参考实现出现在阅读清单里。
|
|
20
|
+
- 只有工具链停在 C++20 及更早、或确实要用 fmt 专有接口时才换成 `fmt::format`。
|
|
21
|
+
- 换 fmt 要同时改构建(`target_link_libraries` 或 FetchContent)与编译数据库;只放头文件不接构建,clangd 仍会报未声明。
|
|
22
|
+
|
|
23
|
+
## 是否继承 `std::formatter<std::string_view>`
|
|
24
|
+
|
|
25
|
+
- 不需要。启用的特化只要满足 BasicFormatter:本身可默认构造、拷贝、赋值、析构、交换,并提供 `parse` 与 `format`;标准不要求继承任何 formatter。
|
|
26
|
+
- 继承或组合标准 formatter 的用途是复用格式说明符的解析。输出本质是字符串、并且确实要支持 `{:>10}`、`{:.3}` 这类字符串语法时才值得;`Position` 是最底层的坐标类型,把两个整数拼成 `(row, col)`,输出不是字符串值,继承 `formatter<std::string_view>` 只会让字符串说明符被接受,而 `format` 仍要自己拼文本。
|
|
27
|
+
- `Player` 与 `Move` 也不需要继承:`Player` 输出 `X`/`O` 符号,继承 `formatter<int>` 只会放宽可接受的说明符,输出仍由本类的 `format` 决定;`Move` 用 `"{}{}"` 组合 `Player` 与 `Position` 的 formatter。
|
|
28
|
+
- 现实中继承也确实在用:fmt 文档的 `formatter<color> : formatter<string_view>` 让 `parse` 转调基类、`format` 复用基类输出,只用于「输出是字符串」的类型,不是通用模板。
|
|
29
|
+
- 需要复用时,组合与继承都能工作。组合把 `std::formatter<std::string_view> base;` 作为成员,`parse` 转调 `base.parse(ctx)`,`format` 先拼临时字符串再 `base.format(temp, ctx)`,不改变本类型的继承关系;标准草案 [format.formatter.spec] 的示例则直接继承 `formatter<const char*>`。继承标准库类型只当薄适配层,不把它当多态基类。
|
|
30
|
+
- `format` 写成 `const`;LWG 3636 之后这是要求。
|
|
31
|
+
|
|
32
|
+
依据:[P3070R2](https://wg21.link/P3070R2)、[P4267R0](https://wg21.link/P4267R0)、[fmt API](https://fmt.dev/latest/api/#formatting-user-defined-types)、[Abseil StrFormat](https://abseil.io/docs/cpp/guides/format)、[LLVM formatv](https://llvm.org/docs/ProgrammersManual.html#formatting-strings)。
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
|
|
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` 会报。
|
|
@@ -18,7 +18,7 @@ int CountWide(const std::vector<int>& values) {
|
|
|
18
18
|
}
|
|
19
19
|
```
|
|
20
20
|
|
|
21
|
-
循环下标与容器大小都用 `std::size_t
|
|
21
|
+
循环下标与容器大小都用 `std::size_t`(角色表见 `references/language/value-types/03-integer-roles.md`),避免混合比较。业务编号与坐标另按下面的约定。
|
|
22
22
|
|
|
23
23
|
## 依据
|
|
24
24
|
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
<!--
|
|
2
|
-
#
|
|
1
|
+
<!-- 读取时机:查语言细节时;读完能回答:整型角色的通用约定;预算:一次只开这一份(39 行),不自动串联。 -->
|
|
2
|
+
# 整型的角色与选型
|
|
3
3
|
|
|
4
4
|
| 角色 | 类型 | 理由 |
|
|
5
5
|
| --- | --- | --- |
|
|
6
6
|
| 不能为负的尺寸、容器长度、`std::array` 长度 | `std::size_t` | 与 STL 和 NTTP 一致 |
|
|
7
|
+
| 遍历标准容器的下标 | `std::size_t` | 与 `size()` 同类型,避免混合比较 |
|
|
7
8
|
| 坐标等允许表示范围外取值的下标 | `int` | 能表达负值;越界判断交给持有尺寸的类型 |
|
|
8
9
|
| 没有上面两种理由的整数 | `int` | 宽度足够,写起来最短 |
|
|
9
10
|
| 取值很少的 `enum class` 的底层类型 | `std::uint8_t` | 只表达取值集合和布局,不参与算术 |
|
|
@@ -19,11 +19,11 @@
|
|
|
19
19
|
|
|
20
20
|
## 什么时候不用枚举
|
|
21
21
|
|
|
22
|
-
- 每个取值都真实存在,调用方要 `switch`,成功本身不另带数据:`enum class
|
|
23
|
-
- 要么有一个 `T`,要么没有,没有值不需要第二种原因:`std::optional<T
|
|
24
|
-
- 成功带 `T`,失败带调用方要读的原因:C++23 用 `std::expected<T, E>`,`E` 仍可以是 `enum class
|
|
22
|
+
- 每个取值都真实存在,调用方要 `switch`,成功本身不另带数据:`enum class`。状态、模式与结果类型留在这里。
|
|
23
|
+
- 要么有一个 `T`,要么没有,没有值不需要第二种原因:`std::optional<T>`。需要「有值 / 无值」的模式或选项返回 `optional<Mode>`。不为「没有」再造 `kEnd`、`kNone`,也不写枚举加出参。
|
|
24
|
+
- 成功带 `T`,失败带调用方要读的原因:C++23 用 `std::expected<T, E>`,`E` 仍可以是 `enum class`。成功没有额外载荷时继续用枚举,不改成 `expected`。
|
|
25
25
|
- 一次交出多份互相独立的数据:具名 `struct`。
|
|
26
|
-
- 只有是否,没有载荷,两种含义写在 `@return`:`bool
|
|
26
|
+
- 只有是否,没有载荷,两种含义写在 `@return`:`bool`。
|
|
27
27
|
- 函数完不成自己的合同:沿用异常规则。
|
|
28
28
|
|
|
29
29
|
依据:Google C++ Style「Inputs and Outputs」、Abseil TotW 176、C++ Core Guidelines F.20/F.21、cppreference `std::optional` 与 `std::expected`。
|
|
@@ -50,7 +50,7 @@ app/
|
|
|
50
50
|
|
|
51
51
|
## 文件扩展名与归属
|
|
52
52
|
|
|
53
|
-
|
|
53
|
+
扩展名、槽位与 `#pragma once` 的写法以 `references/layout/02-file-template.md` 为准;本文只定位置:公共头放 `include/`,私有头放 `src/`,实现文件放 `src/`,测试放 `tests/`。
|
|
54
54
|
|
|
55
55
|
## 头文件契约
|
|
56
56
|
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
<!--
|
|
1
|
+
<!-- 读取时机:写头文件或实现文件时;读完能回答:文件模板;预算:一次只开这一份(124 行),不自动串联。 -->
|
|
2
2
|
# 文件模板
|
|
3
3
|
|
|
4
|
-
本文件是头文件与实现文件槽位的唯一权威。`references/style/01-google-cpp-style.md`
|
|
4
|
+
本文件是头文件与实现文件槽位的唯一权威。`references/style/01-google-cpp-style.md` 的头文件节保留文件级规则(头文件去重、自包含、包含顺序),槽位细节以本文件为准;`references/layout/01-source-layout.md` 只定文件放在哪;`steps/03-declaration.md` 指向这里。类内声明顺序仍只写在 `references/layout/03-declaration-order.md`,本文件不复制。
|
|
5
5
|
|
|
6
6
|
## 选择扩展名
|
|
7
7
|
|
|
8
|
-
| 扩展名 | 用途 |
|
|
8
|
+
| 扩展名 | 用途 | 是否写 `#pragma once` |
|
|
9
9
|
| --- | --- | --- |
|
|
10
10
|
| `.h` | C++ 公共或私有头文件 | 是 |
|
|
11
11
|
| `.hpp` | 与 `.h` 同槽位;一个工程统一选一种头扩展名 | 是 |
|
|
@@ -14,46 +14,34 @@
|
|
|
14
14
|
| `.inc` | 稀有文本包含 | 否;本工作流不用 |
|
|
15
15
|
|
|
16
16
|
- 一个工程只选一种头扩展名和一种实现扩展名;新工程与示例一律 `.cpp` / `.h`,已有工程跟随既有后缀,不批量改名。
|
|
17
|
-
-
|
|
18
|
-
-
|
|
17
|
+
- 头文件用一行 `#pragma once` 去重,不写 `#ifndef` / `#define` / `#endif` 包含守卫。这是本工作流对 Google 的明确偏离:路径式守卫要把项目名、目录名和文件名拼进宏,组件改名或头文件跨工程复用时宏名会跟着失效;`#pragma once` 由编译器按文件身份判断,不需要宏名,GCC、Clang、MSVC 都支持。
|
|
18
|
+
- 新文件先按本模板起骨架,再填内容;不因「先跑起来」省略 `#pragma once`。
|
|
19
19
|
|
|
20
|
-
##
|
|
20
|
+
## `#pragma once`
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
- `
|
|
25
|
-
-
|
|
26
|
-
-
|
|
27
|
-
|
|
28
|
-
| 文件 | 守卫 |
|
|
29
|
-
| --- | --- |
|
|
30
|
-
| `my_project/include/board.h` | `MY_PROJECT_INCLUDE_BOARD_H_` |
|
|
31
|
-
| `my_project/include/game.h` | `MY_PROJECT_INCLUDE_GAME_H_` |
|
|
32
|
-
| `my_project/include/computer.h` | `MY_PROJECT_INCLUDE_COMPUTER_H_` |
|
|
33
|
-
| `my_project/include/game_session.h` | `MY_PROJECT_INCLUDE_GAME_SESSION_H_` |
|
|
34
|
-
| `my_project/tests/computer_test_support.h` | `MY_PROJECT_TESTS_COMPUTER_TEST_SUPPORT_H_` |
|
|
35
|
-
|
|
36
|
-
- 通用示例:`taskflow/include/task_table.h` → `TASKFLOW_INCLUDE_TASK_TABLE_H_`。
|
|
37
|
-
- 不用 `MY_PROJECT_BOARD_H` 这类省略路径的名字;两个同名头会撞宏。
|
|
22
|
+
- 位置固定在文件头注释之后、第一组 `#include` 之前,单独一行。
|
|
23
|
+
- 只写 `#pragma once`,不写行尾注释,也不写与之配对的宏。
|
|
24
|
+
- 实现文件(`.cpp` / `.cc`)不写;`.inc` 本工作流不新写,第三方 `.inc` 不补。
|
|
25
|
+
- 不把 `#pragma once` 和 `#ifndef` 守卫混用;旧工程已有守卫时,只在改动该文件时顺手换成本模板的写法,不批量改名。
|
|
26
|
+
- 支持范围是 GCC、Clang 与 MSVC 的当前版本;需要在其他预处理器上复用时再讨论退回守卫,不预先写两套。
|
|
38
27
|
|
|
39
28
|
## `.h` 与 `.hpp`
|
|
40
29
|
|
|
41
30
|
槽位顺序固定:
|
|
42
31
|
|
|
43
|
-
1. 文件头注释(格式见 `references/comments/01-comment-rules.md
|
|
44
|
-
2. `#
|
|
32
|
+
1. 文件头注释(格式见 `references/comments/01-comment-rules.md`,留在 `#pragma once` 外)
|
|
33
|
+
2. `#pragma once`
|
|
45
34
|
3. 分组包含(组间空行,组内字母序)
|
|
46
35
|
4. 命名空间内:常量 → 枚举 → 结构体 → 自由函数 → 类
|
|
47
|
-
5. `#endif // MACRO`
|
|
48
36
|
|
|
49
37
|
```cpp
|
|
50
38
|
/// @file thing_table.h
|
|
51
|
-
/// @brief 定义 ThingTable 与它用到的状态。
|
|
52
39
|
///
|
|
53
|
-
///
|
|
40
|
+
/// @brief `ThingTable` 相关类型定义。
|
|
41
|
+
///
|
|
42
|
+
/// 封装了状态 `ThingState`、表项 `Thing` 与表 `ThingTable`。
|
|
54
43
|
|
|
55
|
-
#
|
|
56
|
-
#define PROJECT_PATH_FILE_H_
|
|
44
|
+
#pragma once
|
|
57
45
|
|
|
58
46
|
#include <cstdint>
|
|
59
47
|
#include <vector>
|
|
@@ -64,10 +52,10 @@ namespace project {
|
|
|
64
52
|
|
|
65
53
|
inline constexpr int kThingCount = 3;
|
|
66
54
|
|
|
67
|
-
enum class ThingState { kIdle, kBusy };
|
|
55
|
+
enum class ThingState : std::uint8_t { kIdle, kBusy };
|
|
68
56
|
|
|
69
57
|
struct Thing {
|
|
70
|
-
int id;
|
|
58
|
+
int id{};
|
|
71
59
|
};
|
|
72
60
|
|
|
73
61
|
class ThingTable {
|
|
@@ -79,14 +67,13 @@ class ThingTable {
|
|
|
79
67
|
};
|
|
80
68
|
|
|
81
69
|
} // namespace project
|
|
82
|
-
|
|
83
|
-
#endif // PROJECT_PATH_FILE_H_
|
|
84
70
|
```
|
|
85
71
|
|
|
86
|
-
-
|
|
72
|
+
- 文件头不写版权、作者或变更日志;文件头之后紧跟 `#pragma once`,再进入包含。
|
|
87
73
|
- 头文件自包含:自己用到的类型、宏和标准库头都直接包含,不依赖调用方先包含什么。
|
|
88
74
|
- 不把 `using namespace`、非 inline 普通全局变量、匿名命名空间或 `static` 定义放进头文件。
|
|
89
|
-
- 模板、`constexpr`
|
|
75
|
+
- 模板、`constexpr` 和类内短动作函数可以留在头文件;只读访问器与查询(`Get*`、`Is*` 等)即使只有一行,头文件只留声明,定义放 `.cpp`;其他普通函数体放实现文件。
|
|
76
|
+
- 只读访问器与查询统一放 `.cpp`,不要一部分内联、一部分外移;构造函数即使很短,只要带成员初始化列表或函数体,头文件只留声明,定义放 `.cpp`;`= default`、`= delete`、空体且无初始化列表的转发构造,以及 `constexpr` 需要的定义可留在类内。
|
|
90
77
|
- `.hpp` 完全套用 `.h` 的槽位,只换扩展名,不换顺序。
|
|
91
78
|
|
|
92
79
|
## `.cpp`
|
|
@@ -121,18 +108,18 @@ void ThingTable::Add(const Thing& thing) { things_.push_back(thing); }
|
|
|
121
108
|
|
|
122
109
|
## `.inc`
|
|
123
110
|
|
|
124
|
-
只用于必须做文本包含的场景。本工作流不新写 `.inc`;第三方 `.inc`
|
|
111
|
+
只用于必须做文本包含的场景。本工作流不新写 `.inc`;第三方 `.inc` 不补 `#pragma once`,也不为它改本模板。
|
|
125
112
|
|
|
126
113
|
## 自检
|
|
127
114
|
|
|
128
|
-
-
|
|
129
|
-
-
|
|
115
|
+
- 每个头文件在文件头之后、包含之前恰好一行 `#pragma once`。
|
|
116
|
+
- 头文件没有 `#ifndef` / `#define` / `#endif` 包含守卫,也没有与 `#pragma once` 并存。
|
|
130
117
|
- 头文件能单独编译:只包含它的一个空 `.cpp` 必须通过。
|
|
131
|
-
-
|
|
132
|
-
-
|
|
118
|
+
- 实现文件没有 `#pragma once`,配对头在第一组。
|
|
119
|
+
- 工程内不再新增路径式守卫宏。
|
|
133
120
|
|
|
134
121
|
## 依据
|
|
135
122
|
|
|
136
|
-
- [Google C++ Style Guide: Header Files](https://google.github.io/styleguide/cppguide.html#Header_Files)
|
|
137
|
-
- [Google C++ Style Guide: The #define Guard](https://google.github.io/styleguide/cppguide.html#The__define_Guard)
|
|
138
|
-
- [C++ Core Guidelines: SF.8](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#rs-guards)
|
|
123
|
+
- [Google C++ Style Guide: Header Files](https://google.github.io/styleguide/cppguide.html#Header_Files):头文件自包含、包含顺序;本工作流保留这两条。
|
|
124
|
+
- [Google C++ Style Guide: The #define Guard](https://google.github.io/styleguide/cppguide.html#The__define_Guard):Google 要求 `<PROJECT>_<PATH>_<FILE>_H_` 守卫;本工作流明确偏离,改用 `#pragma once`,理由见「`#pragma once`」一节。
|
|
125
|
+
- [C++ Core Guidelines: SF.8](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#rs-guards):同样建议包含守卫;本工作流按上一行的偏离执行。
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
<!-- 读取时机:排声明顺序时;读完能回答:声明顺序与定义签名;预算:一次只开这一份(
|
|
1
|
+
<!-- 读取时机:排声明顺序时;读完能回答:声明顺序与定义签名;预算:一次只开这一份(157 行),不自动串联。 -->
|
|
2
2
|
# 声明顺序
|
|
3
3
|
|
|
4
4
|
顺序的作用是让读者先看类型和契约,再看数据。`clang-format` 不管顺序,靠人工和 review 维持。
|
|
@@ -24,9 +24,9 @@ class TaskTable {
|
|
|
24
24
|
~TaskTable();
|
|
25
25
|
|
|
26
26
|
// 5. 其他成员函数
|
|
27
|
-
[[nodiscard]] bool HasTask(int id) const;
|
|
28
27
|
[[nodiscard]] TaskState GetTaskState(int id) const;
|
|
29
28
|
void SetTaskState(int id, TaskState state);
|
|
29
|
+
[[nodiscard]] bool HasTask(int id) const;
|
|
30
30
|
|
|
31
31
|
// 6. 数据成员
|
|
32
32
|
private:
|
|
@@ -55,9 +55,10 @@ class TaskTable {
|
|
|
55
55
|
|
|
56
56
|
Google、LLVM、OpenCV 都没有规定这一组内部的次序;本工作流补上:
|
|
57
57
|
|
|
58
|
-
- 有比较运算符时,开头是 `operator==`:成员默认比较和 `friend`
|
|
58
|
+
- 有比较运算符时,开头是 `operator==`:成员默认比较和 `friend` 都放这里,然后按属性/概念分组:同一属性的 `Get`/`Set` 成对相邻,`Is` / `Has` / `Can` 放在这对之后;再放普通函数,按使用先后。不要把谓词插进 `Get`/`Set` 之间,也不要把全部谓词提前到全部 `Get` 之前。
|
|
59
59
|
- 不属于 `Is` / `Get` / `Set` 的流程函数按调用层次排:入口操作在前,它调用的显示 helper 紧随其后;`DisplayOptions`、`Note` 等共享 helper 放在调用它们的流程之后;终端进入、退出与清屏可集中在组首,按进入、退出、清屏排。
|
|
60
|
-
- `CheckInRange` 是诊断函数,紧挨 `IsInRange`
|
|
60
|
+
- `CheckInRange` 是诊断函数,紧挨 `IsInRange` 写,不参与 `Get`/`Set` 的分组;它是诊断函数的相邻例外,见 `references/exceptions/01-exception-rules.md`。
|
|
61
|
+
- 没有 `Set` 配对时,谓词放在读取接口之后、修改接口之前。例:`GetOptionCount`、`GetSelectedIndex`、`OptionAt` 之后是 `IsSelected`,再是 `Select` / `MovePrevious` / `MoveNext` / `Reset`。
|
|
61
62
|
- 多个直接返回成员的 `Get` 按对应数据成员的声明顺序排列,不按调用频率或字母序:成员依次是 `current_player_`、`board_`、`history_`,getter 就依次是 `GetCurrentPlayer()`、`GetBoard()`、`GetHistory()`;成员顺序调整时 getter 一起改,一个类里只留一套顺序。
|
|
62
63
|
- 直接返回成员的 `Get` 按成员顺序;没有独立成员的派生查询排在直接 `Get` 之后,按「先局部元素、后整体状态」阅读,不插进直接 `Get` 之间。例:`Game` 的 `GetResult()` 扫描棋盘后才得到结果,排在直接返回成员的 `GetCurrentPlayer()`、`GetBoard()`、`GetHistory()` 之后。
|
|
63
64
|
- 成对的局部/整体操作沿用同一方向:先 `ClearSquare(pos)` 清一格,再 `Clear()` 清整盘;局部操作在前,整体操作在后,不按调用频率互换。
|
|
@@ -68,11 +69,10 @@ Google、LLVM、OpenCV 都没有规定这一组内部的次序;本工作流补
|
|
|
68
69
|
|
|
69
70
|
## 命名空间作用域的头文件
|
|
70
71
|
|
|
71
|
-
|
|
72
|
+
文件槽位与 `#pragma once` 的写法以 `references/layout/02-file-template.md` 为准;本节只看命名空间作用域内的排列顺序。
|
|
72
73
|
|
|
73
74
|
```cpp
|
|
74
|
-
#
|
|
75
|
-
#define TASKFLOW_INCLUDE_TASK_TABLE_H_
|
|
75
|
+
#pragma once
|
|
76
76
|
|
|
77
77
|
#include <array>
|
|
78
78
|
#include <cstdint>
|
|
@@ -100,8 +100,6 @@ class TaskTable {
|
|
|
100
100
|
};
|
|
101
101
|
|
|
102
102
|
} // namespace taskflow
|
|
103
|
-
|
|
104
|
-
#endif // TASKFLOW_INCLUDE_TASK_TABLE_H_
|
|
105
103
|
```
|
|
106
104
|
|
|
107
105
|
顺序:包含 → 命名空间 → `inline constexpr` 常量 → `enum class` → `struct`(被依赖的在前)→ 自由函数 → 类。类的公共接口类型放在类内最前。
|
|
@@ -136,7 +134,7 @@ class TaskTable {
|
|
|
136
134
|
|
|
137
135
|
### 函数体内的空行
|
|
138
136
|
|
|
139
|
-
-
|
|
137
|
+
- 每个以 `return` / `throw` 结束的守卫各自成组,守卫组之间空 1 行;同一次检查拆成取值、判断与返回多条语句时组内不空行;守卫组与后续处理之间空 1 行。
|
|
140
138
|
- 取回一个结果并按结果分支的语句连成一组(声明、`if`、`throw` / `return`),组内不空行;这一组与收尾(记录这一步、轮到对方、返回成功)之间空 1 行。
|
|
141
139
|
- 同一组语句不拆:三个校验之后的赋值与 `return` 之间不空行。
|
|
142
140
|
- 只有次序依赖的一对 `if`(`GetWinner`、`JudgeAfterPlace`)之间空 1 行。
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
<!-- 读取时机:命名或统一术语时;读完能回答:命名;预算:一次只开这一份(
|
|
1
|
+
<!-- 读取时机:命名或统一术语时;读完能回答:命名;预算:一次只开这一份(136 行),不自动串联。 -->
|
|
2
2
|
# 命名
|
|
3
3
|
|
|
4
4
|
命名先回答「它是什么角色」,再选词。规则表是底线,语义角色表决定用哪个动词。
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
| 种类 | 规则 | 好 | 差 |
|
|
9
9
|
| --- | --- | --- | --- |
|
|
10
10
|
| 类型 / 类 / 结构 / 枚举 | PascalCase | `TaskTable`, `Order`, `TaskState` | `task_table_t`, `order_result` |
|
|
11
|
-
| 函数 / 成员函数 | PascalCase | `
|
|
11
|
+
| 函数 / 成员函数 | PascalCase | `GetTaskState`, `PlayerToOpponent` | `contains`, `gettaskstate` |
|
|
12
12
|
| 变量 / 参数 | snake_case | `task_count`, `order` | `TaskCount`, `o` |
|
|
13
13
|
| 类成员 | snake_case + `_` | `tasks_`, `orders_` | `m_tasks`, `tasks` |
|
|
14
14
|
| 结构体成员 | snake_case,无尾随 `_` | `name`, `priority` | `name_`, `priority_` |
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
| 概念 | PascalCase | `Sortable`, `Hashable` | `sortable`, `is_hashable` |
|
|
21
21
|
|
|
22
22
|
- 文件名用 snake_case:`order_result.h`、`task_table.cpp`;测试文件 `task_table_test.cpp`。Google 也允许 `-`,本项目统一 `_`,与 target 和测试名保持一致。
|
|
23
|
-
- 查询名与返回类型一致:返回方格的值时写 `
|
|
23
|
+
- 查询名与返回类型一致:返回方格的值时写 `GetValue`,返回玩家时写 `GetCurrentPlayer`;不写项目里没有对应类型的 `GetPlayer`。
|
|
24
24
|
- 命名空间的自由函数用 `来源To结果` 加返回值类型,不用 `Get`:玩家换方写 `PlayerToOpponent`,玩家变成方格的值写 `PlayerToSquareValue`,不写 `SquareValueOf`。`Get` 留给类里直接返回成员的查询,如 `GetCurrentPlayer`。`To` 后面先写结果类型再写角色,`PlayerToOpponent` 不带重复的 `Player`;没有 `To` 承接时单个 `Opponent` 读不出是玩家还是方格,`To` 语境下反而清楚。名字要点明两端角色,与注释同一个说法。
|
|
25
25
|
- 布尔变量用 `is_` / `has_` / `can_` 开头:`is_valid`、`has_stock`;不要用 `flag`、`status`。
|
|
26
26
|
- 容器用复数:`tasks`、`orders`;单个元素用单数。
|
|
@@ -39,6 +39,12 @@
|
|
|
39
39
|
- 可能选不中时返回 `std::optional`:`ChoosePosition`、`ChooseRandomPosition`、`ChooseBestPosition` 没有合法选择时返回 `std::nullopt`,不返回哨兵位置。
|
|
40
40
|
- 共享资源访问器用名词短语(`RandomEngine`),不写 `GetRandomEngine`;`Get` 留给类里直接返回成员的查询。
|
|
41
41
|
|
|
42
|
+
### 界面与索引
|
|
43
|
+
|
|
44
|
+
- 界面组件名先写职责,再写形态:`ListMenu` 是以列表呈现的菜单,`MenuList` 是多个菜单的列表;`MainMenu`、`DifficultyMenu` 用界面职责命名,不用 `MenuModel` 泛称多个菜单的模型。
|
|
45
|
+
- 多个菜单共享同一套列表选择逻辑时,把状态模型命名为 `ListMenuModel`;只有一个菜单时不单独造模型名。字段保存下标时写 `selected_index_`,保存被选对象时写 `selected_`;注释写“当前选中标签在标签列表中的下标”。
|
|
46
|
+
- 非拥有视图成员按内容命名(`labels_`、`history_`),不写 `labels_view_` 重复类型;成员注释写清它不拥有什么、生命周期由谁保证。
|
|
47
|
+
|
|
42
48
|
## 字符集与保留标识符
|
|
43
49
|
|
|
44
50
|
- 代码标识符只用 ASCII:字母、数字、下划线。标准允许 Unicode XID 标识符,但工具链、字体与跨平台编辑不一致,Google 同样要求 ASCII。
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
- 英文棋类资料把 `Square` 当坐标或位置:CPW 的 `Squares`、Stockfish 的 `enum Square`、chess.hpp、python-chess、Chess.com 都这样用;`Square` 只表示「哪一格」,不表示「格中是什么」。
|
|
20
20
|
- 井字棋英文资料更常用 `mark`:Exploratorium 写「putting their marks in empty squares」,SciMigo 的 `enum class Mark { Empty, X, O }` 也是直接先例。`Mark` 比 `SquareValue` 短,也不会把容器名重复进值类型。
|
|
21
21
|
- `SquareValue` 也有先例:LucHermitte 的 C++ 井字棋用 `enum class SquareValue` 与 `Square::value()`;Rust 的 tictacrustle 用 `SquareValue` 与 `set_value`;React 井字棋用 `value`。它和 `Square` 配对,读作「方格的值」。
|
|
22
|
-
- 风险在 `Value` 的歧义:Stockfish 的 `using Value = int` 是搜索分值;中文项目的 `Value[3][3]` 是点位评估分。`SquareValue`
|
|
22
|
+
- 风险在 `Value` 的歧义:Stockfish 的 `using Value = int` 是搜索分值;中文项目的 `Value[3][3]` 是点位评估分。`SquareValue` 有被读成「分值」的可能,所以类型注释只写「方格的值。」,取值由枚举行的 `///<` 表达,不把取值写回类型注释;函数名跟类型走 `GetValue` / `SetValue`。
|
|
23
23
|
- 本项目按用户裁决使用 `SquareValue`,与插件规则、代码和书稿统一;`Mark` 只在英文资料的取舍说明里出现,不作为代码名。
|
|
24
24
|
- 中文用词:`Square` 写「方格」,`SquareValue` 写「方格的值」;`格子` 更泛(汉典「方形的空栏或框子」),`棋格` 是「下棋的技艺水平」(汉典),都不能替代「方格」;`棋盘` 是整块盘面。`值` 用「方格的值」「空」「X」「O」;`标记` 不再用于这个类型。
|
|
25
25
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
<!-- 读取时机:查格式与写法时;读完能回答:Google C++ 风格:本技能采用的版本;预算:一次只开这一份(93 行),不自动串联。 -->
|
|
2
2
|
# Google C++ 风格:本技能采用的版本
|
|
3
3
|
|
|
4
|
-
以 [Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html) 为基线。除「异常政策」「新工程默认 C++23」「枚举底层类型 `std::uint8_t
|
|
4
|
+
以 [Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html) 为基线。除「异常政策」「新工程默认 C++23」「枚举底层类型 `std::uint8_t`」「注释格式」「头文件去重方式」和「排版(4 空格、120 列、`IncludeBlocks: Preserve`)」六处明确偏离外,排版、命名、头文件、包含顺序和类型都按 Google 原文执行;普通对象默认花括号属于 Google 允许的本地选择,不单列为偏离;本地习惯不能悄悄覆盖它。
|
|
5
5
|
|
|
6
6
|
## 语言版本
|
|
7
7
|
|
|
@@ -13,14 +13,14 @@
|
|
|
13
13
|
|
|
14
14
|
## 头文件
|
|
15
15
|
|
|
16
|
-
-
|
|
16
|
+
- 每个头文件用一行 `#pragma once` 去重,写在文件头之后、第一组 `#include` 之前;不写 `#ifndef` / `#define` 宏守卫。Google 要求路径式守卫,本工作流明确偏离,原因是宏名要把项目名、目录名和文件名拼在一起,组件改名或头文件跨工程复用时宏名会失效,而 `#pragma once` 按文件身份判断;GCC、Clang、MSVC 都支持。模板见 `references/layout/02-file-template.md`。
|
|
17
17
|
- 头文件必须自包含:自己用到的类型、宏、标准库头都直接包含,不依赖调用方的包含顺序。
|
|
18
18
|
- `.cpp` 第一个包含自己的配对头,然后按下面的顺序包含。
|
|
19
|
-
- 包含顺序:配对头 → C 系统头 → C++ 标准库头 → 其他库头 → 本项目头。每组之间空一行,组内按字母序。clang-format 的 Google 预设自带 `IncludeCategories`(`^<.*\.h>` 第一组、其余尖括号头第二组、引号头最后)并开了 `IncludeBlocks: Regroup`,配置里不写 `SortIncludes: false`
|
|
19
|
+
- 包含顺序:配对头 → C 系统头 → C++ 标准库头 → 其他库头 → 本项目头。每组之间空一行,组内按字母序。clang-format 的 Google 预设自带 `IncludeCategories`(`^<.*\.h>` 第一组、其余尖括号头第二组、引号头最后)并开了 `IncludeBlocks: Regroup`,配置里不写 `SortIncludes: false` 时这份分组与排序会自动落实。模板资产把 `IncludeBlocks` 设为 `Preserve`,保留手写空行分组,组内仍按上述类别排序。clang-format 按 `IncludeIsMainRegex` 从文件名推断配对头(`board_test.cpp` 配 `board.h`);文件名不遵循 `<name>` 或 `<name>_test` 时要自己把配对头放在第一条。`#include` 之间夹独立注释或 `#if` 会切断分组,这类位置要自己保持次序。需要把第三方库单独成组(例如 `<gtest/gtest.h>` 与标准库之间空一行)时,才在工程 `.clang-format` 里追加优先于 `^<.*` 的类别,并说明理由;预设已覆盖的分组不要重复手写。`clang-tidy` 的 `llvm-include-order` 按 LLVM 分组,与这条不同,不要拿来替代 clang-format。
|
|
20
20
|
- 本项目头用引号写裸文件名,例如 `#include "logging.h"`。构建时把该组件的 `include/` 加入 include 搜索路径(`target_include_directories(<target> PUBLIC .../include)`),不要写 `"base/logging.h"` 这种从源码根起的长路径。
|
|
21
21
|
- 公共头文件直接放在组件的 `include/` 下;不要为了「整齐」再加一层命名空间目录。
|
|
22
22
|
- 跨文件共享的常量用 `inline constexpr`,放在命名空间里并带 `k` 前缀;头文件里不要定义非 inline 的普通全局变量。
|
|
23
|
-
- 头文件里只放模板、`constexpr
|
|
23
|
+
- 头文件里只放模板、`constexpr`、类内联的短动作函数和声明;只读访问器与查询即使很短也放 `.cpp`;普通函数体放 `.cpp`。
|
|
24
24
|
- 头文件里不要用匿名命名空间或 `static` 隐藏定义;内部链接的实现放 `.cpp`。
|
|
25
25
|
|
|
26
26
|
## 命名
|
|
@@ -35,11 +35,11 @@
|
|
|
35
35
|
## 类型与声明
|
|
36
36
|
|
|
37
37
|
- 纯数据聚合用 `struct`;有不变量的类型用 `class`,不变量在构造函数和公共成员函数里维持。
|
|
38
|
-
-
|
|
38
|
+
- 单参数构造函数默认写 `explicit`,例外见 `references/language/language-basics/06-functions.md`;零参数构造函数不写。
|
|
39
39
|
- 默认成员初始化写在声明处(NSDMI),不要在构造函数初始化列表里重复;初始值来自构造参数或工厂调用时,改在构造函数初始化列表用花括号写一次,如 `screen_{MakeScreen()}`。
|
|
40
40
|
- 普通对象默认用花括号初始化:`int count{};`、`std::vector<int> values{1, 2, 3};`。成员声明处也默认写 `{}`;拷贝初始化或 `auto` 推导用 `=`;`name = {{` 只用于多行聚合常量表,单个对象和空初始化仍写 `count{}`;容量构造等 `()` 例外见 `references/language/language-basics/02-initialization.md`。
|
|
41
|
-
-
|
|
42
|
-
- 用户声明的拷贝/移动/析构函数在头文件声明、在 `.cpp`
|
|
41
|
+
- 类内声明顺序(public → protected → private,类型、常量、工厂、构造、函数、数据成员)以 `references/layout/03-declaration-order.md` 为准,本文不复述。
|
|
42
|
+
- 用户声明的拷贝/移动/析构函数在头文件声明、在 `.cpp` 定义;默认实现足够时写 `= default`(默认构造无参数,拷贝/移动构造与赋值按各自的签名)。
|
|
43
43
|
- 查询成员函数标尾部 `const`;返回结果对象的 API 加 `[[nodiscard]]`,不要给所有非 `void` 函数机械添加。标注位置与 `friend` 的例外见 `references/language/value-types/07-nodiscard.md`。
|
|
44
44
|
- 所有权优先 `std::unique_ptr`;没有共享所有权的理由时不用 `std::shared_ptr`。禁止裸 `new` / `delete`。
|
|
45
45
|
- 禁止 C 风格强制转换;用 `static_cast`、`const_cast`、`reinterpret_cast`,C++20 起可用 `std::bit_cast`,低标准用 `std::memcpy`;并让 `clang-tidy` 的 `google-readability-casting` 兜底。
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
- 优先返回值,不用输出参数:返回 `std::string`、`std::vector<T>`、结果结构体。
|
|
56
56
|
- 已有成员按 `const` 引用返回,例如 `GetHistory()`;不要返回内部容器的可变引用。
|
|
57
57
|
- 函数保持短小;没有硬上限,超过约 40 行时先拆出有名字的步骤,而不是加注释分节。
|
|
58
|
-
- 头文件里只把大约 10
|
|
58
|
+
- 头文件里只把大约 10 行以内、确实适合内联的短动作函数就地定义;只读访问器与查询即使很短也放 `.cpp`;更长或需要完整定义的函数放 `.cpp`,避免头文件膨胀和重复编译。
|
|
59
59
|
- 虚函数不用默认参数;默认参数只用于非虚的便利接口。
|
|
60
60
|
- 尾置返回类型只在普通语法不可行或明显更难读时使用,例如返回类型依赖参数;不要为了风格统一全面改写。
|
|
61
61
|
- `if` / `for` / `while` / `do` 总是写花括号;`case` 的取舍见 `references/language/language-basics/05-expressions.md`。
|
|
@@ -65,7 +65,7 @@
|
|
|
65
65
|
|
|
66
66
|
- 比较不写 Yoda 形式:`x == 0`、`task == TaskState::kPending`;不要写 `0 == x`、`nullptr == p`。
|
|
67
67
|
- 半开区间写成两个「变量在左」的比较:`id >= 0 && id < kTaskCount`;不要写 `0 <= id`。
|
|
68
|
-
-
|
|
68
|
+
- 混合优先级算术加显式括号,让读代码的人不必先还原优先级;默认 Checks 不含 `readability-math-missing-parentheses`,这条靠评审。
|
|
69
69
|
- 内建整数默认用 `int`;需要确定宽度时用 `<cstdint>` 的 `std::int32_t`、`std::int64_t` 等(可省略 `std::` 前缀),可能到 2^31 的值用 64 位;`std::size_t` / `std::ptrdiff_t` 只用于大小和指针差。不要用无符号类型表达「这个值不会为负」。
|
|
70
70
|
- 封闭状态码用 `enum class`;本项目固定写 `: std::uint8_t`(见 `references/language/value-types/04-enum.md`,有意偏离 Google 的默认 `int`),普通枚举用默认底层类型。枚举值只在必要时显式指定;默认的连续值有利于 `switch` 的实现,也能避免因手写数值而出错(C++ Core Guidelines Enum.8)。枚举值总是带作用域:`OrderResult::kSuccess`。
|
|
71
71
|
- 枚举的尾随注释要么全写要么全不写;两种句式与禁止项见 `references/comments/01-comment-rules.md`「枚举项与 `struct` 短字段:两种句式」。
|
|
@@ -83,7 +83,7 @@ Google 原文不使用 C++ 异常;本工作流只在开发阶段的源码指
|
|
|
83
83
|
- 组合消息以 `std::format` 为主:`std::format("任务 {} 进入状态 {}", task, state)`。
|
|
84
84
|
- 写到已有的 `std::ostream` 用 `<<`;没有流对象时,默认用 `std::print` 与 `std::println`,它们只在 C++23 且标准库支持时可用;见 `references/language/standard-library/06-format.md`。
|
|
85
85
|
- 简单字面量输出可以直接用 `std::cout << "..."`;需要拼接变量、对齐、数值格式时用 `std::format`。
|
|
86
|
-
-
|
|
86
|
+
- 列宽跟文件最近的 `.clang-format` 的 `ColumnLimit` 走(从文件目录向上查到工程根);工程未声明时按 120 列,模板资产固定 `ColumnLimit: 120`。每级缩进用 4 个空格,续行与行内对齐也用空格;缩进与对齐由 `.clang-format` 统一,不在条文里另定一套。
|
|
87
87
|
- 浮点输出固定精度,整数不依赖隐式格式;`std::format` 的格式串写全。
|
|
88
88
|
|
|
89
89
|
## 注释
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// 只读注释审计:按 references/comments/01-comment-rules.md 的硬规则检查 C/C++ 源文件。
|
|
3
3
|
//
|
|
4
4
|
// 检查项:Doxygen 文件头、注释行宽度、列表缩进、标签、标签间空行、注释与声明的贴合、
|
|
5
|
-
//
|
|
5
|
+
// 成员功能清单、设计论证(方案对比)、否定式边界、领域尺寸、谓词首句、@param 完整性、摘要裸参数名、枚举成员注释一致性、枚举行尾句式、数据成员主注释位置、控制序列常量注释、定义处重复声明注释、只在 .cpp 出现的函数契约。
|
|
6
6
|
// 不重写任何文件。
|
|
7
7
|
//
|
|
8
8
|
// 用法:
|
|
@@ -162,15 +162,17 @@ const TAG_GROUPS = {
|
|
|
162
162
|
throw: 'throws',
|
|
163
163
|
exception: 'throws',
|
|
164
164
|
note: 'note',
|
|
165
|
+
pre: 'pre',
|
|
166
|
+
post: 'pre',
|
|
165
167
|
}
|
|
166
168
|
|
|
167
|
-
/**
|
|
169
|
+
/** 取标签所属分组;参数、返回值、异常、前置条件、说明各成一组。 */
|
|
168
170
|
function tagGroupOf(tag) {
|
|
169
171
|
const name = tag.slice(1).toLowerCase().split(/[\s{[(]/)[0]
|
|
170
172
|
return TAG_GROUPS[name] ?? 'other'
|
|
171
173
|
}
|
|
172
174
|
|
|
173
|
-
/**
|
|
175
|
+
/** 分组之间允许空行;同一分组内出现空行才算标签粘连。摘要与首个标签之间的空行另由 brief-tag-gap 检查。 */
|
|
174
176
|
function hasTagGap(contents) {
|
|
175
177
|
let lastGroup = null
|
|
176
178
|
let pendingGap = false
|
|
@@ -193,6 +195,19 @@ function hasTagGap(contents) {
|
|
|
193
195
|
return false
|
|
194
196
|
}
|
|
195
197
|
|
|
198
|
+
/** 摘要或详细描述与首个标签之间必须有空行。 */
|
|
199
|
+
function hasBriefTagGap(contents) {
|
|
200
|
+
let sawText = false
|
|
201
|
+
for (let index = 0; index < contents.length; index += 1) {
|
|
202
|
+
const trimmed = contents[index].trim()
|
|
203
|
+
if (trimmed.startsWith('@')) {
|
|
204
|
+
return sawText && index > 0 && contents[index - 1].trim() !== ''
|
|
205
|
+
}
|
|
206
|
+
if (trimmed !== '') sawText = true
|
|
207
|
+
}
|
|
208
|
+
return false
|
|
209
|
+
}
|
|
210
|
+
|
|
196
211
|
/** 取出顶层逗号分隔的参数段,忽略模板与括号内的逗号。 */
|
|
197
212
|
function splitParams(text) {
|
|
198
213
|
const parts = []
|
|
@@ -452,8 +467,8 @@ export function checkText(text, options = {}) {
|
|
|
452
467
|
while (doxyHeaderEnd + 1 < lines.length && lines[doxyHeaderEnd + 1].trim().startsWith('///')) doxyHeaderEnd += 1
|
|
453
468
|
}
|
|
454
469
|
if (requireHeader && firstContent !== -1 && !hasDoxyHeader) {
|
|
455
|
-
if (style === 'block') add(firstContent + 1, 'header-missing', '文件开头缺少文件头:写 /** ... */,第一行 @file
|
|
456
|
-
else add(firstContent + 1, 'header-missing', '文件开头缺少文件头:写 /// @file
|
|
470
|
+
if (style === 'block') add(firstContent + 1, 'header-missing', '文件开头缺少文件头:写 /** ... */,第一行 @file <文件名>,随后写 @brief')
|
|
471
|
+
else add(firstContent + 1, 'header-missing', '文件开头缺少文件头:写 /// @file <文件名>,随后写 /// @brief')
|
|
457
472
|
}
|
|
458
473
|
if (firstContent !== -1 && hasDoxyHeader) {
|
|
459
474
|
const own = options.fileName ? options.fileName.split(/[\\/]/).pop() : ''
|
|
@@ -465,11 +480,15 @@ export function checkText(text, options = {}) {
|
|
|
465
480
|
}
|
|
466
481
|
}
|
|
467
482
|
if (options.requireBrief !== false) {
|
|
483
|
+
const slashBrief = (lines[firstContent + 1] ?? '').trim()
|
|
484
|
+
const slashGap = /^\/\/\/\s*$/.test(slashBrief)
|
|
485
|
+
const blockBrief = headerBlock?.contents[1] ?? ''
|
|
486
|
+
const blockGap = style === 'block' && blockBrief.trim() === ''
|
|
468
487
|
const hasBrief = style === 'block'
|
|
469
|
-
? BLOCK_BRIEF.test(headerBlock
|
|
470
|
-
: DOXY_BRIEF.test((lines[firstContent +
|
|
488
|
+
? BLOCK_BRIEF.test(blockBrief) || (blockGap && BLOCK_BRIEF.test(headerBlock?.contents[2] ?? ''))
|
|
489
|
+
: DOXY_BRIEF.test(slashBrief) || (slashGap && DOXY_BRIEF.test((lines[firstContent + 2] ?? '').trim()))
|
|
471
490
|
if (!hasBrief) {
|
|
472
|
-
add(style === 'block' ? headerBlock.start + 3 : firstContent + 2, 'header-brief', '
|
|
491
|
+
add(style === 'block' ? headerBlock.start + 3 : firstContent + 2, 'header-brief', '文件头在 @file 之后缺少 @brief 一句话说明')
|
|
473
492
|
}
|
|
474
493
|
}
|
|
475
494
|
const headerLines = style === 'block'
|
|
@@ -518,6 +537,9 @@ export function checkText(text, options = {}) {
|
|
|
518
537
|
add(raw + 1, 'list-indent', '块注释列表项要在 * 后空三格再写 -:` * - `,与总起句拉开层级')
|
|
519
538
|
}
|
|
520
539
|
}
|
|
540
|
+
if (!doc.contents.some((content) => content.includes('@file')) && hasBriefTagGap(doc.contents)) {
|
|
541
|
+
add(doc.start + 1, 'brief-tag-gap', '摘要或详细描述与标签块之间要空一行:在首个标签前补一行 `///`')
|
|
542
|
+
}
|
|
521
543
|
if (!doc.contents.some((content) => content.includes('@file')) && hasTagGap(doc.contents)) {
|
|
522
544
|
add(doc.start + 1, 'tag-gap', '同一分组内的 Doxygen 标签之间有空行:参数、返回值、异常、说明各成一组,组间空一行,组内连续')
|
|
523
545
|
}
|
|
@@ -547,6 +569,9 @@ export function checkText(text, options = {}) {
|
|
|
547
569
|
const declaration = lines[next].trim()
|
|
548
570
|
const block = lines.slice(start, end + 1).join('\n')
|
|
549
571
|
const docContents = lines.slice(start, end + 1).map((line) => line.trim().replace(/^\/\/\/\s?/, ''))
|
|
572
|
+
if (hasBriefTagGap(docContents)) {
|
|
573
|
+
add(start + 1, 'brief-tag-gap', '摘要或详细描述与标签块之间要空一行:在首个标签前补一行 `///`')
|
|
574
|
+
}
|
|
550
575
|
if (hasTagGap(docContents)) {
|
|
551
576
|
add(start + 1, 'tag-gap', '同一分组内的 Doxygen 标签之间有空行:参数、返回值、异常、说明各成一组,组间空一行,组内连续')
|
|
552
577
|
}
|
|
@@ -587,7 +612,6 @@ export function checkText(text, options = {}) {
|
|
|
587
612
|
add(start + 1, 'param-in-summary', `函数摘要直接写了参数名 ${named.join('、')}:改用「指定坐标」「传入的值」这类语义名词,参数名只放在 @param 行`)
|
|
588
613
|
}
|
|
589
614
|
const documented = [...block.matchAll(/@param(?:\[[^\]]*\])?\s+(\w+)/g)].map((match) => match[1])
|
|
590
|
-
if (documented.length === 0) continue
|
|
591
615
|
for (const name of params) {
|
|
592
616
|
if (!documented.includes(name)) add(start + 1, 'param-missing', `参数 ${name} 没有 @param 说明`)
|
|
593
617
|
}
|
|
@@ -636,7 +660,6 @@ export function checkText(text, options = {}) {
|
|
|
636
660
|
add(doc.start + 1, 'param-in-summary', `函数摘要直接写了参数名 ${named.join('、')}:改用「指定坐标」「传入的值」这类语义名词,参数名只放在 @param 行`)
|
|
637
661
|
}
|
|
638
662
|
const documented = [...block.matchAll(/@param(?:\[[^\]]*\])?\s+(\w+)/g)].map((match) => match[1])
|
|
639
|
-
if (documented.length === 0) continue
|
|
640
663
|
for (const name of params) {
|
|
641
664
|
if (!documented.includes(name)) add(doc.start + 1, 'param-missing', `参数 ${name} 没有 @param 说明`)
|
|
642
665
|
}
|