@chengzhao-dev/dsh-cpp-workflow 0.6.20 → 0.7.3
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 +23 -32
- package/README.md +24 -33
- package/cordis.patch.yml +6 -6
- package/lib/client.js +35 -23
- package/lib/comment-policy.js +15 -6
- package/lib/index.js +45 -37
- package/lib/workspace.js +10 -32
- package/package.json +7 -7
- package/skills/building-cpp/SKILL.md +24 -24
- package/skills/building-cpp/assets/templates/projects/common/running-build.sh +5 -1
- package/skills/building-cpp/project/vscode/01-extensions.md +6 -0
- package/skills/building-cpp/project/vscode/02-settings.md +7 -1
- package/skills/building-cpp/references/build-script.md +10 -3
- package/skills/building-cpp/references/environment.md +8 -1
- package/skills/building-cpp/scripts/checking-environment.mjs +11 -13
- package/skills/building-cpp/steps/01-build.md +5 -4
- package/skills/building-cpp/steps/02-tooling.md +10 -10
- package/skills/building-cpp/steps/03-check.md +13 -11
- package/skills/developing-cpp/SKILL.md +39 -40
- package/skills/developing-cpp/references/comments/comment-rules.md +195 -0
- package/skills/developing-cpp/references/comments/comment-style.md +76 -0
- package/skills/developing-cpp/references/comments/examples/declaration-class.md +65 -0
- package/skills/developing-cpp/references/comments/examples/declaration-constructor.md +22 -0
- package/skills/developing-cpp/references/comments/examples/declaration-enum.md +48 -0
- package/skills/developing-cpp/{source/07-comments → references/comments}/examples/declaration-file.md +5 -5
- package/skills/developing-cpp/references/comments/examples/declaration-function.md +135 -0
- package/skills/developing-cpp/references/comments/examples/declaration-member.md +23 -0
- package/skills/developing-cpp/{source/07-comments → references/comments}/examples/declaration-predicate.md +3 -2
- package/skills/developing-cpp/references/comments/examples/declaration-struct.md +18 -0
- package/skills/developing-cpp/references/comments/examples/implementation-body.md +81 -0
- package/skills/developing-cpp/references/comments/glossary/comment-terms.md +92 -0
- package/skills/developing-cpp/{source/03-design → references/design}/examples/coordinate-table.md +3 -3
- package/skills/developing-cpp/{source/03-design → references/design}/examples/header-place.md +1 -1
- package/skills/developing-cpp/references/design/examples/move-result.md +48 -0
- package/skills/developing-cpp/references/design/examples/preview-read.md +36 -0
- package/skills/developing-cpp/references/design/project-design.md +160 -0
- package/skills/developing-cpp/{source/01-editing → references/editing}/01-edit-operations.md +5 -3
- package/skills/developing-cpp/{source/01-editing → references/editing}/02-rule-priority.md +2 -2
- package/skills/developing-cpp/{source/08-exceptions → references/exceptions}/examples/throw-build-mode.md +4 -4
- package/skills/developing-cpp/{source/08-exceptions → references/exceptions}/examples/throw-message.md +10 -3
- package/skills/developing-cpp/references/exceptions/throw-rules.md +216 -0
- package/skills/developing-cpp/{source/05-language → references/language}/glossary/language-terms.md +2 -2
- package/skills/developing-cpp/{source/05-language/01-language-basics → references/language/language-basics}/01-objects.md +8 -2
- package/skills/developing-cpp/{source/05-language/01-language-basics → references/language/language-basics}/02-initialization.md +8 -1
- package/skills/developing-cpp/{source/05-language/01-language-basics → references/language/language-basics}/03-deduction.md +6 -0
- package/skills/developing-cpp/references/language/language-basics/04-const.md +25 -0
- package/skills/developing-cpp/references/language/language-basics/05-expressions.md +28 -0
- package/skills/developing-cpp/references/language/language-basics/06-functions.md +17 -0
- package/skills/developing-cpp/{source/05-language/01-language-basics → references/language/language-basics}/07-classes.md +7 -1
- package/skills/developing-cpp/references/language/language-basics/08-raii.md +15 -0
- package/skills/developing-cpp/{source/05-language/01-language-basics → references/language/language-basics}/09-templates.md +6 -0
- package/skills/developing-cpp/{source/05-language/01-language-basics → references/language/language-basics}/10-checklist.md +2 -1
- package/skills/developing-cpp/references/language/performance/01-measure.md +22 -0
- package/skills/developing-cpp/{source/05-language/04-performance → references/language/performance}/02-complexity.md +6 -0
- package/skills/developing-cpp/{source/05-language/04-performance → references/language/performance}/03-memory.md +7 -1
- package/skills/developing-cpp/{source/05-language/04-performance → references/language/performance}/04-undefined-behavior.md +9 -1
- package/skills/developing-cpp/{source/05-language/04-performance → references/language/performance}/05-pitfalls.md +6 -0
- package/skills/developing-cpp/references/language/performance/06-false-optimizations.md +13 -0
- package/skills/developing-cpp/{source/05-language/03-standard-library → references/language/standard-library}/01-containers.md +6 -0
- package/skills/developing-cpp/references/language/standard-library/02-access.md +14 -0
- package/skills/developing-cpp/{source/05-language/03-standard-library → references/language/standard-library}/03-iteration.md +6 -0
- package/skills/developing-cpp/{source/05-language/03-standard-library → references/language/standard-library}/04-algorithms.md +6 -0
- package/skills/developing-cpp/{source/05-language/03-standard-library → references/language/standard-library}/05-string.md +6 -0
- package/skills/developing-cpp/references/language/standard-library/06-format.md +114 -0
- package/skills/developing-cpp/references/language/standard-library/07-time.md +13 -0
- package/skills/developing-cpp/references/language/standard-library/08-random.md +13 -0
- package/skills/developing-cpp/references/language/standard-library/09-files.md +14 -0
- package/skills/developing-cpp/{source/05-language/02-value-types → references/language/value-types}/01-width.md +6 -0
- package/skills/developing-cpp/{source/05-language/02-value-types → references/language/value-types}/02-signed.md +7 -1
- package/skills/developing-cpp/references/language/value-types/03-integer-roles.md +38 -0
- package/skills/developing-cpp/references/language/value-types/04-enum.md +24 -0
- package/skills/developing-cpp/{source/05-language/02-value-types → references/language/value-types}/06-const-method.md +5 -0
- package/skills/developing-cpp/{source/05-language/02-value-types → references/language/value-types}/07-nodiscard.md +6 -0
- package/skills/developing-cpp/references/language/value-types/08-comparison.md +10 -0
- package/skills/developing-cpp/{source/05-language/02-value-types → references/language/value-types}/09-subscript.md +2 -1
- package/skills/developing-cpp/{source/05-language/02-value-types → references/language/value-types}/10-float-bool.md +6 -0
- package/skills/developing-cpp/{source/05-language/02-value-types → references/language/value-types}/11-checklist.md +1 -1
- package/skills/developing-cpp/{source/04-layout → references/layout}/01-source-layout.md +8 -3
- package/skills/developing-cpp/{source/04-layout → references/layout}/02-file-template.md +14 -8
- package/skills/developing-cpp/references/layout/03-declaration-order.md +141 -0
- package/skills/developing-cpp/references/naming/examples/naming-accessor.md +28 -0
- package/skills/developing-cpp/references/naming/examples/naming-basic.md +3 -0
- package/skills/developing-cpp/references/naming/examples/naming-local.md +45 -0
- package/skills/developing-cpp/{source/06-naming → references/naming}/examples/naming-lookup.md +1 -1
- package/skills/developing-cpp/references/naming/examples/naming-lower-layer.md +46 -0
- package/skills/developing-cpp/references/naming/examples/naming-player-value.md +15 -0
- package/skills/developing-cpp/{source/06-naming → references/naming}/examples/naming-predicate.md +1 -1
- package/skills/developing-cpp/{source/06-naming → references/naming}/examples/naming-role.md +1 -1
- package/skills/developing-cpp/references/naming/examples/naming-state-change.md +70 -0
- package/skills/developing-cpp/references/naming/glossary/naming-terms.md +23 -0
- package/skills/developing-cpp/references/naming/glossary/position.md +23 -0
- package/skills/developing-cpp/references/naming/glossary/square.md +47 -0
- package/skills/developing-cpp/references/naming/identifier-names.md +109 -0
- package/skills/developing-cpp/{source/02-style/01-google-cpp-style.md → references/style/google-cpp-style.md} +14 -13
- package/skills/developing-cpp/scripts/checking-comments.mjs +80 -15
- package/skills/developing-cpp/scripts/checking-declaration-order.mjs +383 -0
- package/skills/developing-cpp/steps/01-behavior.md +5 -5
- package/skills/developing-cpp/steps/02-boundary.md +9 -9
- package/skills/developing-cpp/steps/03-declaration.md +16 -14
- package/skills/developing-cpp/steps/04-implementation.md +22 -18
- package/skills/engineering/SKILL.md +43 -40
- package/skills/engineering/{cmake → references/cmake}/assets/CompileSettings.cmake +2 -0
- package/skills/engineering/{cmake → references/cmake}/linking/01-libraries.md +6 -0
- package/skills/engineering/references/cmake/linking/02-rpath.md +25 -0
- package/skills/engineering/{cmake → references/cmake}/linking/03-undefined-reference.md +7 -1
- package/skills/engineering/{cmake → references/cmake}/linking/04-inspect.md +6 -0
- package/skills/engineering/{cmake/02-modules/01-functions.md → references/cmake/modules/functions.md} +8 -5
- package/skills/engineering/{cmake/03-standard → references/cmake}/standards/README.md +12 -12
- package/skills/engineering/{cmake/03-standard → references/cmake}/standards/c++23/README.md +3 -3
- package/skills/engineering/{cmake/03-standard/01-cxx-standard.md → references/cmake/standards/cxx-standard.md} +8 -7
- package/skills/engineering/{cmake/01-targets → references/cmake/targets}/examples/target.md +2 -2
- package/skills/engineering/{cmake/01-targets/01-rules.md → references/cmake/targets/rules.md} +18 -11
- package/skills/engineering/{cmake → references/cmake}/testing/ctest.md +10 -3
- package/skills/engineering/{formatting → references/formatting}/01-encoding.md +1 -1
- package/skills/engineering/{formatting → references/formatting}/02-clang-format.md +7 -1
- package/skills/engineering/{formatting → references/formatting}/03-clang-tidy.md +7 -0
- package/skills/engineering/{formatting → references/formatting}/04-clangd.md +7 -0
- package/skills/engineering/references/formatting/examples/clang-format.md +38 -0
- package/skills/engineering/{formatting → references/formatting}/examples/clang-tidy.md +0 -21
- package/skills/engineering/{formatting → references/formatting}/scripts/checking-encoding.mjs +6 -7
- package/skills/engineering/{cmake/steps → steps}/01-targets.md +10 -9
- package/skills/engineering/{cmake/steps → steps}/02-modules.md +4 -4
- package/skills/engineering/{cmake/steps → steps}/03-check.md +5 -3
- package/skills/developing-cpp/source/03-design/01-project-design.md +0 -105
- package/skills/developing-cpp/source/03-design/examples/move-result.md +0 -39
- package/skills/developing-cpp/source/03-design/examples/preview-read.md +0 -40
- package/skills/developing-cpp/source/04-layout/03-declaration-order.md +0 -118
- package/skills/developing-cpp/source/05-language/01-language-basics/04-const.md +0 -15
- package/skills/developing-cpp/source/05-language/01-language-basics/05-expressions.md +0 -8
- package/skills/developing-cpp/source/05-language/01-language-basics/06-functions.md +0 -9
- package/skills/developing-cpp/source/05-language/01-language-basics/08-raii.md +0 -9
- package/skills/developing-cpp/source/05-language/02-value-types/03-integer-roles.md +0 -23
- package/skills/developing-cpp/source/05-language/02-value-types/04-enum.md +0 -17
- package/skills/developing-cpp/source/05-language/02-value-types/08-comparison.md +0 -4
- package/skills/developing-cpp/source/05-language/03-standard-library/02-access.md +0 -8
- package/skills/developing-cpp/source/05-language/03-standard-library/06-format.md +0 -44
- package/skills/developing-cpp/source/05-language/03-standard-library/07-time.md +0 -3
- package/skills/developing-cpp/source/05-language/03-standard-library/08-random.md +0 -3
- package/skills/developing-cpp/source/05-language/03-standard-library/09-files.md +0 -4
- package/skills/developing-cpp/source/05-language/04-performance/01-measure.md +0 -15
- package/skills/developing-cpp/source/05-language/04-performance/06-false-optimizations.md +0 -6
- package/skills/developing-cpp/source/06-naming/01-identifier-names.md +0 -78
- package/skills/developing-cpp/source/06-naming/examples/naming-accessor.md +0 -17
- package/skills/developing-cpp/source/06-naming/examples/naming-basic.md +0 -3
- package/skills/developing-cpp/source/06-naming/examples/naming-lower-layer.md +0 -39
- package/skills/developing-cpp/source/06-naming/examples/naming-player-mark.md +0 -17
- package/skills/developing-cpp/source/06-naming/examples/naming-state-change.md +0 -57
- package/skills/developing-cpp/source/07-comments/01-comment-rules.md +0 -239
- package/skills/developing-cpp/source/07-comments/examples/declaration-class.md +0 -44
- package/skills/developing-cpp/source/07-comments/examples/declaration-constructor.md +0 -22
- package/skills/developing-cpp/source/07-comments/examples/declaration-enum.md +0 -24
- package/skills/developing-cpp/source/07-comments/examples/declaration-function.md +0 -100
- package/skills/developing-cpp/source/07-comments/examples/declaration-member.md +0 -25
- package/skills/developing-cpp/source/07-comments/examples/declaration-struct.md +0 -18
- package/skills/developing-cpp/source/07-comments/examples/implementation-body.md +0 -42
- package/skills/developing-cpp/source/07-comments/glossary/comment-terms.md +0 -75
- package/skills/developing-cpp/source/08-exceptions/01-throw-rules.md +0 -160
- package/skills/engineering/cmake/linking/02-rpath.md +0 -17
- package/skills/engineering/formatting/examples/clang-format.md +0 -17
- /package/skills/developing-cpp/{source/07-comments → references/comments}/examples/implementation-todo.md +0 -0
- /package/skills/developing-cpp/{source/03-design → references/design}/examples/behavior-sentence.md +0 -0
- /package/skills/developing-cpp/{source/03-design → references/design}/examples/library-boundary.md +0 -0
- /package/skills/developing-cpp/{source/03-design → references/design}/examples/library-split.md +0 -0
- /package/skills/developing-cpp/{source/08-exceptions → references/exceptions}/examples/throw-boundary.md +0 -0
- /package/skills/developing-cpp/{source/08-exceptions → references/exceptions}/examples/throw-catch.md +0 -0
- /package/skills/developing-cpp/{source/08-exceptions → references/exceptions}/examples/throw-destructor.md +0 -0
- /package/skills/developing-cpp/{source/08-exceptions → references/exceptions}/examples/throw-format.md +0 -0
- /package/skills/developing-cpp/{source/08-exceptions → references/exceptions}/examples/throw-when.md +0 -0
- /package/skills/developing-cpp/{source/05-language/04-performance → references/language/performance}/07-checklist.md +0 -0
- /package/skills/developing-cpp/{source/05-language/03-standard-library → references/language/standard-library}/10-checklist.md +0 -0
- /package/skills/developing-cpp/{source/05-language/02-value-types → references/language/value-types}/05-strong-type.md +0 -0
- /package/skills/developing-cpp/{source/06-naming → references/naming}/examples/naming-current-prefix.md +0 -0
- /package/skills/developing-cpp/{source/06-naming → references/naming}/examples/naming-multiple-return.md +0 -0
- /package/skills/developing-cpp/{source/06-naming → references/naming}/examples/naming-rename.md +0 -0
- /package/skills/developing-cpp/{source/06-naming → references/naming}/examples/naming-responsibility.md +0 -0
- /package/skills/engineering/{cmake → references/cmake}/assets/LinkSettings.cmake +0 -0
- /package/skills/engineering/{cmake/03-standard → references/cmake}/standards/c++11/README.md +0 -0
- /package/skills/engineering/{cmake/03-standard → references/cmake}/standards/c++11/cmake-snippet.cmake +0 -0
- /package/skills/engineering/{cmake/03-standard → references/cmake}/standards/c++14/README.md +0 -0
- /package/skills/engineering/{cmake/03-standard → references/cmake}/standards/c++14/cmake-snippet.cmake +0 -0
- /package/skills/engineering/{cmake/03-standard → references/cmake}/standards/c++17/README.md +0 -0
- /package/skills/engineering/{cmake/03-standard → references/cmake}/standards/c++17/cmake-snippet.cmake +0 -0
- /package/skills/engineering/{cmake/03-standard → references/cmake}/standards/c++20/README.md +0 -0
- /package/skills/engineering/{cmake/03-standard → references/cmake}/standards/c++20/cmake-snippet.cmake +0 -0
- /package/skills/engineering/{cmake/03-standard → references/cmake}/standards/c++23/cmake-snippet.cmake +0 -0
- /package/skills/engineering/{cmake/03-standard → references/cmake/standards}/examples/standard.md +0 -0
- /package/skills/engineering/{cmake/01-targets → references/cmake/targets}/examples/glob.md +0 -0
- /package/skills/engineering/{cmake/01-targets → references/cmake/targets}/examples/rpath.md +0 -0
- /package/skills/engineering/{formatting → references/formatting}/examples/clangd.md +0 -0
package/skills/developing-cpp/{source/01-editing → references/editing}/01-edit-operations.md
RENAMED
|
@@ -32,6 +32,8 @@
|
|
|
32
32
|
|
|
33
33
|
## 四个域
|
|
34
34
|
|
|
35
|
+
四个域也叫四类改动;两种说法指同一组内容、结构、版式、措辞。域是操作内部的层次,不是第四种操作。
|
|
36
|
+
|
|
35
37
|
| 域 | 回答的问题 | 可以改 | 不能改 |
|
|
36
38
|
| --- | --- | --- | --- |
|
|
37
39
|
| 内容 | 这次要保证什么新事实或新行为? | 行为、契约、后置条件、成员事实 | 已有事实、否定、条件、数字、引文 |
|
|
@@ -39,7 +41,7 @@
|
|
|
39
41
|
| 版式 | 它长什么样? | 缩进、行宽、空行、注释符号、标签格式 | 标识符、字符串、命令、语义 |
|
|
40
42
|
| 措辞 | 这句话是否准确、必要、可读? | 命名、用词、句式、复述、术语统一 | 结构、事实、段落顺序、信息量 |
|
|
41
43
|
|
|
42
|
-
四域按「内容 → 结构 → 版式 → 措辞」推进。前一层没落定,不进入下一层;后一层发现前一层没做完,退回那一层,不在当前层补。`
|
|
44
|
+
四域按「内容 → 结构 → 版式 → 措辞」推进。前一层没落定,不进入下一层;后一层发现前一层没做完,退回那一层,不在当前层补。`references/editing/02-rule-priority.md` 的五级次序是冲突时的判据:行为与契约 > 结构 > 清晰 > 一致 > 形式。
|
|
43
45
|
|
|
44
46
|
## 跨技能交接
|
|
45
47
|
|
|
@@ -55,7 +57,7 @@
|
|
|
55
57
|
## 依据
|
|
56
58
|
|
|
57
59
|
- IPEd《Types of editing》把编辑分为 substantive editing、copyediting 与 proofreading;本模型把「写作、改写、润色」对应到「新增、重组、措辞」三档,版式独立成形式层。
|
|
58
|
-
- Google C++ Style Guide、LLVM Coding Standards 与
|
|
60
|
+
- Google C++ Style Guide、LLVM Coding Standards 与 C++ Core Guidelines 的注释条目都要求写意图、用法与陷阱,不把代码翻译成自然语言。
|
|
59
61
|
- 这些资料只解释模型,不替代项目技能、`environment.json` 或用户明确要求。
|
|
60
62
|
|
|
61
63
|
## 何时打开
|
|
@@ -69,5 +71,5 @@
|
|
|
69
71
|
3. 事实有没有变:有没有新增数字、命令、结论、引文,或把「可能」「通常」改成确定?
|
|
70
72
|
4. 边界写正了吗:有没有「不检查…」清单?前置条件是否写成「调用方保证…」「仅接受…」或 `@pre`?
|
|
71
73
|
5. 原样保留了吗:术语、命令、标识符、路径、URL 与围栏内容是否未被改写?
|
|
72
|
-
6. 次序守住了吗:前一层是否已落定并经检查,后一层才开始?冲突是否按 `
|
|
74
|
+
6. 次序守住了吗:前一层是否已落定并经检查,后一层才开始?冲突是否按 `references/editing/02-rule-priority.md` 判?
|
|
73
75
|
|
|
@@ -2,13 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
源码规则不是同一层的检查表。两条规则冲突时,先保住更高层,再谈低层的「更短」「更统一」「更整齐」。
|
|
4
4
|
|
|
5
|
-
本表管写作内部的取舍顺序;三种操作与四个域的先后见 `
|
|
5
|
+
本表管写作内部的取舍顺序;三种操作与四个域的先后见 `references/editing/01-edit-operations.md`。
|
|
6
6
|
|
|
7
7
|
| 级别 | 保住什么 | 典型判断 | 冲突时让位给谁 |
|
|
8
8
|
| --- | --- | --- | --- |
|
|
9
9
|
| 1 行为与契约 | 可观察行为、不变量、所有权、异常保证不走样 | 这个后置条件删掉后,调用方还知道构造出来的对象是什么状态吗? | 不让位。为短、为顺、为统一删掉契约,都是失败 |
|
|
10
10
|
| 2 结构 | 库与程序的边界、依赖方向、头文件自包含 | 这个 include 把哪一层带进了公共头? | 让位给行为;高于清晰、一致、形式 |
|
|
11
|
-
| 3 清晰 | 名字、签名、注释能直接读出用途与限制 | 读者要不要猜 `
|
|
11
|
+
| 3 清晰 | 名字、签名、注释能直接读出用途与限制 | 读者要不要猜 `squares_` 的两层下标哪层是行? | 让位给行为与结构;高于一致、形式 |
|
|
12
12
|
| 4 一致 | 同类声明用同一种句式、位置和术语 | 两个成员的注释是否都在说同一类事实? | 让位给行为、结构、清晰;高于形式 |
|
|
13
13
|
| 5 形式 | 行宽、缩进、空行、符号 | 这行能不能再短一点? | 前四级都通过后才动 |
|
|
14
14
|
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
#ifdef NDEBUG
|
|
7
7
|
return false;
|
|
8
8
|
#else
|
|
9
|
-
throw std::out_of_range{row, col};
|
|
9
|
+
throw std::out_of_range{std::format("position ({}, {}) is out of range", row, col)};
|
|
10
10
|
#endif
|
|
11
11
|
```
|
|
12
12
|
|
|
@@ -25,13 +25,13 @@ void Place(Position pos, Player player);
|
|
|
25
25
|
**差**:用 `assert` 承担 Release 也必须执行的 invariant 检查;`NDEBUG` 后检查消失。
|
|
26
26
|
|
|
27
27
|
```cpp
|
|
28
|
-
assert(game.
|
|
28
|
+
assert(game.GetResult() == GameResult::kInProgress);
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
-
**好**:两种模式都必须检查的 invariant 用 `Check`(项目保留时),失败时打印诊断并终止;只在开发期值得做、Release 可以省掉的自检用 `DebugCheck
|
|
31
|
+
**好**:两种模式都必须检查的 invariant 用 `Check`(项目保留时),失败时打印诊断并终止;只在开发期值得做、Release 可以省掉的自检用 `DebugCheck`。没有这两个宏时,两种模式都必须执行的检查用 `std::terminate`(失败前先打印诊断),只在开发期做的自检用 `assert`。
|
|
32
32
|
|
|
33
33
|
```cpp
|
|
34
|
-
Check(game.
|
|
34
|
+
Check(game.GetResult() == GameResult::kInProgress, "Ai::Choose: game is already over");
|
|
35
35
|
DebugCheck(index < history_.size(), "history index out of range");
|
|
36
36
|
```
|
|
37
37
|
|
|
@@ -10,8 +10,15 @@ throw std::out_of_range{std::format("GetTask: 下标 {} 越界", index)};
|
|
|
10
10
|
**好**:语义已经由标准类型给出时直接用标准类型;消息按「标签: 完整句」组织,句子主语是失败的值,实际值直接插进句子,限度用 `for` 接在后面;语言由项目约定,入口需要面向用户的前缀时只加前缀。
|
|
11
11
|
|
|
12
12
|
```cpp
|
|
13
|
-
throw std::out_of_range{std::format(
|
|
14
|
-
"GetTask: index {} is out of range", index)};
|
|
13
|
+
throw std::out_of_range{std::format("GetTask: index {} is out of range", index)};
|
|
15
14
|
```
|
|
16
15
|
|
|
17
|
-
|
|
16
|
+
消息需要跨行时,先存进变量再抛出:
|
|
17
|
+
|
|
18
|
+
```cpp
|
|
19
|
+
const std::string message =
|
|
20
|
+
std::format("GetTask: index {} is out of range for list of size {}", index, size);
|
|
21
|
+
throw std::out_of_range{message};
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
消息写操作和关键值;英文消息小写开头、句末不加句号,中文消息按中文标点,句式不变;实际值用 `{}` 直接放进句子,不要写成 `(index={})`。`std::format` 只是构造消息的方式,句式见 `references/exceptions/throw-rules.md` 的「消息」节。项目若定义 `Error` 基类,按该文件先定基类再派生,调用方捕获基类接住整族。
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
# 异常与错误
|
|
2
|
+
|
|
3
|
+
本文件是**开发阶段**的源码写作指导,只在 `developing-cpp` 被打开时阅读。插件运行时不编译 C++、不执行示例,也不把这里的规定送进任何运行时流程;它只是一份随技能分发的 Markdown 参考。
|
|
4
|
+
|
|
5
|
+
Google C++ Style Guide 明确写「We do not use C++ exceptions」。本工作流在书面规范上做一次有边界、可追溯的偏离:**源码示例和异常指导采用 C++ Core Guidelines 的 Error handling 规则**,因为这套规则由 Bjarne Stroustrup 与 Herb Sutter 编辑,代表现代 C++ 社区对异常安全、RAII 和 invariant 的共识。偏离只覆盖异常这一节;其他排版、命名、类型和注释规则仍以 Google 为准。不要在工程里同时引入 LLVM、Chromium 或 Abseil 的异常政策作为第二套标准。
|
|
6
|
+
|
|
7
|
+
规范来源:
|
|
8
|
+
|
|
9
|
+
- [C++ Core Guidelines: Error handling](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#s-errors)(E.1–E.31)。
|
|
10
|
+
- [Boost.Exception](https://www.boost.org/doc/libs/latest/libs/exception/doc/boost-exception.html):异常类型层级的工程典范,只借它的结构,不把 Boost 变成依赖。
|
|
11
|
+
- [ISO C++ `[except]`](https://eel.is/c++draft/except):异常对象、栈展开与构造/析构期间的规则。
|
|
12
|
+
- [cppreference: Exceptions](https://en.cppreference.com/w/cpp/language/exceptions):异常安全保证与 `noexcept` 的语法参考。
|
|
13
|
+
|
|
14
|
+
## 先定错误策略
|
|
15
|
+
|
|
16
|
+
E.1 要求尽早决定错误处理策略:先问调用方还能不能继续、失败是正常结果还是 invariant 已破坏、消息是否需要跨层携带,再写接口。
|
|
17
|
+
|
|
18
|
+
策略一旦定下,声明、实现、测试和入口都用同一套;不要在同一层里一半返回错误码、一半抛异常。
|
|
19
|
+
|
|
20
|
+
判断是否算「混用」,看函数合同而不是看返回类型:查询必须产出一个值、失败时产不出(如 `GetSquare` 坐标越界),就抛;写入操作把「这一手写不进」当作正常结果、调用方本来就要分支(如 `Game::Place` 返回 `PlaceResult`,玩家、终局、空值与占用各有一个取值),就用返回值。动作可能因为多个前置条件失败、而调用方需要区分原因时,返回 `enum class` 而不是 `bool` 加输出参数。两者并存时,公共头必须把「拒绝」与「抛」的边界各写一行;没有这行注释才算混用。
|
|
21
|
+
|
|
22
|
+
## 什么时候抛
|
|
23
|
+
|
|
24
|
+
E.2:函数不能完成它被分配的任务时,就抛异常;构造函数无法建立 invariant 时也抛,而不是留下半初始化对象。E.3:异常只用于错误处理,不能拿 `throw` 当返回值或正常分支。E.4:错误处理围绕 invariant 设计,而不是围绕「哪一行可能失败」。E.5:构造函数负责建立 invariant,失败就抛。E.6:用 RAII 防止泄漏,优先局部对象而不是裸 `new`。
|
|
25
|
+
|
|
26
|
+
判断句可以写成:**调用方是否还能履行自己的合同?** 不能,就让异常向上传播;能,并且失败是接口的正常结果,就用返回值、`std::optional` 或项目自己的结果类型表达。不要为了「统一」把可恢复的业务分支包成异常,也不要把已经无法维持 invariant 的状态压成错误码继续跑。
|
|
27
|
+
|
|
28
|
+
## 检查次序
|
|
29
|
+
|
|
30
|
+
同一个函数既有前置条件、又有正常结果时,先判断这次调用还能不能进行,再校验调用方送来的参数,最后把能交给下层的工作交给下层。次序按「哪条判断决定这一步能否进行」排:
|
|
31
|
+
|
|
32
|
+
1. 整局状态:对局是否结束由 `Game::GetResult` 回答,已结束返回 `kFinishedGame`,玩家、坐标、棋盘与历史都不再检查。对局已经无法继续时,再谈「轮到谁」没有意义。
|
|
33
|
+
2. 本层状态的前置条件:这一步是否轮到该玩家。对局自己记着轮次,这一条用返回值 `kInvalidPlayer` 答,调用方按结果分支,不抛。
|
|
34
|
+
3. 本层能判断的写入条件:要写入的值是否为空,为空返回 `kEmptySquare`。值来自参数,不依赖棋盘,先判断可以省掉一次读取。
|
|
35
|
+
4. 下层的检查与剩余的写入条件:读取目标格时由 `Board::GetSquare` 抛出 `std::out_of_range`;方格已有值返回 `kOccupiedSquare`。不要为了提前抛出而在 `Game` 里重复 `Board::IsInRange`。
|
|
36
|
+
5. 执行操作并返回 `kPlaced`:换方、写入棋盘、记录历史,三步按成员声明顺序做;构造函数预留了整盘手数的容量,写入阶段不分配内存。
|
|
37
|
+
|
|
38
|
+
整局状态排在最前,因为对局结束后这次调用根本不会落子,玩家与坐标都只是无关的输入;把玩家检查排在前面,会让「终局后落子」被解释成玩家错误。本层负责的返回值检查仍必须排在「交给下层」之前:`Game::Place` 若先把坐标交给 `Board::GetSquare`,越界异常会把「不是当前玩家」一起盖掉,调用方拿不到真正的拒绝原因。属于下层的检查不在此列:对局已结束时 `Game` 不再进入棋盘,越界坐标随 `kFinishedGame` 一起返回,这是正常的短路,不是遗漏。
|
|
39
|
+
|
|
40
|
+
「失败尽早返回」有一手依据:[F.56](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#f-nesting) 写「Use a guard-clause to take care of exceptional cases and return early.」。标准库的格式化输入也按这个方向:sentry 为 `false` 时「returns without attempting to obtain any input」,而 sentry 在 `is.good()` 为 `false` 时即判失败([istream.sentry](https://eel.is/c++draft/istream.sentry)、[istream.formatted.reqmts](https://eel.is/c++draft/istream.formatted.reqmts))。但「先判对象状态、再校验参数」这一条次序本身没有一手条文:Google C++ Style Guide 全篇没有前提条件与参数校验的次序规定,Rust API Guidelines 只要求尽量校验输入、不规定校验函数返回什么。所以它是本工作流的约定,理由是状态不依赖参数,先判可以直接短路,也不会把终局后的落子误报成玩家错误。
|
|
41
|
+
|
|
42
|
+
参数之间按固定的语义顺序排,不要求与声明顺序一致;关键是同一函数内不来回换,注释、测试和消息都按这个次序。
|
|
43
|
+
|
|
44
|
+
```cpp
|
|
45
|
+
PlaceResult Game::Place(Move move) {
|
|
46
|
+
// 检查对局是否仍在进行。
|
|
47
|
+
if (GetResult() != GameResult::kInProgress) {
|
|
48
|
+
return PlaceResult::kFinishedGame;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// 检查落子玩家是否为当前玩家。
|
|
52
|
+
if (current_player_ != move.player) {
|
|
53
|
+
return PlaceResult::kInvalidPlayer;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// 检查要写入的值是否为空。
|
|
57
|
+
const SquareValue target_square_value = PlayerToSquareValue(move.player);
|
|
58
|
+
if (target_square_value == SquareValue::kEmpty) {
|
|
59
|
+
return PlaceResult::kEmptySquare;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// 检查目标方格是否为空。
|
|
63
|
+
const Square target_square = board_.GetSquare(move.position);
|
|
64
|
+
if (!target_square.IsEmpty()) {
|
|
65
|
+
return PlaceResult::kOccupiedSquare;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
current_player_ = PlayerToOpponent(current_player_);
|
|
69
|
+
board_.SetSquare(move.position, target_square_value);
|
|
70
|
+
history_.push_back(move);
|
|
71
|
+
return PlaceResult::kPlaced;
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### 重复的入口检查
|
|
76
|
+
|
|
77
|
+
同一层有多个入口重复同一个 `if (!IsInRange(...)) throw ...` 时,把检查收进一个函数。`std::source_location` 放在默认参数里取调用点,函数名也由它提供:
|
|
78
|
+
|
|
79
|
+
```cpp
|
|
80
|
+
void Board::CheckInRange(Position pos, std::source_location location) {
|
|
81
|
+
// 检查坐标是否越界。
|
|
82
|
+
if (!IsInRange(pos)) {
|
|
83
|
+
const std::string message = std::format(
|
|
84
|
+
"position {} out of range for {}x{} board, at {} ({}:{}:{})", pos, kRowCount,
|
|
85
|
+
kColumnCount, location.function_name(), location.file_name(), location.line(),
|
|
86
|
+
location.column());
|
|
87
|
+
throw std::out_of_range{message};
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
声明写成 `static void CheckInRange(Position pos, std::source_location location = std::source_location::current());`,紧挨 `IsInRange`,放在 public 区:对局层要先确认坐标再决定怎么写时可以直接调用它,不必复制一份判断。`source_location::current()` 作为默认参数在调用点求值,所以 `function_name()`、`file_name()`、`line()` 与 `column()` 描述的都是调用方;调用点不写这个参数,上一行写一句 `// 检查坐标是否在棋盘内。`,本行只写 `CheckInRange(pos)`。
|
|
93
|
+
|
|
94
|
+
`source_location` 是 C++20 设施;`line()` 与 `column()` 从 1 起算。测试只断言值、范围、文件名与消息里出现的函数名,源码移动后测试不会失效。
|
|
95
|
+
|
|
96
|
+
依据:[Core Guidelines I.5](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#ri-pre)、[I.6](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#ri-expects) 把前置条件写进接口;标准库的 `vector::at` 与 `std::stoi` 在访问前检查边界,.NET 的 [CA1062](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1062) 也要求先验证再使用。[P2053R0](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p2053r0.pdf) 区分输入校验与防御性检查:外部输入必须校验,内部前置条件可用断言或 contract 表达。这里只让坐标范围抛异常,因为这份数据由 `Board` 拥有,重复判断会让同一规则出现两份,消息与异常类型还可能分叉;玩家与整局状态是 `Game` 自己拥有的前置条件,用返回值答,调用方本来就要按结果分支。`Player` 的取值由适配层保证;坐标越界属于调用方的程序错误,不是对局里的正常分支,所以仍抛出。
|
|
97
|
+
|
|
98
|
+
## 异常类型
|
|
99
|
+
|
|
100
|
+
E.14:用专门的用户定义类型表示异常,不要抛内建类型、`std::string` 或字符串字面量。类型本身可以只表达语义,数据放在成员里;也可以完全不带数据,让 `catch` 只按类型选择。
|
|
101
|
+
|
|
102
|
+
```cpp
|
|
103
|
+
// 领域错误:类型即语义,调用方可以只捕获自己认识的那一支。
|
|
104
|
+
class ParseError : public std::runtime_error {
|
|
105
|
+
public:
|
|
106
|
+
explicit ParseError(const std::string& message) : std::runtime_error(message) {}
|
|
107
|
+
};
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
标准库已经命名的失败直接用标准类型:坐标越界用 `std::out_of_range`,调用时不变量已破坏用 `std::logic_error`,正常输入里的解析失败可以用 `std::runtime_error`。只有调用方必须把这些失败和上述类型区分开时,才新增派生自它们的类型;不要为了「看起来规范」给每个参数错误都建一支。项目若定义自己的异常基类(如 `Error`),先定基类再按领域派生,调用方捕获基类就能接住整族异常。
|
|
111
|
+
|
|
112
|
+
## 两种构建模式的三层合同
|
|
113
|
+
|
|
114
|
+
库的失败方式在 Debug 与 Release 下必须可预测;只有诊断信息的详略可以不同。
|
|
115
|
+
|
|
116
|
+
- 返回值:两种模式完全一致。函数用返回值报告可恢复失败时,Debug 不得改成抛异常,Release 也不得把异常改成错误码。
|
|
117
|
+
- 标准异常:两种模式的抛出条件相同。越界抛 `std::out_of_range`,前置条件被破坏抛 `std::logic_error`;项目若定义 `Error` 基类,派生类型同时派生自 `std::exception`,Debug 的 `what()` 可带 `file:line`,Release 只留值。
|
|
118
|
+
- `Check`:项目保留这种断言时,两种模式都检查,失败时打印诊断并终止;用于 invariant 已被破坏、继续执行没有意义的场合。没有 `Check` 的项目:两种模式都必须执行的检查用 `std::terminate`(失败前先打印诊断),只在开发期做的自检用 `assert`,不另写包装。
|
|
119
|
+
- `DebugCheck`:项目保留时只在 Debug 生效,`NDEBUG` 后展开为空;用于代价高、只在开发期值得做的自检。不得用它承担 Release 也必须执行的检查。
|
|
120
|
+
|
|
121
|
+
`Check` 不是 `[[noreturn]]`:条件成立时正常返回。`DebugCheck` 的参数在 Release 不求值,不能放入有副作用的表达式。
|
|
122
|
+
|
|
123
|
+
## 抛出与捕获
|
|
124
|
+
|
|
125
|
+
E.15:按值抛出、按引用捕获(通常是 `const` 引用);不要抛裸指针;重新抛出用 `throw;`,不要 `throw error;` 丢类型。捕获后能补偿才捕获,不能补偿就让异常继续向上。
|
|
126
|
+
|
|
127
|
+
E.17:不要在每个函数里捕获所有异常。E.18:少用显式 `try/catch`,优先 RAII 和 `finally` 风格的清理对象。E.19:没有资源句柄时,用 `final_action` 之类的 scope guard 做清理。
|
|
128
|
+
|
|
129
|
+
E.31:`catch` 子句从派生到基类、从具体到通用排序,`catch (...)` 只能放最后。不要 `catch (...)` 后静默返回,除非当前层真的能把错误翻译成调用方合同的一部分。
|
|
130
|
+
|
|
131
|
+
## 不抛的边界
|
|
132
|
+
|
|
133
|
+
E.12:在不可能或不可接受抛异常的地方用 `noexcept`。声明前先读实现:函数体里出现 `at()` 或其他可能抛出的调用,就去掉 `noexcept`;不要再包一层只转发下标的私有函数来维持 `noexcept`,直接写 `squares_.at(row).at(col)` 并去掉 `noexcept`。正例是只比较两个整数的 `IsInRange`;反例是用 `at()` 读取并会抛 `std::out_of_range` 的 `GetSquare`。普通查询和返回结果枚举的函数,不因为当前函数体不抛就补 `noexcept`;`noexcept` 留给移动、交换、析构,以及已经标上的纯谓词。E.13:直接拥有资源的函数不要抛,资源释放应交给 RAII。E.16:析构、`deallocate`、`swap` 以及异常类型的拷贝/移动构造不得失败;析构和 `swap` 应当是 `noexcept`。
|
|
134
|
+
|
|
135
|
+
E.30:不要写异常规范(`throw(...)` 之类);C++11 之后用 `noexcept` 表达「不抛」。需要无异常构建时,由项目单独决定,不要把这套异常指导改写成两面都对的模糊话。
|
|
136
|
+
|
|
137
|
+
## 程序入口
|
|
138
|
+
|
|
139
|
+
库让异常向上传播,入口是翻译边界:捕获 `std::exception`,输出可理解的诊断,返回项目约定的非零退出码。退出码的具体取值属于项目合同,本节不规定 0/1/2。
|
|
140
|
+
|
|
141
|
+
```cpp
|
|
142
|
+
int main() {
|
|
143
|
+
try {
|
|
144
|
+
return Run(std::cin, std::cout);
|
|
145
|
+
} catch (const std::exception& error) {
|
|
146
|
+
std::cerr << std::format("错误: {}\n", error.what());
|
|
147
|
+
return 1;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
消息要能让调用者定位操作和关键值,句式见下节;`std::format` 只是构造 `what()` 或边界消息的方式。C++20 及更早没有 `std::print`;默认 C++23 且标准库支持时,可在程序边界用 `std::print`,库代码仍只组合消息、不直接打印。
|
|
153
|
+
|
|
154
|
+
## 消息
|
|
155
|
+
|
|
156
|
+
`what()` 是给程序员看的诊断文本,句式是「标签: 完整句」。标签写明来源,句子主语是失败的值或对象,谓语说明哪里不对,实际值直接插进句子,限度用 `for` 接在句末。标签是标识符,成员写 `Type::Function`,自由函数只写函数名,冒号后一个空格。私有检查函数用默认参数 `std::source_location::current()` 取调用点时不写标签,句子从失败的值开始,限度的对象用 `for` 接在句末,再写调用函数名与 `file_name():line():column()` 组成的括号 `at {} ({}:{}:{})`:`position {} out of range for {}x{} board, at {} ({}:{}:{})`,逐个填入失败值、尺寸与 `function_name()`、`file_name()`、`line()`、`column()`,`function_name()` 原样输出,不解析。先写哪个值失败,再写失败条件,最后写限度;语言由项目约定,下面的示例用英文。句子要完整:主语和谓语都在,不写 `board position is out of range` 这种重复标签类型的主语,也不把位置以外的值塞进括号,不写 Error / Failed 或空泛的「失败」。实际值用 `{}` 直接放进句子,不再写 `(name=value)`;没有实际值就不写值,没有限度就不写 `for`。需要原因时用 `because`,不要用分号堆叠。
|
|
157
|
+
|
|
158
|
+
英文消息只用 `:`、`()`、`,`、`=` 四个 ASCII 符号:冒号紧贴标签、后接一个空格;括号只放次要值或补充说明;逗号前不空、后一个空格;等号两侧不空。不用第二个冒号、句号、问号、感叹号、破折号或 `×`;句子小写开头,句末不加句号;尺寸写 `3x3`,`out of range` 不加连字符。中文消息按中文标点,但句式与「值在前、限度在后」的顺序不变。
|
|
159
|
+
|
|
160
|
+
值对象已有 `std::formatter` 时,格式串直接写 `{}`,不要拆成多个字段;例如 `position {}` 会输出 `position (-1, 0)`。没有格式化器的类型才拆字段。
|
|
161
|
+
|
|
162
|
+
有插值才用 `std::format`,没有洞的消息用字符串字面量。`std::out_of_range` 与 `std::logic_error` 在构造时复制消息,不能把格式化推迟到 `what()`。格式串只填手头的整数和短文本;格式化若抛出,调用方看到 `std::format_error` 或 `std::bad_alloc`,所以不在抛出表达式里做 I/O。自有异常类型才在 `what()` 里惰性格式化并兜底,本仓库不为此新增类型。
|
|
163
|
+
|
|
164
|
+
`what()` 不是分支接口;调用方要数字就放进类型成员,不解析字符串。入口可以在消息前加面向用户的前缀,库代码不打印日志。
|
|
165
|
+
|
|
166
|
+
写进 `throw` 时,短消息直接内联;整条 `throw` 一行写不下、需要折行时,先把消息存进 `const std::string message`,再 `throw ...{message};`。公共函数的消息带标签:
|
|
167
|
+
|
|
168
|
+
```cpp
|
|
169
|
+
const std::string message = std::format("ParseConfig: line {} has no key", line_number);
|
|
170
|
+
throw ParseError{message};
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
跨行时先把消息存进 `const std::string message`,再写一行 `throw`:`throw` 行只表达抛什么,格式串与参数留在上一句,读者不必先跨过消息才能看到异常类型的结束。消息没有插值时仍写一行 `throw std::logic_error{"ChooseSquare: game is already over"};`。私有检查函数由 `source_location` 取调用点,不带标签,句式见上文「重复的入口检查」。
|
|
174
|
+
|
|
175
|
+
常用库把消息的构造放进异常类型或辅助函数:spdlog 的 `spdlog_ex` 在构造函数里写 `msg_`,CLI11 的 `Error` 族在静态工厂里拼好句子,POCO 的 `Exception` 在构造函数里存消息;调用处因此只有一行 `throw ...(...)`。本工作流在整条 `throw` 跨行时引入 `const std::string message`(需要 `#include <string>`),变量名固定,不写 `std::move`;异常类型构造时复制消息。
|
|
176
|
+
|
|
177
|
+
Rust 把失败分成两类:`Result` 表示可预期的失败,用 `?` 传播;`panic!` 只表示程序里的 bug(invariant 已被破坏),消息同样在调用点构造;跨行时先在调用点拼好再抛出。C++ 的异常同时承担这两种职责,本工作流因此把「调用方无法继续」写成异常,把「可恢复的拒绝」写成返回值。
|
|
178
|
+
|
|
179
|
+
这两条是本工作流的选择,不是普遍更优:Google C++ Style Guide 整体禁用异常,只保留「用户输入不应引发异常」这一条共识;Core Guidelines E.27 的说法是「不能抛异常时改用错误码系统」。C++23 项目也可以把可预期的失败写成 `std::expected<T, E>`,或用带字段的 struct 一次返回多个值;out 参数在本工作流里一律不用。
|
|
180
|
+
|
|
181
|
+
```cpp
|
|
182
|
+
const std::string message =
|
|
183
|
+
std::format("ReadConfig: {} has no header because the first line is empty", path);
|
|
184
|
+
throw ParseError{message};
|
|
185
|
+
|
|
186
|
+
throw std::logic_error{"ChooseSquare: game is already over"};
|
|
187
|
+
throw std::format_error{"Position: format specifier is not supported, use {} instead"};
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
反例:`Game::Place: the move is not acceptable (player={}); the game is in progress`。标签已经说明是 `Game::Place`,句子又用 `the move` 当空泛主语,实际值还放在括号里;读者要在标签、主语和括号之间来回拼。
|
|
191
|
+
|
|
192
|
+
## 声明注释
|
|
193
|
+
|
|
194
|
+
在公共声明的注释里写清:什么条件下抛、抛哪一类、调用方如何避免。实现文件里的内部异常不必逐条写进公共头。
|
|
195
|
+
|
|
196
|
+
```cpp
|
|
197
|
+
/// 读取配置文件。
|
|
198
|
+
/// @throws FileReadError 文件不存在或无法读取。
|
|
199
|
+
/// @throws ParseError 内容不是合法配置。
|
|
200
|
+
Config ReadConfig(const std::filesystem::path& path);
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
## 检查清单
|
|
204
|
+
|
|
205
|
+
- [ ] 错误策略在接口设计时已确定,同一层没有混用错误码和异常。
|
|
206
|
+
- [ ] 构造函数失败时抛异常,没有留下半初始化对象。
|
|
207
|
+
- [ ] 异常类型有语义,没有抛内建类型、字符串或裸指针。
|
|
208
|
+
- [ ] 一行写得下的 `throw` 直接接收 `std::format(...)` 或字面量;跨行时先建 `const std::string message`,再 `throw` 变量。
|
|
209
|
+
- [ ] 按值抛出、按 `const` 引用捕获;重抛用 `throw;`。
|
|
210
|
+
- [ ] 析构、`swap`、异常类型的拷贝/移动不抛。
|
|
211
|
+
- [ ] 没有用异常做正常分支,也没有在每个函数里捕获所有异常。
|
|
212
|
+
- [ ] 本层负责的参数校验与状态检查排在正常结果返回之前;非法参数没有被 `kXWin`、`kOWin` 或 `kDraw` 之类的终局结果掩盖。
|
|
213
|
+
- [ ] 入口有统一捕获,消息可定位,退出码符合项目合同。
|
|
214
|
+
- [ ] 公共声明的 `@throws` 与实际抛出类型一致,且 Debug/Release 的抛出条件相同。
|
|
215
|
+
- [ ] 每个 `noexcept` 函数的函数体不调用 `at()` 或其他会抛的接口。
|
|
216
|
+
- [ ] 项目若保留 `Check` / `DebugCheck`:`Check` 两模式都生效,`DebugCheck` 只在 Debug 生效;没有让 Debug 抛、Release 返回 false。
|
package/skills/developing-cpp/{source/05-language → references/language}/glossary/language-terms.md
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# 术语
|
|
2
2
|
|
|
3
|
-
代码标识符用英文;注释和说明用中文时,同一个概念只用一种译法。下表是本项目的默认译法,项目技能可以增补,但不要改已有词的含义。标识符怎么拼(大小写、前缀、语义角色)见 `
|
|
3
|
+
代码标识符用英文;注释和说明用中文时,同一个概念只用一种译法。下表是本项目的默认译法,项目技能可以增补,但不要改已有词的含义。标识符怎么拼(大小写、前缀、语义角色)见 `references/naming/identifier-names.md`;本表只管术语译法与近义词辨析。
|
|
4
4
|
|
|
5
5
|
| 英文 | 中文 | 说明 |
|
|
6
6
|
| --- | --- | --- |
|
|
@@ -84,5 +84,5 @@
|
|
|
84
84
|
- **声明 vs 定义**:头文件里多是声明;函数体、全局变量初始化是定义。
|
|
85
85
|
- **参数 vs 实参**:声明里叫参数(parameter),调用点叫实参(argument);中文口语都叫参数时,写文档要区分。
|
|
86
86
|
- **接口 vs 实现**:公共头是接口,`.cpp` 是实现;不要用「接口」指整个类。
|
|
87
|
-
- **异常 vs 错误**:异常是语言机制,错误是结果;是否用异常表达错误由接口合同和 invariant 决定,不能把两者当同义词。规则见 `
|
|
87
|
+
- **异常 vs 错误**:异常是语言机制,错误是结果;是否用异常表达错误由接口合同和 invariant 决定,不能把两者当同义词。规则见 `references/exceptions/throw-rules.md`。
|
|
88
88
|
- **格式化 vs 排版**:`std::format` 组合消息;clang-format 排版源码。不要混用「格式化」指代两者。
|
|
@@ -4,6 +4,12 @@
|
|
|
4
4
|
|
|
5
5
|
- 类型决定允许的操作和表示;对象是某类型的一块存储;值是可以复制、比较、传递的内容。
|
|
6
6
|
- 变量有名字、类型、存储期和作用域。局部变量在离开作用域时销毁;`static` 局部变量在首次执行到声明时初始化,程序结束时销毁。
|
|
7
|
-
-
|
|
7
|
+
- 声明让名字可见,定义分配存储或给出函数体。非 `inline` 的变量与函数一个程序只能定义一次;`inline` 函数/变量与模板可以出现在多个翻译单元,但定义必须一致。头文件只放声明,定义放 `.cpp`,例外是 `inline` 与模板。
|
|
8
8
|
- 引用是已有对象的别名,必须初始化,不能重新绑定;指针可以为空,也可以改指向。接口优先用引用表达“必须存在”,用指针表达“可以不存在”。
|
|
9
|
-
- `*this` 不用于复制当前对象:要一份副本就直说语义(如“推演落子”只读原数据,或显式写一个复制函数)。`return *this`
|
|
9
|
+
- `*this` 不用于复制当前对象:要一份副本就直说语义(如“推演落子”只读原数据,或显式写一个复制函数)。`return *this` 仍只属于赋值运算符,值类型没有这种运算符。
|
|
10
|
+
|
|
11
|
+
## 依据
|
|
12
|
+
|
|
13
|
+
- [ISO C++ `[basic.def.odr]`](https://eel.is/c++draft/basic.def.odr):一个实体允许的声明与定义次数,以及 ODR 的一致要求。
|
|
14
|
+
- [cppreference: Definitions and ODR](https://en.cppreference.com/w/cpp/language/definition):声明、定义、`inline` 与模板的多翻译单元规则。
|
|
15
|
+
- [C++ Core Guidelines: P.1](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#s-philosophy):用类型与对象表达意图,不靠注释补语义。
|
|
@@ -6,14 +6,21 @@
|
|
|
6
6
|
int count{}; // 值初始化,0
|
|
7
7
|
std::vector<int> values{1, 2, 3}; // 列表初始化
|
|
8
8
|
std::string name = "demo"; // 拷贝初始化,类型明确
|
|
9
|
-
auto total = 0.0; //
|
|
9
|
+
auto total = 0.0; // 本工作流统一用 = 推导
|
|
10
10
|
```
|
|
11
11
|
|
|
12
12
|
- `kLines` 这类多行聚合常量表写 `name = {{`;单个对象和空初始化仍写 `count{}`,不写 `=`;不把其他花括号初始化改成 `=`。
|
|
13
13
|
- `T value{}` 做值初始化:内置类型得 0,类类型调用默认构造函数。
|
|
14
|
+
- `auto x{1}` 的推导类型在 C++17 前后不同:C++17 起是 `int`,之前是 `std::initializer_list<int>`;`{}` 与 `initializer_list` 的重载也容易误读,本工作流统一写 `auto x = 1`。
|
|
14
15
|
- 花括号禁止窄化转换:`int x{3.5}` 编译失败,`int x = 3.5` 静默截断。需要截断时显式 `static_cast<int>(3.5)`。
|
|
15
16
|
- `std::vector<int> values(3)` 是三个 0;`values{3}` 是一个元素 3。两种括号语义不同,不要凭习惯混用。
|
|
16
17
|
- 类成员在声明处写默认成员初始化(NSDMI),一律用花括号:标量 `count_{0}`,数组 `items_{}`,容器与类类型成员写空 `{}`(如 `std::vector<int> values_{};`、`Board board_{};`)。
|
|
17
18
|
- `=` 只用于花括号不可用或会改变含义的例外:引用成员不写 NSDMI,在构造函数初始化列表绑定;容器需要 `std::vector<int>(n)` 的容量构造时写 `= std::vector<int>(n)`,或在构造函数初始化列表写 `v_(n)`;`std::initializer_list` 成员避免使用,确实需要时写 `= {..}` 并保证底层数组的生命周期长于对象;花括号无法构造的类型才回退到 `= value` 或构造函数初始化列表。`=` 仍然是初始化,不是赋值。
|
|
18
19
|
- `static` 与 `static constexpr` 数据成员不是对象成员,继续写 `= 3`;`auto` 不能做非静态数据成员。
|
|
19
20
|
- 构造函数初始化列表按成员声明顺序执行,不按书写顺序;初始化列表顺序与声明顺序保持一致,避免依赖未初始化成员。
|
|
21
|
+
|
|
22
|
+
## 依据
|
|
23
|
+
|
|
24
|
+
- [ISO C++ `[dcl.init]`](https://eel.is/c++draft/dcl.init):值初始化、列表初始化、拷贝初始化与窄化转换。
|
|
25
|
+
- [cppreference: Initialization](https://en.cppreference.com/w/cpp/language/initialization):各种初始化语法与重载选择。
|
|
26
|
+
- [C++ Core Guidelines: ES.23](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#res-list):优先 `{}` 初始化;本工作流按此执行,并只为拷贝/`auto` 保留 `=`。
|
|
@@ -4,3 +4,9 @@
|
|
|
4
4
|
- 公共接口、类成员、函数返回类型写显式类型;返回值需要隐藏实现细节时才可以写 `auto`,且头文件里要能看出契约。
|
|
5
5
|
- `auto` 会丢掉顶层 `const` 和引用:`auto value = const_ref` 得到副本;需要引用时写 `const auto&`。
|
|
6
6
|
- 结构化绑定 `auto [key, value] : map` 中,`key` 是副本;要避免复制时写 `const auto& [key, value]`。
|
|
7
|
+
|
|
8
|
+
## 依据
|
|
9
|
+
|
|
10
|
+
- [ISO C++ `[dcl.spec.auto]`](https://eel.is/c++draft/dcl.spec.auto):`auto` 的推导规则与占位符约束。
|
|
11
|
+
- [cppreference: `auto`](https://en.cppreference.com/w/cpp/language/auto):顶层 `const`/引用丢失、`decltype(auto)` 与结构化绑定。
|
|
12
|
+
- [Google C++ Style Guide: `auto`](https://google.github.io/styleguide/cppguide.html#auto):只在类型显然的局部变量使用,公共接口写显式类型。
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# `const`、`constexpr`、`consteval`、`constinit`
|
|
2
|
+
|
|
3
|
+
| 关键字 | 承诺 | 典型位置 |
|
|
4
|
+
| --- | --- | --- |
|
|
5
|
+
| `const` | 初始化后不能通过这个名字修改 | 局部变量、参数、成员函数尾部 |
|
|
6
|
+
| `constexpr` | 能在编译期求值(满足条件时) | 常量、函数、构造函数 |
|
|
7
|
+
| `consteval` | 必须在编译期求值 | 编译期校验函数 |
|
|
8
|
+
| `constinit` | 静态或线程存储期变量在编译期完成初始化 | 全局、静态与 `thread_local` 变量 |
|
|
9
|
+
| `inline constexpr` | 头文件里可共享的编译期常量,避免 ODR 问题 | 命名空间作用域常量 |
|
|
10
|
+
|
|
11
|
+
- 共享常量写在头文件的命名空间作用域:`inline constexpr int kMaxRows = 3;`,不要用 `#define`。`.cpp` 里非 `inline`、非 `extern` 的命名空间作用域 `constexpr` 已有内部链接。
|
|
12
|
+
- `constexpr` 函数在首次声明处隐式 `inline`,用到它的翻译单元都必须看见定义,所以定义放头文件;只写在 `.cpp` 里会让其他翻译单元的调用成为不要求诊断的不合规程序。
|
|
13
|
+
- 不要给所有函数加 `constexpr`;只有确实需要在编译期使用、且实现满足约束时才加。
|
|
14
|
+
- `constinit` 只保证初始化时机,不保证不可修改;需要不可修改时同时写 `const`。
|
|
15
|
+
- C++14 起 `constexpr` 函数就可以包含循环、局部变量与 `if`/`switch`;C++20 起还允许编译期分配,但分配要在同一次求值内释放。也不要把运行期逻辑塞进编译期。
|
|
16
|
+
- 局部变量默认不加 `const`:短生命周期的局部值只读一次,`const` 不改变存储与运行行为。把 `const` 留给接口契约——`const` 引用参数、`const` 成员函数、指向只读数据的指针与视图,以及确实要防止后续误写的变量。Core Guidelines 的 Con.1 主张默认不可变;本仓取 Google 风格的做法,只在接口层要求 `const`。
|
|
17
|
+
|
|
18
|
+
## 依据
|
|
19
|
+
|
|
20
|
+
- [ISO C++ `[dcl.constexpr]`](https://eel.is/c++draft/dcl.constexpr):`constexpr`、`consteval` 的求值时机与函数约束。
|
|
21
|
+
- [cppreference: `constexpr`](https://en.cppreference.com/w/cpp/language/constexpr):常量表达式、`constinit` 与 `consteval` 的对照。
|
|
22
|
+
- [cppreference: `constinit`](https://en.cppreference.com/w/cpp/language/constinit):静态与线程存储期的初始化时机,不改变可修改性。
|
|
23
|
+
- [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
|
+
- [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
|
+
- [C++ Core Guidelines: Con.1](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#rconst-immutable):主张默认不可变;本仓只在接口处采用这一主张,局部变量按上一条处理。
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# 表达式与比较
|
|
2
|
+
|
|
3
|
+
- 比较写成“变量在左、常量在右”:`if (id < kTaskCount)`,不写 Yoda 条件 `if (kTaskCount > id)`;常量与枚举值总在右边。成员与参数比较时,成员状态在左、参数在右,如 `if (current_player_ != move.player)`:判断「轮到谁」以对象自己的状态为起点,参数是被核对的一次输入;成员与常量比较时成员在左,如 `if (value_ != SquareValue::kEmpty)`;参数与常量比较时参数在左。分不清主次时按“读起来的主语”决定,同一段代码保持同一种顺序。
|
|
4
|
+
- 同一个值对多个离散取值分派时用 `switch`,不用一串 `if`:值来自枚举或整数常量、分支 3 个以上、分支体短。枚举覆盖全部取值时不写 `default`,新增取值由 `-Wswitch` 指出;编译器需要出口时在 `switch` 后写 `std::unreachable()`(C++23 起;低标准写 `assert(false)` 或 `std::abort()`),不写 `throw` 兜底。
|
|
5
|
+
- 条件是比较范围、布尔组合,或只有 1–2 个分支时用 `if`;一个函数里有多条拒绝分支时,每条各写一条独立 `if` 并提前返回,成功路径留在函数末尾,不串 `else if`,也不用 `else` 包住。
|
|
6
|
+
- 否定按「类型有什么谓词」决定,不按个人习惯:类型提供 `IsEmpty()`、`IsFull()` 这类肯定式查询时,条件直接从肯定式写起,如 `if (square.IsEmpty())`。被拒绝的情形没有对应谓词时才用 `!`,如占用检查只能写成 `if (!square.IsEmpty())`;这种 `!` 只套一层,不写 `!!`,也不把 `!(value != SquareValue::kEmpty)` 这类双重否定留给读者。枚举比较同样先写正向判断:判空写 `value == SquareValue::kEmpty`,判非空写 `value != SquareValue::kEmpty`。
|
|
7
|
+
- 同一类值有 3 个以上时用数组加下标,不写 `value0`、`value1`、`value2`:`std::array<SquareValue, 3> values{};` 配合 `values[i]`;成组的常量数据写成 `std::array<Line, 8> kLines` 再由循环消费。只有 1–2 个语义各自独立的值才各起名字。
|
|
8
|
+
- 不依赖隐式穿透;确实需要穿透时写 `[[fallthrough]]`。
|
|
9
|
+
- 混合优先级的算术表达式加括号:`(a + b) * c`,不依赖读者记忆优先级。`readability-math-missing-parentheses` 会检查。
|
|
10
|
+
- 区间用半开区间 `[low, high)`;判空用 `empty()`,不要用 `size() == 0`。
|
|
11
|
+
- 逻辑表达式按语义分组,一组一个括号;组内的单个比较不再加括号,`&&` 的优先级已经清楚,多加一层反而难核对。
|
|
12
|
+
- 整行在列宽内时写一行;放不下时按组换行,各组对齐。不要把多个否定串成一行。
|
|
13
|
+
- 自增自减单独成句;不要在同一个表达式里对同一对象多次修改,那是未定义行为。
|
|
14
|
+
|
|
15
|
+
## 依据
|
|
16
|
+
|
|
17
|
+
- [ISO C++ `[expr]`](https://eel.is/c++draft/expr):运算符优先级、求值顺序与未定义行为。
|
|
18
|
+
- [cppreference: Expressions](https://en.cppreference.com/w/cpp/language/expressions):运算符分类、短路与序列点规则。
|
|
19
|
+
- [C++ Core Guidelines: ES.41](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#s-expr):避免容易误读的表达式,必要时加括号。
|
|
20
|
+
- [Style Guide for Writing Comparisons in Conditions](https://www.enyo.de/fw/notes/style-guide-conditions.html):变化最快、最不确定的一方写在比较左边;常量前置已无必要,且常掩盖本意。
|
|
21
|
+
- [提议中的 clang-tidy 检查 `readability-constant-operand-order`](https://github.com/llvm/llvm-project/pull/167158):仍为 open 的 PR #167158,尚未合入;其方向是现代代码库统一写 `x == 0`,不写 `0 == x`。
|
|
22
|
+
- [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`),不把常量前置。
|
|
23
|
+
- [C++ Core Guidelines: ES.70、ES.78、ES.79](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#s-expr):有选择时优先 `switch`;对常量比较,通常比 `if`-`then`-`else` 链更容易优化,并能提示枚举是否覆盖全;不依赖隐式穿透,`default` 只处理常见情况。
|
|
24
|
+
- [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`)。
|
|
25
|
+
- [Google C++ Style Guide: switch statements](https://google.github.io/styleguide/cppguide.html#switch-statements):非枚举条件的 `switch` 必须有 `default`;枚举由编译器提醒未覆盖值;`[[fallthrough]]` 必须标注。
|
|
26
|
+
- 关于多分支与提前返回:[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`。
|
|
27
|
+
- 肯定/否定谓词:[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 的条文只作跨语言旁证。
|
|
28
|
+
- 3 个以上同类值用数组加下标:没有一手规范规定这一点,属于本工作流的项目约定;理由是同一段循环只写一次,新增一项不必再起一个名字。
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# 函数、参数与返回值
|
|
2
|
+
|
|
3
|
+
- 命名规则以 `references/naming/identifier-names.md` 为准:函数 PascalCase、查询 `Is` / `Has` / `Can` / `Get` / `Find`、转换 `SourceToTarget`。
|
|
4
|
+
- 输入参数默认按值传小对象、按 `const&` 传大对象;需要修改调用方对象时用引用参数,并在名字或注释里标 `[out]`。
|
|
5
|
+
- 优先返回值而不是输出参数;需要返回多个值时用 `struct` 聚合,不用多个 `bool` 输出参数。
|
|
6
|
+
- 返回容器或大对象时按值返回,依赖 RVO / 移动;不要返回局部对象的引用或指针。
|
|
7
|
+
- 单参数构造函数加 `explicit`,除非确实需要隐式转换;转换运算符也加 `explicit`。
|
|
8
|
+
- 不在虚函数上写默认参数;默认参数在静态类型处绑定,派生类改默认值会造成意外。
|
|
9
|
+
- 函数只做一件事;超过一屏时先找可命名的子步骤,分阶段与注释写法见 `steps/04-implementation.md`。
|
|
10
|
+
- lambda 捕获按需列名,不默认写 `[this]`:只在确实要读写成员时才捕获 `this`,只读个别值时按名捕获,纯计算写 `[]`。需要成员访问时优先把要用的值取到局部变量再捕获;捕获 `this` 的 lambda 不得比对象活得更久,异步或存储回调前先复制需要的值。
|
|
11
|
+
|
|
12
|
+
## 依据
|
|
13
|
+
|
|
14
|
+
- [ISO C++ `[dcl.fct]`](https://eel.is/c++draft/dcl.fct):函数声明、参数与返回类型。
|
|
15
|
+
- [cppreference: Functions](https://en.cppreference.com/w/cpp/language/functions):重载、默认参数与返回类型推导。
|
|
16
|
+
- [Google C++ Style Guide: Functions](https://google.github.io/styleguide/cppguide.html#Functions):短小、单一职责,参数与返回值写清契约。
|
|
17
|
+
- [Google C++ Style Guide: Lambda expressions](https://google.github.io/styleguide/cppguide.html#Lambda_expressions):优先显式捕获、按值捕获要用的对象;捕获 `this` 的 lambda 不能比对象活得更久。
|
|
@@ -3,7 +3,13 @@
|
|
|
3
3
|
- `struct` 用于公开数据的聚合;`class` 用于维护不变量、隐藏表示。两者只差默认访问权限,按语义选。
|
|
4
4
|
- 数据成员默认私有;需要暴露只读视图时提供 `const` 成员函数,不返回可变内部引用。
|
|
5
5
|
- 单参数构造函数用 `explicit`;构造函数失败时抛异常,不留下半初始化对象。
|
|
6
|
-
- 特殊成员(构造、析构、拷贝、移动)在头文件声明,在 `.cpp`
|
|
6
|
+
- 特殊成员(构造、析构、拷贝、移动)在头文件声明,在 `.cpp` 定义;只有默认实现足够时才写 `= default`,默认构造要无参数,拷贝/移动构造按各自的签名。
|
|
7
7
|
- 定义了析构函数、拷贝构造或拷贝赋值之一时,认真考虑其余特殊成员;用 `= delete` 明确禁止不需要的操作。
|
|
8
8
|
- 多态基类析构函数必须是 `virtual` 或 `protected`;否则通过基类指针删除派生对象是未定义行为。
|
|
9
9
|
- 不在构造函数里调用虚函数;此时派生部分还没构造完。
|
|
10
|
+
|
|
11
|
+
## 依据
|
|
12
|
+
|
|
13
|
+
- [ISO C++ `[class]`](https://eel.is/c++draft/class):类成员、访问控制与特殊成员的声明规则。
|
|
14
|
+
- [cppreference: Classes](https://en.cppreference.com/w/cpp/language/classes):构造/析构、拷贝/移动与 `= default`/`= delete`。
|
|
15
|
+
- [Google C++ Style Guide: Classes](https://google.github.io/styleguide/cppguide.html#Classes):struct 与 class 的分工、显式构造函数与虚析构。
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# RAII、所有权与移动
|
|
2
|
+
|
|
3
|
+
- 资源获取即初始化:用对象生存期管理内存、文件、锁、套接字;析构函数负责释放。
|
|
4
|
+
- 默认用 `std::unique_ptr<T>` 表达独占所有权;需要共享所有权时才用 `std::shared_ptr<T>`,并用 `std::weak_ptr` 打破循环。
|
|
5
|
+
- 不写裸 `new` / `delete`;需要动态数组时用 `std::vector`,需要单个对象时用 `std::make_unique`。
|
|
6
|
+
- 移动把资源从源对象转走;`std::move` 只是 `static_cast<T&&>`,本身不移动任何东西。标准只保证移动后的对象处于有效但未指定状态,智能指针通常为空,但不要依赖具体值。
|
|
7
|
+
- 移动后的对象只能重新赋值或销毁,不要读取它的值。
|
|
8
|
+
- 移动构造函数和移动赋值标记 `noexcept`,否则 `std::vector` 等容器在扩容时可能选择拷贝。
|
|
9
|
+
- 返回值直接 `return value;`,不要 `return std::move(value);`;后者阻止 RVO。
|
|
10
|
+
|
|
11
|
+
## 依据
|
|
12
|
+
|
|
13
|
+
- [ISO C++ `[class.copy.ctor]`](https://eel.is/c++draft/class.copy.ctor):移动构造与移动赋值的语义、`noexcept` 与隐式生成。
|
|
14
|
+
- [cppreference: RAII](https://en.cppreference.com/w/cpp/language/raii):资源生存期与异常安全。
|
|
15
|
+
- [C++ Core Guidelines: R.1](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#s-resource):用 RAII 管理资源,避免裸 `new`/`delete`。
|
|
@@ -18,3 +18,9 @@ T Clamp(T value, T low, T high) {
|
|
|
18
18
|
- 概念只表达语法和语义约束,不替代文档;错误消息要能让调用者知道违反了哪条约束。
|
|
19
19
|
- CTAD(类模板实参推导)用于局部变量和明显场景:`std::pair pair{1, 2.0};`;公共接口写显式模板实参,避免推导规则变化影响契约。
|
|
20
20
|
- 不用模板做运行期分支;需要运行期多态时用虚函数或 `std::variant`。
|
|
21
|
+
|
|
22
|
+
## 依据
|
|
23
|
+
|
|
24
|
+
- [ISO C++ `[temp]`](https://eel.is/c++draft/temp):模板定义、实例化与两阶段名字查找。
|
|
25
|
+
- [cppreference: Templates](https://en.cppreference.com/w/cpp/language/templates):模板参数、特化与依赖名。
|
|
26
|
+
- [cppreference: Constraints and concepts](https://en.cppreference.com/w/cpp/language/constraints):概念、`requires` 与约束归约。
|
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
- [ ] 初始化方式统一,窄化转换被编译器拦住。
|
|
4
4
|
- [ ] `auto` 只出现在类型显然的局部变量;公共接口有显式类型。
|
|
5
5
|
- [ ] 共享常量用 `inline constexpr`,没有宏常量。
|
|
6
|
-
- [ ]
|
|
6
|
+
- [ ] 局部变量不加 `const`;`const` 用在参数、成员函数与只读视图上。
|
|
7
|
+
- [ ] 比较不是 Yoda 形式;成员与参数比较时成员状态在左、参数在右;混合优先级有括号。
|
|
7
8
|
- [ ] 资源由 RAII 管理,没有裸 `new` / `delete`。
|
|
8
9
|
- [ ] 特殊成员声明与定义位置正确,移动操作 `noexcept`。
|
|
9
10
|
- [ ] 模板约束用概念表达,定义在头文件。
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# 测量顺序
|
|
2
|
+
|
|
3
|
+
先证明慢在哪里,再改代码。没有测量的“优化”通常只是把复杂度搬了个位置。
|
|
4
|
+
|
|
5
|
+
本组条文只管性能判断与测量;语言规则见 `references/language/language-basics/01-objects.md`,容器与算法选择见 `references/language/standard-library/01-containers.md`,静态检查开关见 `engineering` 的 `references/formatting/03-clang-tidy.md`。
|
|
6
|
+
|
|
7
|
+
1. 写清可观察的性能目标:延迟、吞吐、内存上限,或“输入规模翻倍时耗时如何增长”。
|
|
8
|
+
2. 用真实规模的输入测量;玩具数据上的常数差异没有意义。
|
|
9
|
+
3. 先看算法复杂度,再看内存访问,再看常数因子。
|
|
10
|
+
4. 用 profiler 找热点;不要凭代码长短猜。
|
|
11
|
+
5. 改一处,复测同一指标;记录测量条件和结果。
|
|
12
|
+
|
|
13
|
+
- 发布构建(`-O2` / `-O3`)测性能,调试构建测正确性。
|
|
14
|
+
- 微基准要防止编译器把结果优化掉;使用输入、累加结果并输出。
|
|
15
|
+
- 单次测量受缓存、频率、调度影响;取多次的中位数或分位数,不只报最好的一次。
|
|
16
|
+
- 采样 profiler(Linux `perf record`、macOS Instruments、Windows ETW)给出热点分布;调用次数与单次耗时一起看,避免被高频小函数带偏。
|
|
17
|
+
|
|
18
|
+
## 依据
|
|
19
|
+
|
|
20
|
+
- [ISO C++ `[intro.abstract]`](https://eel.is/c++draft/intro.abstract):as-if 规则允许编译器重排,性能结论只能来自测量。
|
|
21
|
+
- [C++ Core Guidelines: Per.1](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#s-performance):没有理由不要优化;先测量再改。
|
|
22
|
+
- [C++ Core Guidelines: Per.6](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#s-performance):没有测量就没有性能结论。
|
|
@@ -5,3 +5,9 @@
|
|
|
5
5
|
- 频繁按 key 查找时,`unordered_map` 通常比 `map` 快,但迭代顺序不稳定;需要有序输出时不要选它。
|
|
6
6
|
- 小规模数据(几十个元素)上,连续 `vector` 的线性扫描常比哈希表更快;不要机械套用复杂度。
|
|
7
7
|
- 递归深度可能随输入增长时,改成迭代或显式栈;栈溢出不是可捕获的异常。
|
|
8
|
+
|
|
9
|
+
## 依据
|
|
10
|
+
|
|
11
|
+
- [ISO C++ `[container.requirements]`](https://eel.is/c++draft/container.requirements):各容器的复杂度保证。
|
|
12
|
+
- [cppreference: Containers library](https://en.cppreference.com/w/cpp/container):查找、插入、遍历的复杂度对照。
|
|
13
|
+
- [C++ Core Guidelines: Per.4](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#s-performance):理解算法与数据结构的成本,再谈常数因子。
|
|
@@ -3,6 +3,12 @@
|
|
|
3
3
|
- 连续存储对缓存友好;`vector<struct>` 比 `vector<unique_ptr<struct>>` 更容易预取。
|
|
4
4
|
- 只遍历部分字段时,AoS(数组的结构)可能浪费带宽;数据量大且按列访问时考虑 SoA(结构的数组),但先用 profiler 证明。
|
|
5
5
|
- 小对象按值传递通常比堆分配快;不要为了“避免拷贝”给所有参数加指针。
|
|
6
|
-
- `reserve` 减少 `vector`
|
|
6
|
+
- `reserve` 减少 `vector` 扩容和搬移;只有已知上界或能准确估算时才用。`reserve` 只改容量、不改 size;`shrink_to_fit` 不保证真正释放。
|
|
7
7
|
- 移动大对象比拷贝便宜,但移动不是免费;不要把 `std::move` 当成优化开关。
|
|
8
8
|
- 返回局部对象直接 `return value;`,依赖 RVO / NRVO;`return std::move(value);` 会阻止优化。
|
|
9
|
+
|
|
10
|
+
## 依据
|
|
11
|
+
|
|
12
|
+
- [ISO C++ `[vector]`](https://eel.is/c++draft/vector):容量、扩容与 `reserve` 的保证。
|
|
13
|
+
- [cppreference: `std::vector`](https://en.cppreference.com/w/cpp/container/vector):容量接口与迭代器失效。
|
|
14
|
+
- [C++ Core Guidelines: Per.16](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#s-performance):用紧凑的连续数据结构提升缓存命中。
|