@chengzhao-dev/dsh-cpp-workflow 0.7.1 → 0.7.4
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 +16 -10
- package/README.md +16 -10
- package/cordis.patch.yml +6 -6
- package/lib/client.js +3 -3
- package/lib/comment-policy.js +1 -1
- package/lib/index.js +45 -37
- package/lib/workspace.js +10 -17
- package/package.json +9 -3
- package/skills/building-cpp/SKILL.md +17 -17
- package/skills/building-cpp/assets/templates/projects/common/running-build.sh +0 -0
- package/skills/building-cpp/examples/environment.md +4 -3
- package/skills/building-cpp/references/{build-script.md → 01-build-script.md} +4 -3
- package/skills/building-cpp/references/{environment.md → 02-environment.md} +3 -2
- package/skills/building-cpp/scripts/checking-environment.mjs +11 -13
- package/skills/building-cpp/steps/01-build.md +4 -3
- package/skills/building-cpp/steps/02-tooling.md +12 -12
- package/skills/building-cpp/steps/03-check.md +5 -5
- package/skills/building-cpp/{project/vscode/02-settings.md → steps/04-vscode.md} +24 -6
- package/skills/developing-cpp/SKILL.md +21 -18
- package/skills/developing-cpp/references/comments/{comment-rules.md → 01-comment-rules.md} +81 -64
- package/skills/developing-cpp/references/comments/{comment-style.md → 02-style.md} +16 -18
- package/skills/developing-cpp/references/comments/examples/{declaration-class.md → 01-declaration-class.md} +16 -16
- package/skills/developing-cpp/references/comments/examples/{declaration-constructor.md → 02-declaration-constructor.md} +3 -2
- package/skills/developing-cpp/references/comments/examples/03-declaration-enum.md +48 -0
- package/skills/developing-cpp/references/comments/examples/{declaration-file.md → 04-declaration-file.md} +6 -5
- package/skills/developing-cpp/references/comments/examples/05-declaration-function.md +116 -0
- package/skills/developing-cpp/references/comments/examples/{declaration-member.md → 06-declaration-member.md} +4 -3
- package/skills/developing-cpp/references/comments/examples/07-declaration-predicate.md +15 -0
- package/skills/developing-cpp/references/comments/examples/{declaration-struct.md → 08-declaration-struct.md} +4 -3
- package/skills/developing-cpp/references/comments/examples/09-implementation-body.md +95 -0
- package/skills/developing-cpp/references/comments/examples/10-implementation-todo.md +17 -0
- package/skills/developing-cpp/references/comments/glossary/01-comment-terms.md +93 -0
- package/skills/developing-cpp/references/design/{project-design.md → 01-project-design.md} +21 -17
- package/skills/developing-cpp/references/design/examples/{behavior-sentence.md → 01-behavior-sentence.md} +1 -0
- package/skills/developing-cpp/references/design/examples/{coordinate-table.md → 02-coordinate-table.md} +4 -3
- package/skills/developing-cpp/references/design/examples/{header-place.md → 03-header-place.md} +2 -1
- package/skills/developing-cpp/references/design/examples/{library-boundary.md → 04-library-boundary.md} +1 -0
- package/skills/developing-cpp/references/design/examples/{library-split.md → 05-library-split.md} +1 -0
- package/skills/developing-cpp/references/design/examples/06-move-result.md +42 -0
- package/skills/developing-cpp/references/design/examples/07-preview-read.md +37 -0
- package/skills/developing-cpp/references/design/session-layer.md +89 -0
- package/skills/developing-cpp/references/exceptions/{throw-rules.md → 01-exception-rules.md} +56 -61
- package/skills/developing-cpp/references/exceptions/examples/{throw-boundary.md → 01-throw-boundary.md} +1 -0
- package/skills/developing-cpp/references/exceptions/examples/{throw-build-mode.md → 02-throw-build-mode.md} +5 -4
- package/skills/developing-cpp/references/exceptions/examples/{throw-catch.md → 03-throw-catch.md} +1 -0
- package/skills/developing-cpp/references/exceptions/examples/{throw-destructor.md → 04-throw-destructor.md} +1 -0
- package/skills/developing-cpp/references/exceptions/examples/{throw-format.md → 05-throw-format.md} +1 -0
- package/skills/developing-cpp/references/exceptions/examples/{throw-message.md → 06-throw-message.md} +10 -1
- package/skills/developing-cpp/references/exceptions/examples/{throw-when.md → 07-throw-when.md} +1 -0
- package/skills/developing-cpp/references/index.md +132 -0
- package/skills/developing-cpp/references/language/glossary/{language-terms.md → 01-language-terms.md} +3 -2
- package/skills/developing-cpp/references/language/language-basics/01-objects.md +2 -1
- package/skills/developing-cpp/references/language/language-basics/02-initialization.md +2 -1
- package/skills/developing-cpp/references/language/language-basics/03-deduction.md +1 -0
- package/skills/developing-cpp/references/language/language-basics/04-const.md +5 -4
- package/skills/developing-cpp/references/language/language-basics/05-expressions.md +13 -4
- package/skills/developing-cpp/references/language/language-basics/06-functions.md +4 -1
- package/skills/developing-cpp/references/language/language-basics/07-classes.md +1 -0
- package/skills/developing-cpp/references/language/language-basics/08-raii.md +1 -0
- package/skills/developing-cpp/references/language/language-basics/09-templates.md +1 -0
- package/skills/developing-cpp/references/language/language-basics/{10-checklist.md → 10-language-checklist.md} +2 -0
- package/skills/developing-cpp/references/language/performance/01-measure.md +1 -0
- package/skills/developing-cpp/references/language/performance/02-complexity.md +1 -0
- package/skills/developing-cpp/references/language/performance/03-memory.md +1 -0
- package/skills/developing-cpp/references/language/performance/04-undefined-behavior.md +1 -0
- package/skills/developing-cpp/references/language/performance/05-pitfalls.md +1 -0
- package/skills/developing-cpp/references/language/performance/06-false-optimizations.md +1 -0
- package/skills/developing-cpp/references/language/performance/07-checklist.md +1 -0
- package/skills/developing-cpp/references/language/standard-library/01-containers.md +1 -0
- package/skills/developing-cpp/references/language/standard-library/02-access.md +2 -1
- package/skills/developing-cpp/references/language/standard-library/03-iteration.md +1 -0
- package/skills/developing-cpp/references/language/standard-library/04-algorithms.md +1 -0
- package/skills/developing-cpp/references/language/standard-library/05-string.md +1 -0
- package/skills/developing-cpp/references/language/standard-library/06-format.md +17 -6
- package/skills/developing-cpp/references/language/standard-library/07-time.md +1 -0
- package/skills/developing-cpp/references/language/standard-library/08-random.md +2 -0
- package/skills/developing-cpp/references/language/standard-library/09-files.md +1 -0
- package/skills/developing-cpp/references/language/standard-library/{10-checklist.md → 10-library-checklist.md} +1 -0
- package/skills/developing-cpp/references/language/value-types/01-width.md +1 -0
- package/skills/developing-cpp/references/language/value-types/02-signed.md +1 -0
- package/skills/developing-cpp/references/language/value-types/03-integer-roles.md +1 -0
- package/skills/developing-cpp/references/language/value-types/04-enum.md +3 -2
- package/skills/developing-cpp/references/language/value-types/05-strong-type.md +1 -0
- package/skills/developing-cpp/references/language/value-types/06-const-method.md +1 -0
- package/skills/developing-cpp/references/language/value-types/07-nodiscard.md +2 -1
- package/skills/developing-cpp/references/language/value-types/08-comparison.md +2 -1
- package/skills/developing-cpp/references/language/value-types/09-subscript.md +2 -1
- package/skills/developing-cpp/references/language/value-types/10-float-bool.md +1 -0
- package/skills/developing-cpp/references/language/value-types/11-checklist.md +1 -0
- package/skills/developing-cpp/references/layout/01-source-layout.md +3 -3
- package/skills/developing-cpp/references/layout/02-file-template.md +10 -9
- package/skills/developing-cpp/references/layout/03-declaration-order.md +25 -12
- package/skills/developing-cpp/references/naming/01-identifier-names.md +128 -0
- package/skills/developing-cpp/references/naming/examples/01-naming-accessor.md +29 -0
- package/skills/developing-cpp/references/naming/examples/02-naming-basic.md +4 -0
- package/skills/developing-cpp/references/naming/examples/{naming-current-prefix.md → 03-naming-current-prefix.md} +1 -0
- package/skills/developing-cpp/references/naming/examples/04-naming-local.md +46 -0
- package/skills/developing-cpp/references/naming/examples/{naming-lookup.md → 05-naming-lookup.md} +2 -1
- package/skills/developing-cpp/references/naming/examples/06-naming-lower-layer.md +46 -0
- package/skills/developing-cpp/references/naming/examples/{naming-multiple-return.md → 07-naming-multiple-return.md} +1 -0
- package/skills/developing-cpp/references/naming/examples/08-naming-player-value.md +16 -0
- package/skills/developing-cpp/references/naming/examples/09-naming-predicate.md +19 -0
- package/skills/developing-cpp/references/naming/examples/{naming-rename.md → 10-naming-rename.md} +1 -0
- package/skills/developing-cpp/references/naming/examples/{naming-responsibility.md → 11-naming-responsibility.md} +1 -0
- package/skills/developing-cpp/references/naming/examples/{naming-role.md → 12-naming-role.md} +2 -1
- package/skills/developing-cpp/references/naming/examples/13-naming-state-change.md +70 -0
- package/skills/developing-cpp/references/naming/glossary/01-naming-terms.md +27 -0
- package/skills/developing-cpp/references/naming/glossary/02-position.md +24 -0
- package/skills/developing-cpp/references/naming/glossary/03-selection.md +23 -0
- package/skills/developing-cpp/references/naming/glossary/04-square.md +48 -0
- package/skills/developing-cpp/references/naming/glossary/05-scoring.md +27 -0
- package/skills/developing-cpp/references/naming/glossary/06-difficulty.md +26 -0
- package/skills/developing-cpp/references/style/{google-cpp-style.md → 01-google-cpp-style.md} +16 -9
- package/skills/developing-cpp/scripts/checking-comments.mjs +116 -9
- package/skills/developing-cpp/scripts/checking-declaration-order.mjs +27 -1
- package/skills/developing-cpp/steps/01-behavior.md +5 -5
- package/skills/developing-cpp/steps/02-boundary.md +6 -6
- package/skills/developing-cpp/steps/03-declaration.md +7 -7
- package/skills/developing-cpp/steps/04-implementation.md +16 -16
- package/skills/engineering/SKILL.md +19 -19
- package/skills/engineering/references/cmake/linking/01-libraries.md +7 -4
- package/skills/engineering/references/cmake/linking/02-rpath.md +1 -0
- package/skills/engineering/references/cmake/linking/03-undefined-reference.md +3 -2
- package/skills/engineering/references/cmake/linking/04-inspect.md +1 -1
- package/skills/engineering/references/cmake/modules/{functions.md → 01-functions.md} +4 -3
- package/skills/engineering/references/cmake/{standard/cxx-standard.md → standards/01-cxx-standard.md} +2 -1
- package/skills/engineering/references/cmake/{standard/examples/standard.md → standards/examples/01-standard.md} +2 -1
- package/skills/engineering/references/cmake/{standard/standards/README.md → standards/index.md} +22 -21
- package/skills/engineering/references/cmake/standards/versions.md +44 -0
- package/skills/engineering/references/cmake/targets/{rules.md → 01-target-rules.md} +34 -5
- package/skills/engineering/references/cmake/targets/examples/{rpath.md → 01-rpath.md} +1 -0
- package/skills/engineering/references/cmake/testing/{ctest.md → 01-ctest.md} +4 -3
- package/skills/engineering/references/formatting/01-encoding.md +2 -1
- package/skills/engineering/references/formatting/02-clang-format.md +4 -3
- package/skills/engineering/references/formatting/03-clang-tidy.md +3 -2
- package/skills/engineering/references/formatting/04-clangd.md +2 -0
- package/skills/engineering/references/formatting/examples/{clang-format.md → 01-clang-format.md} +2 -1
- package/skills/engineering/references/formatting/examples/{clang-tidy.md → 02-clang-tidy.md} +1 -0
- package/skills/engineering/references/formatting/examples/{clangd.md → 03-clangd.md} +1 -0
- package/skills/engineering/references/index.md +32 -0
- package/skills/engineering/{references/formatting/scripts → scripts}/checking-encoding.mjs +6 -7
- package/skills/engineering/steps/01-targets.md +5 -4
- package/skills/engineering/steps/02-modules.md +3 -3
- package/skills/engineering/steps/03-check.md +3 -2
- package/skills/shared/budget.md +20 -0
- package/skills/{developing-cpp/references/editing/01-edit-operations.md → shared/operations.md} +8 -4
- package/skills/{developing-cpp/references/editing/02-rule-priority.md → shared/rule-priority.md} +2 -1
- package/skills/building-cpp/project/vscode/01-extensions.md +0 -28
- package/skills/developing-cpp/references/comments/examples/declaration-enum.md +0 -48
- package/skills/developing-cpp/references/comments/examples/declaration-function.md +0 -127
- package/skills/developing-cpp/references/comments/examples/declaration-predicate.md +0 -14
- package/skills/developing-cpp/references/comments/examples/implementation-body.md +0 -64
- package/skills/developing-cpp/references/comments/examples/implementation-todo.md +0 -16
- package/skills/developing-cpp/references/comments/glossary/comment-terms.md +0 -93
- package/skills/developing-cpp/references/design/examples/move-result.md +0 -39
- package/skills/developing-cpp/references/design/examples/preview-read.md +0 -40
- package/skills/developing-cpp/references/naming/examples/naming-accessor.md +0 -17
- package/skills/developing-cpp/references/naming/examples/naming-basic.md +0 -3
- package/skills/developing-cpp/references/naming/examples/naming-lower-layer.md +0 -39
- package/skills/developing-cpp/references/naming/examples/naming-player-value.md +0 -16
- package/skills/developing-cpp/references/naming/examples/naming-predicate.md +0 -18
- package/skills/developing-cpp/references/naming/examples/naming-state-change.md +0 -57
- package/skills/developing-cpp/references/naming/identifier-names.md +0 -104
- package/skills/engineering/references/cmake/standard/standards/c++11/README.md +0 -50
- package/skills/engineering/references/cmake/standard/standards/c++14/README.md +0 -48
- package/skills/engineering/references/cmake/standard/standards/c++17/README.md +0 -50
- package/skills/engineering/references/cmake/standard/standards/c++20/README.md +0 -50
- package/skills/engineering/references/cmake/standard/standards/c++23/README.md +0 -50
- package/skills/engineering/references/cmake/targets/examples/glob.md +0 -22
- package/skills/engineering/references/cmake/targets/examples/target.md +0 -156
- /package/skills/engineering/references/cmake/{standard/standards → standards}/c++11/cmake-snippet.cmake +0 -0
- /package/skills/engineering/references/cmake/{standard/standards → standards}/c++14/cmake-snippet.cmake +0 -0
- /package/skills/engineering/references/cmake/{standard/standards → standards}/c++17/cmake-snippet.cmake +0 -0
- /package/skills/engineering/references/cmake/{standard/standards → standards}/c++20/cmake-snippet.cmake +0 -0
- /package/skills/engineering/references/cmake/{standard/standards → standards}/c++23/cmake-snippet.cmake +0 -0
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
<!-- 读取时机:查语言细节时;读完能回答:`const`、`constexpr`、`consteval`、`constinit`;预算:一次只开这一份(25 行),不自动串联。 -->
|
|
1
2
|
# `const`、`constexpr`、`consteval`、`constinit`
|
|
2
3
|
|
|
3
4
|
| 关键字 | 承诺 | 典型位置 |
|
|
@@ -5,21 +6,21 @@
|
|
|
5
6
|
| `const` | 初始化后不能通过这个名字修改 | 局部变量、参数、成员函数尾部 |
|
|
6
7
|
| `constexpr` | 能在编译期求值(满足条件时) | 常量、函数、构造函数 |
|
|
7
8
|
| `consteval` | 必须在编译期求值 | 编译期校验函数 |
|
|
8
|
-
| `constinit` |
|
|
9
|
+
| `constinit` | 静态或线程存储期变量在编译期完成初始化 | 全局、静态与 `thread_local` 变量 |
|
|
9
10
|
| `inline constexpr` | 头文件里可共享的编译期常量,避免 ODR 问题 | 命名空间作用域常量 |
|
|
10
11
|
|
|
11
12
|
- 共享常量写在头文件的命名空间作用域:`inline constexpr int kMaxRows = 3;`,不要用 `#define`。`.cpp` 里非 `inline`、非 `extern` 的命名空间作用域 `constexpr` 已有内部链接。
|
|
12
|
-
- `constexpr`
|
|
13
|
+
- `constexpr` 函数在首次声明处隐式 `inline`,用到它的翻译单元都必须看见定义,所以定义放头文件;只写在 `.cpp` 里会让其他翻译单元的调用成为不要求诊断的不合规程序。
|
|
13
14
|
- 不要给所有函数加 `constexpr`;只有确实需要在编译期使用、且实现满足约束时才加。
|
|
14
15
|
- `constinit` 只保证初始化时机,不保证不可修改;需要不可修改时同时写 `const`。
|
|
15
|
-
- C++
|
|
16
|
+
- C++14 起 `constexpr` 函数就可以包含循环、局部变量与 `if`/`switch`;C++20 起还允许编译期分配,但分配要在同一次求值内释放。也不要把运行期逻辑塞进编译期。
|
|
16
17
|
- 局部变量默认不加 `const`:短生命周期的局部值只读一次,`const` 不改变存储与运行行为。把 `const` 留给接口契约——`const` 引用参数、`const` 成员函数、指向只读数据的指针与视图,以及确实要防止后续误写的变量。Core Guidelines 的 Con.1 主张默认不可变;本仓取 Google 风格的做法,只在接口层要求 `const`。
|
|
17
18
|
|
|
18
19
|
## 依据
|
|
19
20
|
|
|
20
21
|
- [ISO C++ `[dcl.constexpr]`](https://eel.is/c++draft/dcl.constexpr):`constexpr`、`consteval` 的求值时机与函数约束。
|
|
21
22
|
- [cppreference: `constexpr`](https://en.cppreference.com/w/cpp/language/constexpr):常量表达式、`constinit` 与 `consteval` 的对照。
|
|
22
|
-
- [cppreference: `constinit`](https://en.cppreference.com/w/cpp/language/constinit)
|
|
23
|
+
- [cppreference: `constinit`](https://en.cppreference.com/w/cpp/language/constinit):静态与线程存储期的初始化时机,不改变可修改性。
|
|
23
24
|
- [Google C++ Style Guide: Use of const](https://google.github.io/styleguide/cppguide.html#Use_of_const):「We strongly recommend using `const` in APIs (i.e., on function parameters, methods, and non-local variables) wherever it is meaningful and accurate.」局部变量不在要求之内。
|
|
24
25
|
- [Chromium C++ style](https://chromium.googlesource.com/chromium/src/+/main/styleguide/c++/c++.md) 与 [LLVM Coding Standards](https://llvm.org/docs/CodingStandards.html):同样没有要求局部变量加 `const`;LLVM 只对静态与全局常量用 `constexpr`。
|
|
25
26
|
- [C++ Core Guidelines: Con.1](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#rconst-immutable):主张默认不可变;本仓只在接口处采用这一主张,局部变量按上一条处理。
|
|
@@ -1,9 +1,14 @@
|
|
|
1
|
+
<!-- 读取时机:查语言细节时;读完能回答:表达式与比较;预算:一次只开这一份(31 行),不自动串联。 -->
|
|
1
2
|
# 表达式与比较
|
|
2
3
|
|
|
3
4
|
- 比较写成“变量在左、常量在右”:`if (id < kTaskCount)`,不写 Yoda 条件 `if (kTaskCount > id)`;常量与枚举值总在右边。成员与参数比较时,成员状态在左、参数在右,如 `if (current_player_ != move.player)`:判断「轮到谁」以对象自己的状态为起点,参数是被核对的一次输入;成员与常量比较时成员在左,如 `if (value_ != SquareValue::kEmpty)`;参数与常量比较时参数在左。分不清主次时按“读起来的主语”决定,同一段代码保持同一种顺序。
|
|
4
|
-
- 同一个值对多个离散取值分派时用 `switch`,不用一串 `if`:值来自枚举或整数常量、分支 3
|
|
5
|
-
-
|
|
6
|
-
-
|
|
5
|
+
- 同一个值对多个离散取值分派时用 `switch`,不用一串 `if`:值来自枚举或整数常量、分支 3 个以上、分支体短。
|
|
6
|
+
- `if` / `for` / `while` / `do` 即使只有一条语句也写花括号。`case` 不照搬:单条 `return`、`break`、`continue` 或 `throw` 不加花括号;这一支有声明,或不止一条语句时,才用花括号把声明收进这一支。连续几个 `case` 共用一条语句时不加花括号,也不写 `[[fallthrough]]`。
|
|
7
|
+
- 枚举的全部分支都写了 `case` 时不写 `default`。需要出口时,`std::unreachable()` 放在 `switch` 右花括号之后、紧挨着它,中间不空行。头文件是 `<utility>`,C++23;更低标准写 `assert(false)` 或 `std::abort()`。不放进 `default`,也不写 `throw` 兜底。`default` 会让 `-Wswitch` 不再报告新增枚举值。
|
|
8
|
+
- 条件不是枚举,或枚举只处理一部分常见值时,写 `default` 并在里面做真正的处理。域已经在前面收窄、又不是枚举时,才在 `default` 里写 `std::unreachable()`。非法枚举值要报告时,在 `switch` 后写正常返回,不用 `std::unreachable()`。不依赖隐式穿透;确实要穿透时写 `[[fallthrough]]`。
|
|
9
|
+
- 条件是比较范围、布尔组合,或只有 1–2 个分支时用 `if`;一个函数里有多条拒绝分支时,每条各写一条独立 `if` 并提前返回,成功路径留在函数末尾,不串 `else if`,也不用 `else` 包住。
|
|
10
|
+
- 否定按「类型有什么谓词」决定,不按个人习惯:类型提供 `IsEmpty()`、`IsFull()` 这类肯定式查询时,条件直接从肯定式写起,如 `if (square.IsEmpty())`。被拒绝的情形没有对应谓词时才用 `!`,如占用检查只能写成 `if (!square.IsEmpty())`;这种 `!` 只套一层,不写 `!!`,也不把 `!(value != SquareValue::kEmpty)` 这类双重否定留给读者。枚举比较同样先写正向判断:判空写 `value == SquareValue::kEmpty`,判非空写 `value != SquareValue::kEmpty`。同一个状态的比较只写在谓词定义里;调用处写 `IsInProgress()` 或 `!IsInProgress()`,不把 `GetResult() != GameResult::kInProgress` 再写一遍。
|
|
11
|
+
- 同一类值有 3 个以上时用数组加下标,不写 `value0`、`value1`、`value2`:`std::array<SquareValue, 3> values{};` 配合 `values[i]`;成组的常量数据写成 `std::array<Line, 8> kLines` 再由循环消费。只有 1–2 个语义各自独立的值才各起名字。
|
|
7
12
|
- 混合优先级的算术表达式加括号:`(a + b) * c`,不依赖读者记忆优先级。`readability-math-missing-parentheses` 会检查。
|
|
8
13
|
- 区间用半开区间 `[low, high)`;判空用 `empty()`,不要用 `size() == 0`。
|
|
9
14
|
- 逻辑表达式按语义分组,一组一个括号;组内的单个比较不再加括号,`&&` 的优先级已经清楚,多加一层反而难核对。
|
|
@@ -16,8 +21,12 @@
|
|
|
16
21
|
- [cppreference: Expressions](https://en.cppreference.com/w/cpp/language/expressions):运算符分类、短路与序列点规则。
|
|
17
22
|
- [C++ Core Guidelines: ES.41](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#s-expr):避免容易误读的表达式,必要时加括号。
|
|
18
23
|
- [Style Guide for Writing Comparisons in Conditions](https://www.enyo.de/fw/notes/style-guide-conditions.html):变化最快、最不确定的一方写在比较左边;常量前置已无必要,且常掩盖本意。
|
|
19
|
-
- [clang-tidy `readability-constant-operand-order`](https://github.com/llvm/llvm-project/pull/167158)
|
|
24
|
+
- [提议中的 clang-tidy 检查 `readability-constant-operand-order`](https://github.com/llvm/llvm-project/pull/167158):仍为 open 的 PR #167158,尚未合入;其方向是现代代码库统一写 `x == 0`,不写 `0 == x`。
|
|
20
25
|
- [LLVM Coding Standards](https://llvm.org/docs/CodingStandards.html) 与 [Linux kernel coding style](https://www.kernel.org/doc/html/latest/process/coding-style.html):示例都用自然语序(`I != E`、`system_state == SYSTEM_RUNNING`),不把常量前置。
|
|
21
26
|
- [C++ Core Guidelines: ES.70、ES.78、ES.79](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#s-expr):有选择时优先 `switch`;对常量比较,通常比 `if`-`then`-`else` 链更容易优化,并能提示枚举是否覆盖全;不依赖隐式穿透,`default` 只处理常见情况。
|
|
22
27
|
- [LLVM Coding Standards: Don't use default labels in fully covered switches over enumerations](https://llvm.org/docs/CodingStandards.html#don-t-use-default-labels-in-fully-covered-switches-over-enumerations):全覆盖枚举 `switch` 不写 `default`;需要给编译器出口时在 `switch` 后用 `llvm_unreachable`(对应本项目的 `std::unreachable`)。
|
|
23
28
|
- [Google C++ Style Guide: switch statements](https://google.github.io/styleguide/cppguide.html#switch-statements):非枚举条件的 `switch` 必须有 `default`;枚举由编译器提醒未覆盖值;`[[fallthrough]]` 必须标注。
|
|
29
|
+
- 花括号与 `default` 的取舍:[Mozilla C++ 风格](https://firefox-source-docs.mozilla.org/code-quality/coding-style/coding_style_cpp.html) 只在 `case` 声明局部变量时加花括号;[clang-tidy `readability-braces-around-statements`](https://clang.llvm.org/extra/clang-tidy/checks/readability/braces-around-statements.html) 与 clang-format 的 `InsertBraces` 不检查 `case`;[cppreference `std::unreachable`](https://en.cppreference.com/w/cpp/utility/unreachable) 的 `default` 例子是整数而不是枚举;[MISRA C++:2023 9.4.2](https://www.mathworks.com/help/bugfinder/ref/misracpp2023rule9.4.2.html) 要求 `default`,本项目按 LLVM/Google 对封闭枚举的取舍不跟这条,范围外值由程序保证不出现。
|
|
30
|
+
- 关于多分支与提前返回:[LLVM Coding Standards](https://llvm.org/docs/CodingStandards.html) 要求 `return`、`break`、`continue`、`goto` 之后不写 `else`,[C++ Core Guidelines F.56](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#f-nesting) 把 `return` 之后的 `else` 列为冗余,[clang-tidy `readability-else-after-return`](https://clang.llvm.org/extra/clang-tidy/checks/readability/else-after-return.html) 直接检查 `return`、`break`、`continue`、`throw` 之后的 `else`。本工作流按这个方向写:每条拒绝各写一条独立 `if` 并提前返回,成功路径留在末尾;不同拒绝原因之间没有先后依赖,不串 `else if`。
|
|
31
|
+
- 肯定/否定谓词:[Microsoft Framework Design Guidelines: Names of Type Members](https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/names-of-type-members) 要求布尔成员用肯定短语(`CanSeek`,不用 `CantSeek`),可加 `Is`、`Can`、`Has`。C++ Core Guidelines、LLVM 与 Google 都没有规定条件的极性,所以「默认肯定式」在本工作流属于项目约定,Microsoft 的条文只作跨语言旁证。
|
|
32
|
+
- 3 个以上同类值用数组加下标:没有一手规范规定这一点,属于本工作流的项目约定;理由是同一段循环只写一次,新增一项不必再起一个名字。
|
|
@@ -1,15 +1,18 @@
|
|
|
1
|
+
<!-- 读取时机:查语言细节时;读完能回答:函数、参数与返回值;预算:一次只开这一份(17 行),不自动串联。 -->
|
|
1
2
|
# 函数、参数与返回值
|
|
2
3
|
|
|
3
|
-
- 命名规则以 `references/naming/identifier-names.md` 为准:函数 PascalCase、查询 `Is` / `Has` / `Can` / `Get` / `Find`、转换 `SourceToTarget`。
|
|
4
|
+
- 命名规则以 `references/naming/01-identifier-names.md` 为准:函数 PascalCase、查询 `Is` / `Has` / `Can` / `Get` / `Find`、转换 `SourceToTarget`。
|
|
4
5
|
- 输入参数默认按值传小对象、按 `const&` 传大对象;需要修改调用方对象时用引用参数,并在名字或注释里标 `[out]`。
|
|
5
6
|
- 优先返回值而不是输出参数;需要返回多个值时用 `struct` 聚合,不用多个 `bool` 输出参数。
|
|
6
7
|
- 返回容器或大对象时按值返回,依赖 RVO / 移动;不要返回局部对象的引用或指针。
|
|
7
8
|
- 单参数构造函数加 `explicit`,除非确实需要隐式转换;转换运算符也加 `explicit`。
|
|
8
9
|
- 不在虚函数上写默认参数;默认参数在静态类型处绑定,派生类改默认值会造成意外。
|
|
9
10
|
- 函数只做一件事;超过一屏时先找可命名的子步骤,分阶段与注释写法见 `steps/04-implementation.md`。
|
|
11
|
+
- lambda 捕获按需列名,不默认写 `[this]`:只在确实要读写成员时才捕获 `this`,只读个别值时按名捕获,纯计算写 `[]`。需要成员访问时优先把要用的值取到局部变量再捕获;捕获 `this` 的 lambda 不得比对象活得更久,异步或存储回调前先复制需要的值。
|
|
10
12
|
|
|
11
13
|
## 依据
|
|
12
14
|
|
|
13
15
|
- [ISO C++ `[dcl.fct]`](https://eel.is/c++draft/dcl.fct):函数声明、参数与返回类型。
|
|
14
16
|
- [cppreference: Functions](https://en.cppreference.com/w/cpp/language/functions):重载、默认参数与返回类型推导。
|
|
15
17
|
- [Google C++ Style Guide: Functions](https://google.github.io/styleguide/cppguide.html#Functions):短小、单一职责,参数与返回值写清契约。
|
|
18
|
+
- [Google C++ Style Guide: Lambda expressions](https://google.github.io/styleguide/cppguide.html#Lambda_expressions):优先显式捕获、按值捕获要用的对象;捕获 `this` 的 lambda 不能比对象活得更久。
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
<!-- 读取时机:查语言细节时;读完能回答:检查清单;预算:一次只开这一份(11 行),不自动串联。 -->
|
|
1
2
|
# 检查清单
|
|
2
3
|
|
|
3
4
|
- [ ] 初始化方式统一,窄化转换被编译器拦住。
|
|
@@ -5,6 +6,7 @@
|
|
|
5
6
|
- [ ] 共享常量用 `inline constexpr`,没有宏常量。
|
|
6
7
|
- [ ] 局部变量不加 `const`;`const` 用在参数、成员函数与只读视图上。
|
|
7
8
|
- [ ] 比较不是 Yoda 形式;成员与参数比较时成员状态在左、参数在右;混合优先级有括号。
|
|
9
|
+
- [ ] 枚举全覆盖的 `switch` 不写 `default`,单行 `case` 无花括号,出口在 `switch` 后。
|
|
8
10
|
- [ ] 资源由 RAII 管理,没有裸 `new` / `delete`。
|
|
9
11
|
- [ ] 特殊成员声明与定义位置正确,移动操作 `noexcept`。
|
|
10
12
|
- [ ] 模板约束用概念表达,定义在头文件。
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
<!-- 读取时机:查语言细节时;读完能回答:访问与边界;预算:一次只开这一份(14 行),不自动串联。 -->
|
|
1
2
|
# 访问与边界
|
|
2
3
|
|
|
3
4
|
- `operator[]` 不检查边界,越界是未定义行为;`at` 检查并抛 `std::out_of_range`。规则与家族差异见 `references/language/value-types/09-subscript.md`。
|
|
4
|
-
- `vector::front()` / `back()`
|
|
5
|
+
- `vector::front()` / `back()` 在空容器上未定义;`data()` 在空容器上返回的指针不能解引用。先 `empty()`。
|
|
5
6
|
- `map::operator[]` 缺键会插入默认值;只读查询用 `find`、`contains`(C++20)或 `at`。
|
|
6
7
|
- `string_view` 不保证以 `\0` 结尾;传给需要 C 字符串的 API 前先转成 `std::string`。
|
|
7
8
|
- `span` 不拥有元素,也不延长生命周期;不要从临时容器构造后保存。
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
<!-- 读取时机:查语言细节时;读完能回答:`std::format` 与输出;预算:一次只开这一份(116 行),不自动串联。 -->
|
|
1
2
|
# `std::format` 与输出
|
|
2
3
|
|
|
3
4
|
- 组合消息优先 `std::format`;它返回 `std::string`,不直接写流。
|
|
@@ -5,7 +6,7 @@
|
|
|
5
6
|
- 格式串里的 `{}` 按顺序取参数;需要位置时写 `{0}`、`{1}`。
|
|
6
7
|
- 自定义类型要格式化时,特化 `std::formatter<T>`;写法与边界见下面的「自定义类型的格式化」。
|
|
7
8
|
- 浮点默认格式可能与预期不同;需要固定精度时写 `{:.2f}`,需要科学计数时写 `{:e}`。
|
|
8
|
-
- 异常消息、日志、用户界面文本都通过 `std::format` 组合,不手工拼接数字和字符串。异常消息的组织方式见 `references/exceptions/
|
|
9
|
+
- 异常消息、日志、用户界面文本都通过 `std::format` 组合,不手工拼接数字和字符串。异常消息的组织方式见 `references/exceptions/01-exception-rules.md` 的「消息」节;面向用户的中文只留在程序入口的前缀里。
|
|
9
10
|
|
|
10
11
|
## 自定义类型的格式化
|
|
11
12
|
|
|
@@ -33,7 +34,7 @@
|
|
|
33
34
|
|
|
34
35
|
```cpp
|
|
35
36
|
template <>
|
|
36
|
-
struct std::formatter<
|
|
37
|
+
struct std::formatter<game::Player> {
|
|
37
38
|
constexpr auto parse(std::format_parse_context& ctx) {
|
|
38
39
|
if (ctx.begin() != ctx.end() && *ctx.begin() != '}') {
|
|
39
40
|
throw std::format_error("Player: format specifier is not supported, use {} instead");
|
|
@@ -42,11 +43,11 @@ struct std::formatter<tictactoe::Player> {
|
|
|
42
43
|
}
|
|
43
44
|
|
|
44
45
|
template <typename FormatContext>
|
|
45
|
-
auto format(
|
|
46
|
+
auto format(game::Player player, FormatContext& ctx) const {
|
|
46
47
|
switch (player) {
|
|
47
|
-
case
|
|
48
|
+
case game::Player::kX:
|
|
48
49
|
return std::format_to(ctx.out(), "X");
|
|
49
|
-
case
|
|
50
|
+
case game::Player::kO:
|
|
50
51
|
return std::format_to(ctx.out(), "O");
|
|
51
52
|
}
|
|
52
53
|
return std::format_to(ctx.out(), "Player({})", static_cast<int>(player));
|
|
@@ -54,6 +55,8 @@ struct std::formatter<tictactoe::Player> {
|
|
|
54
55
|
};
|
|
55
56
|
```
|
|
56
57
|
|
|
58
|
+
这是在报告非法值,不是 `std::unreachable()`;全覆盖且范围外算程序错误时仍按 `references/language/language-basics/05-expressions.md`。
|
|
59
|
+
|
|
57
60
|
## 枚举值与整数输出
|
|
58
61
|
|
|
59
62
|
`std::format` 没有为枚举提供内建 formatter;没有特化时 `std::format("{}", Player::kX)` 不能编译。枚举的数值回退要先把值转成整数,再按整数的格式说明输出;转换目标不同,`{}` 的结果可能不同。
|
|
@@ -80,7 +83,7 @@ return std::format_to(ctx.out(), "Player({:d})", std::to_underlying(player));
|
|
|
80
83
|
|
|
81
84
|
// C++20
|
|
82
85
|
return std::format_to(ctx.out(), "Player({:d})",
|
|
83
|
-
static_cast<std::underlying_type_t<
|
|
86
|
+
static_cast<std::underlying_type_t<game::Player>>(player));
|
|
84
87
|
```
|
|
85
88
|
|
|
86
89
|
## 标准、提案与生态
|
|
@@ -94,6 +97,14 @@ return std::format_to(ctx.out(), "Player({:d})",
|
|
|
94
97
|
- LLVM `formatv`:自定义用 `format_provider<T>` 特化;文档没有给出枚举专用入口。
|
|
95
98
|
- 结论:C++23 项目继续用 `std::formatter` 特化;数值回退优先 `static_cast<int>`(值域小)或 `std::to_underlying` + `{:d}`(值域大或底层不定);不要为枚举继承 `formatter<std::string_view>`。
|
|
96
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
|
+
|
|
97
108
|
## 是否继承 `std::formatter<std::string_view>`
|
|
98
109
|
|
|
99
110
|
- 不需要。启用的特化只要满足 BasicFormatter:本身可默认构造、拷贝、赋值、析构、交换,并提供 `parse` 与 `format`;标准不要求继承任何 formatter。
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
<!-- 读取时机:查语言细节时;读完能回答:随机;预算:一次只开这一份(14 行),不自动串联。 -->
|
|
1
2
|
# 随机
|
|
2
3
|
|
|
3
4
|
- 随机数用 `<random>` 的引擎 + 分布;不要用 `rand()` / `random_shuffle`。
|
|
4
5
|
- 引擎只播种一次并复用;每次调用都重新播种会让序列重复。线程各自持有引擎,或加锁。
|
|
5
6
|
- 需要均匀整数用 `uniform_int_distribution`;不要写 `engine() % n`,取模有偏。
|
|
6
7
|
- `random_device` 只用于播种,不保证密码学安全;可复现实验用固定种子或 `seed_seq`。
|
|
8
|
+
- 多个随机种子用 `std::array` + `std::generate(..., std::ref(device))` 生成,不连续写多个 `device()`;种子数量超过 3 个时尤其如此。
|
|
7
9
|
- 洗牌用 `std::shuffle(first, last, engine)`。
|
|
8
10
|
|
|
9
11
|
## 依据
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
<!-- 读取时机:查语言细节时;读完能回答:枚举类;预算:一次只开这一份(24 行),不自动串联。 -->
|
|
1
2
|
# 枚举类
|
|
2
3
|
|
|
3
4
|
`enum class` 定义封闭取值集合:
|
|
@@ -11,9 +12,9 @@
|
|
|
11
12
|
|
|
12
13
|
- 新代码用 `enum class`;unscoped `enum` 只用于兼容 C 接口,不作为默认写法。
|
|
13
14
|
- 需要固定布局、序列化或前向声明时写底层类型;本项目的封闭状态码默认写 `: std::uint8_t`,普通枚举用默认底层类型。改动枚举值或底层类型要同步序列化、测试与注释。
|
|
14
|
-
- 枚举值的行尾注释要么全写要么全不写;两种句式与禁止项见 `references/comments/comment-rules.md`「枚举项与 `struct` 短字段:两种句式」。
|
|
15
|
+
- 枚举值的行尾注释要么全写要么全不写;两种句式与禁止项见 `references/comments/01-comment-rules.md`「枚举项与 `struct` 短字段:两种句式」。
|
|
15
16
|
- 需要“编号”与“名字”两套表示时,写显式映射函数,例如 `TaskStateToName` / `TaskStateFromName`,不要在枚举和整数之间来回隐式转换。
|
|
16
|
-
- 编号按“正常优先”排:0
|
|
17
|
+
- 编号按“正常优先”排:0 表示正常态——空、进行中或成功;失败与异常态随后,按固定顺序排列。带缺省语义的枚举(`kEmpty`、`kInProgress`)和返回结果的枚举(`kPlaced`、`kOccupiedSquare`)因此共用同一条约定。身份枚举按领域编号,如 `Player::kX = 1`、`Player::kO = 2`,不套用 0 = 正常态。
|
|
17
18
|
- 编号只用于输出和既有接口;要从编号找回名字,仍走显式映射函数,不拿编号做算术。
|
|
18
19
|
|
|
19
20
|
## 标准与推荐
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
<!-- 读取时机:查语言细节时;读完能回答:`[[nodiscard]]`;预算:一次只开这一份(15 行),不自动串联。 -->
|
|
1
2
|
# `[[nodiscard]]`
|
|
2
3
|
|
|
3
4
|
- 只给“结果携带信息、忽略会出错”的函数:查询型 `const` 成员与返回结果对象的 API。
|
|
@@ -12,4 +13,4 @@
|
|
|
12
13
|
|
|
13
14
|
- ISO C++ `[dcl.attr.nodiscard]`:`[[nodiscard]]` 的位置与可应用的对象。https://eel.is/c++draft/dcl.attr.nodiscard
|
|
14
15
|
- cppreference `[[nodiscard]]`:函数、类与枚举上的用法。https://en.cppreference.com/w/cpp/language/attributes/nodiscard
|
|
15
|
-
- 本项目的 Clang 验证(C++23):类内 `[[nodiscard]] friend`、`friend [[nodiscard]]
|
|
16
|
+
- 本项目的 Clang 验证(C++23):类内 `[[nodiscard]] friend`、`friend [[nodiscard]]`、参数列表后的写法都会被拒绝;类内只有返回类型前的普通函数声明可加。类声明本身在 C++17 起可标 `[[nodiscard]]`,构造函数在 C++20 起可标。不要把“能编译”当成标准保证。
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
<!-- 读取时机:查语言细节时;读完能回答:比较运算符;预算:一次只开这一份(10 行),不自动串联。 -->
|
|
1
2
|
# 比较运算符
|
|
2
3
|
|
|
3
4
|
- 默认比较只给调用方真的会比较的值类型:`Position`、`Move` 这类坐标与记录可以默认比较;`Board` 这类实体在正常流程里不比较,不因为成员可比较就提供 `operator==`。测试要比较实体时,逐格或逐字段比较它的可观察状态,不把比较运算符加进生产接口。
|
|
@@ -7,4 +8,4 @@
|
|
|
7
8
|
|
|
8
9
|
- ISO C++ `[class.compare.default]`:默认 `operator==` 与 `operator<=>` 的逐成员语义。https://eel.is/c++draft/class.compare.default
|
|
9
10
|
- ISO C++ `[expr.eq]`:相等比较的类型与结果规则。https://eel.is/c++draft/expr.eq
|
|
10
|
-
- 需要排序或三路比较时写 `auto operator<=>(const T&) const = default
|
|
11
|
+
- 需要排序或三路比较时写 `auto operator<=>(const T&) const = default;`,不要手写四个关系运算符;默认的 `<=>` 会同时隐式声明默认的 `==`(`!=` 再由 `==` 改写),只有自己手写 `<=>` 时才要单独提供 `==`。默认 `<=>` 的推导类型 `std::strong_ordering` 来自 `<compare>`,使用前先包含它。只给真正会比较的值类型提供比较,实体类不因成员可比较就获得比较运算符。https://en.cppreference.com/w/cpp/language/operator_comparison
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
<!-- 读取时机:查语言细节时;读完能回答:下标与 `at`;预算:一次只开这一份(15 行),不自动串联。 -->
|
|
1
2
|
# 下标与 `at`
|
|
2
3
|
|
|
3
4
|
同一容器上,`operator[]` 与 `at` 的失败方式不同:
|
|
@@ -12,4 +13,4 @@
|
|
|
12
13
|
- 已经证明非负的坐标在 `[]` 前收成 `std::size_t`:写 `static_cast<std::size_t>(index)`,不要先转成 `int`;默认仍用 `at()`,只有选择 `[]` 时才做这一步。
|
|
13
14
|
- 行列下标是乘加结果时,先把各操作数拓宽再乘加,不要先乘加再转换;见 `references/language/value-types/03-integer-roles.md`。
|
|
14
15
|
- `map::operator[]` 会插入,不是“读取可能缺失的键”;只读查询用 `find` / `contains` / `at`。
|
|
15
|
-
- 契约失败的消息用 `std::format`,见 `references/exceptions/
|
|
16
|
+
- 契约失败的消息用 `std::format`,见 `references/exceptions/01-exception-rules.md`。
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
<!-- 读取时机:排声明顺序时;读完能回答:源码布局;预算:一次只开这一份(78 行),不自动串联。 -->
|
|
1
2
|
# 源码布局
|
|
2
3
|
|
|
3
4
|
布局服务于边界:库是纯的,程序是薄的,测试从公共接口进入。这里只定目录和文件职责;CMake target 由 `engineering` 的 `steps/01-targets.md` 定。
|
|
@@ -7,7 +8,7 @@
|
|
|
7
8
|
```text
|
|
8
9
|
<component>/
|
|
9
10
|
include/
|
|
10
|
-
<name>.h
|
|
11
|
+
<name>.h 公共接口,包含时写裸文件名
|
|
11
12
|
src/
|
|
12
13
|
<name>.cpp 实现,可以包含私有头
|
|
13
14
|
<detail>.h 私有头,只被本组件 src 包含
|
|
@@ -53,7 +54,7 @@ app/
|
|
|
53
54
|
|
|
54
55
|
## 头文件契约
|
|
55
56
|
|
|
56
|
-
自包含、共享常量、`using namespace
|
|
57
|
+
自包含、共享常量、`using namespace`、模板实现与包含顺序的规则见 `references/style/01-google-cpp-style.md`「头文件」;扩展名与槽位见 `references/layout/02-file-template.md`。库不包含 `<iostream>` 的边界见本文「程序」节。本文只强调一条:公共头只暴露稳定契约,测试只包含公共头。
|
|
57
58
|
|
|
58
59
|
## 测试
|
|
59
60
|
|
|
@@ -76,4 +77,3 @@ app/
|
|
|
76
77
|
|
|
77
78
|
- [Google C++ Style Guide: File Names](https://google.github.io/styleguide/cppguide.html#File_Names):文件名全小写,`_` 或 `-` 分隔。
|
|
78
79
|
- [C++ Core Guidelines: NL.27](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#rl-file-suffix):代码文件用 `.cpp`,接口文件用 `.h`。
|
|
79
|
-
- [C++ Core Guidelines: SF.1](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#rs-file-suffix):按文件职责区分实现与接口。
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
<!-- 读取时机:排声明顺序时;读完能回答:文件模板;预算:一次只开这一份(137 行),不自动串联。 -->
|
|
1
2
|
# 文件模板
|
|
2
3
|
|
|
3
|
-
本文件是头文件与实现文件槽位的唯一权威。`references/style/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`,本文件不复制。
|
|
4
5
|
|
|
5
6
|
## 选择扩展名
|
|
6
7
|
|
|
@@ -22,24 +23,24 @@
|
|
|
22
23
|
|
|
23
24
|
- `PROJECT` 是源码树顶的项目名,全大写;`PATH` 是相对源码树顶的目录;`FILE` 是文件名;非字母数字改成下划线。
|
|
24
25
|
- 末尾固定 `_H_`;`#endif` 后两个空格,再写 `//` 和同一个宏。
|
|
25
|
-
-
|
|
26
|
+
- 源码树顶是 `my_project/`:
|
|
26
27
|
|
|
27
28
|
| 文件 | 守卫 |
|
|
28
29
|
| --- | --- |
|
|
29
|
-
| `
|
|
30
|
-
| `
|
|
31
|
-
| `
|
|
32
|
-
| `
|
|
33
|
-
| `
|
|
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/terminal.h` | `MY_PROJECT_INCLUDE_TERMINAL_H_` |
|
|
34
|
+
| `my_project/tests/computer_test_support.h` | `MY_PROJECT_TESTS_COMPUTER_TEST_SUPPORT_H_` |
|
|
34
35
|
|
|
35
36
|
- 通用示例:`taskflow/include/task_table.h` → `TASKFLOW_INCLUDE_TASK_TABLE_H_`。
|
|
36
|
-
- 不用 `
|
|
37
|
+
- 不用 `MY_PROJECT_BOARD_H` 这类省略路径的名字;两个同名头会撞宏。
|
|
37
38
|
|
|
38
39
|
## `.h` 与 `.hpp`
|
|
39
40
|
|
|
40
41
|
槽位顺序固定:
|
|
41
42
|
|
|
42
|
-
1. 文件头注释(格式见 `references/comments/comment-rules.md`,留在守卫外)
|
|
43
|
+
1. 文件头注释(格式见 `references/comments/01-comment-rules.md`,留在守卫外)
|
|
43
44
|
2. `#ifndef` / `#define`
|
|
44
45
|
3. 分组包含(组间空行,组内字母序)
|
|
45
46
|
4. 命名空间内:常量 → 枚举 → 结构体 → 自由函数 → 类
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
<!-- 读取时机:排声明顺序时;读完能回答:声明顺序;预算:一次只开这一份(150 行),不自动串联。 -->
|
|
1
2
|
# 声明顺序
|
|
2
3
|
|
|
3
4
|
顺序的作用是让读者先看类型和契约,再看数据。`clang-format` 不管顺序,靠人工和 review 维持。
|
|
@@ -23,29 +24,30 @@ class TaskTable {
|
|
|
23
24
|
~TaskTable();
|
|
24
25
|
|
|
25
26
|
// 5. 其他成员函数
|
|
26
|
-
[[nodiscard]] bool
|
|
27
|
+
[[nodiscard]] bool HasTask(int id) const;
|
|
27
28
|
[[nodiscard]] TaskState GetTaskState(int id) const;
|
|
28
29
|
void SetTaskState(int id, TaskState state);
|
|
29
30
|
|
|
30
31
|
// 6. 数据成员
|
|
31
32
|
private:
|
|
32
33
|
Tasks tasks_{};
|
|
33
|
-
bool is_open_
|
|
34
|
+
bool is_open_{false};
|
|
34
35
|
};
|
|
35
36
|
```
|
|
36
37
|
|
|
37
|
-
- `struct`
|
|
38
|
+
- `struct` 的数据成员紧跟类型之后;有 invariant 时改用 `class`。
|
|
38
39
|
- `public:` 在前,`private:` 在后;`private:` 前空一行,数据成员之间不空行。
|
|
39
40
|
- 静态常量用 `static constexpr`;跨文件共享的常量放命名空间并 `inline constexpr`。
|
|
40
41
|
- 工厂放在构造函数之前;返回 `TaskTable` 的工厂用 `Create` 开头,名字说明创建的是什么状态。
|
|
41
42
|
- 用户声明的特殊成员函数集中放第 4 组;`= default` 也写出来,读者才知道是故意默认。
|
|
42
|
-
-
|
|
43
|
+
- 不要把数据成员插在函数之间;数据成员放在最后一组,`struct` 是唯一例外(紧跟类型,见上)。
|
|
43
44
|
|
|
44
45
|
### 数据成员之间
|
|
45
46
|
|
|
46
47
|
- 数据成员按初始化依赖、逻辑分组和阅读顺序排列,不按字母序。初始化依赖是硬约束:先声明的成员先初始化,初始化器只能安全使用已经完成初始化的成员。
|
|
47
|
-
-
|
|
48
|
+
- 聚合类型的字段顺序参与聚合初始化与指定初始化器,也决定对象布局与 ABI;它不是 standard-layout 的成立条件。重排要同步所有 `T{a, b}` 调用点,并检查序列化或 ABI 假设。
|
|
48
49
|
- 先主值、后限定值,同类字段放一起。例如落子记录先放位置、再放玩家,读作「在哪、谁」。
|
|
50
|
+
- 互不依赖的成员按「读的时候先要哪样」排:`Game` 依次是 `current_player_`、`board_`、`history_`,读作「轮到谁、棋盘什么样、走过哪些手」;`board_` 是落子改动的主体,`history_` 是它的变更日志,所以排在最后。
|
|
49
51
|
- Google 的 Declaration Order 与 C++ Core Guidelines 的 NL.16 只规定访问级别和组间顺序,都没有字母序要求;沿用现有语义顺序即可。
|
|
50
52
|
- 团队若另定字母序,只作为语义相同时的最后兜底并写进项目规则,不和语义顺序混用。
|
|
51
53
|
|
|
@@ -54,9 +56,11 @@ class TaskTable {
|
|
|
54
56
|
Google、LLVM、OpenCV 都没有规定这一组内部的次序;本工作流补上:
|
|
55
57
|
|
|
56
58
|
- 有比较运算符时,开头是 `operator==`:成员默认比较和 `friend` 都放这里,然后才是谓词 `Is` / `Has` / `Can`、`Get`、普通函数按使用先后。`friend` 不再放到这一组末尾。
|
|
59
|
+
- `CheckInRange` 是诊断函数,紧挨 `IsInRange` 写,排在 `Get` 之前;它是本条谓词顺序的例外,见 `references/exceptions/01-exception-rules.md`。
|
|
57
60
|
- 多个直接返回成员的 `Get` 按对应数据成员的声明顺序排列,不按调用频率或字母序:成员依次是 `current_player_`、`board_`、`history_`,getter 就依次是 `GetCurrentPlayer()`、`GetBoard()`、`GetHistory()`;成员顺序调整时 getter 一起改,一个类里只留一套顺序。
|
|
58
|
-
- 直接返回成员的 `Get` 按成员顺序;没有独立成员的派生查询排在直接 `Get` 之后,按「先局部元素、后整体状态」阅读,不插进直接 `Get` 之间。例:`
|
|
61
|
+
- 直接返回成员的 `Get` 按成员顺序;没有独立成员的派生查询排在直接 `Get` 之后,按「先局部元素、后整体状态」阅读,不插进直接 `Get` 之间。例:`Game` 的 `GetResult()` 扫描棋盘后才得到结果,排在直接返回成员的 `GetCurrentPlayer()`、`GetBoard()`、`GetHistory()` 之后。
|
|
59
62
|
- 成对的局部/整体操作沿用同一方向:先 `ClearSquare(pos)` 清一格,再 `Clear()` 清整盘;局部操作在前,整体操作在后,不按调用频率互换。
|
|
63
|
+
- `constexpr` / `consteval` 函数不单独提前:它们属于「其他函数」组,按上面的语义次序排。`constexpr` 与 `consteval` 隐含 `inline`,这才使头文件就地定义可行;它们不决定声明位置;就地定义的短函数也留在该组,不因为写在头文件里就挪到类的最前。
|
|
60
64
|
- 比较运算符的签名与 `[[nodiscard]]` 规则见 `references/language/value-types/`,这里不另写一套。
|
|
61
65
|
- 同一类型上的谓词留在类内,不因为实现只转调公开函数就拆成类外自由函数。
|
|
62
66
|
- `friend` 类型仍跟其他类型放在一起,和 `friend` 函数不是同一组。
|
|
@@ -77,14 +81,14 @@ namespace taskflow {
|
|
|
77
81
|
|
|
78
82
|
inline constexpr int kTaskCount = 16;
|
|
79
83
|
|
|
80
|
-
enum class TaskState : std::uint8_t { kPending, kActive, kDone };
|
|
84
|
+
enum class TaskState : std::uint8_t { kPending = 0, kActive = 1, kDone = 2 };
|
|
81
85
|
|
|
82
86
|
struct TaskRecord {
|
|
83
|
-
int id
|
|
84
|
-
TaskState state
|
|
87
|
+
int id{0};
|
|
88
|
+
TaskState state{TaskState::kPending};
|
|
85
89
|
};
|
|
86
90
|
|
|
87
|
-
[[nodiscard]] bool
|
|
91
|
+
[[nodiscard]] bool HasTask(int id);
|
|
88
92
|
[[nodiscard]] std::string TaskStateToString(TaskState state);
|
|
89
93
|
|
|
90
94
|
class TaskTable {
|
|
@@ -104,7 +108,7 @@ class TaskTable {
|
|
|
104
108
|
## `.cpp` 的定义顺序
|
|
105
109
|
|
|
106
110
|
- 本工作流约定:与头文件声明同序,先类型和常量,再自由函数,再成员函数;成员函数按类内声明顺序。Google、LLVM 与 C++ Core Guidelines 都没有规定 `.cpp` 成员定义的次序;这里选同序,是为了让读者能在头文件与实现之间逐项对照。
|
|
107
|
-
- 文件头注释之后先放本文件的 `namespace taskflow {`,再放公共定义;常量仍排在具名命名空间内、成员函数之前;内部链接的写法按 `references/style/google-cpp-style.md`,本文不复述。
|
|
111
|
+
- 文件头注释之后先放本文件的 `namespace taskflow {`,再放公共定义;常量仍排在具名命名空间内、成员函数之前;内部链接的写法按 `references/style/01-google-cpp-style.md`,本文不复述。
|
|
108
112
|
- 构造函数、析构函数、拷贝/移动定义通常放在成员函数组最前;如果头文件把某个成员声明在构造函数之前,仍按头文件声明顺序,不把构造函数硬提到前面。`= default` 的定义放 `.cpp` 时紧跟声明顺序。
|
|
109
113
|
- 不要为了「先编译」重排定义;C++ 不要求定义顺序,读者顺序优先。
|
|
110
114
|
|
|
@@ -127,12 +131,21 @@ class TaskTable {
|
|
|
127
131
|
- 取回一个结果并按结果分支的语句连成一组(声明、`if`、`throw` / `return`),组内不空行;这一组与收尾(记录这一步、轮到对方、返回成功)之间空 1 行。
|
|
128
132
|
- 同一组语句不拆:三个校验之后的赋值与 `return` 之间不空行。
|
|
129
133
|
- 只有次序依赖的一对 `if`(`GetWinner`、`JudgeAfterPlace`)之间空 1 行。
|
|
134
|
+
- 互斥策略组与组之间空 1 行;`std::unreachable()` 紧跟 `switch` 的右花括号,中间不空行。位置理由不在这里重复。
|
|
135
|
+
- 循环结束后还有 `return` 时,循环与 `return` 之间空 1 行。
|
|
130
136
|
- 最多 1 行,不连续空两行。
|
|
131
137
|
|
|
138
|
+
### 头文件里的空行
|
|
139
|
+
|
|
140
|
+
- 类内六个组之间、同组函数之间、命名空间内顶层实体之间各空 1 行。
|
|
141
|
+
- 数据成员之间不空行;`private:` 之前空 1 行。
|
|
142
|
+
- 注释贴住它说明的声明,注释与声明之间不空行。
|
|
143
|
+
- Doxygen 列表项之间不插空注释行。
|
|
144
|
+
|
|
132
145
|
## 依据
|
|
133
146
|
|
|
134
147
|
- [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)
|
|
148
|
+
- [Google C++ Style Guide: Vertical Whitespace](https://google.github.io/styleguide/cppguide.html#Vertical_Whitespace):垂直留白越少越好;空行用来分隔逻辑分组,函数体首尾不留空行,函数体内不随意加空行。
|
|
136
149
|
- [C++ Core Guidelines: NL.16](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#rl-order):类型、构造/赋值/析构、函数、数据成员的传统次序。
|
|
137
150
|
- [cppreference: 构造函数](https://en.cppreference.com/w/cpp/language/constructor):非静态数据成员按声明顺序初始化,与 mem-initializer 列表顺序无关。
|
|
138
151
|
- [LLVM Coding Standards](https://llvm.org/docs/CodingStandards.html):只要求每个 `.cpp` 先包含定义其接口的头文件,没有规定成员定义的先后;上一条「与头文件声明同序」因此是本工作流约定,不冒充外部标准。
|