@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
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
<!-- 读取时机:设计会话、选着、规则与构建边界时;读完能回答:会话层和程序边界;预算:一次只开这一份(88 行),不自动串联。 -->
|
|
2
|
+
# 会话层与程序边界
|
|
3
|
+
|
|
4
|
+
先定接口,再写实现;实现碰到下层缺口时,回头改接口,小步迭代。本文按会话、选着、规则、构建四层说明这条原则;领域值、结果类型与函数契约仍以 [01-project-design.md](01-project-design.md) 为准,本文只补充「谁持有流、谁做决定、谁不该知道终端」以及三者的构建边界。
|
|
5
|
+
|
|
6
|
+
## 会话
|
|
7
|
+
|
|
8
|
+
会话层把一次进程的生命周期包成一个对象。它持有输入、输出、诊断三股流,持有唯一的一局和唯一的一只电脑;构造只绑定引用,不读、不写、不打印。
|
|
9
|
+
|
|
10
|
+
- 入口只做装配:`main` 构造会话并调用一个 `Run`,捕获异常、打印一行诊断、返回退出码;不解析棋盘,不选难度,不写提示语。
|
|
11
|
+
- 会话按顺序问完开局问题(模式、难度、先手),清掉上一局,再进入一局循环;换局只撤回旧落子,不重新构造会话或电脑。
|
|
12
|
+
- 所有提示和诊断走注入的流;会话不直接使用 `std::cout`、`std::cerr` 或全局状态,测试才能用 `std::istringstream` / `std::ostringstream` 驱动完整交互。
|
|
13
|
+
- 依赖只朝内:会话知道规则和电脑,规则与电脑不知道会话;把终端代码编进静态库,等于让规则依赖终端。
|
|
14
|
+
- 先跑通「读一步、走一步、打印一步」的骨架,再加回放、撤销、难度等分支;每加一段,回看会话接口是否仍只有一个入口、是否泄漏了本层不该知道的状态。
|
|
15
|
+
|
|
16
|
+
tictactoe 的会话是 `Terminal`(`games/tictactoe/tictactoe/include/terminal.h`、`src/terminal.cpp`):构造函数只写 `: in_{in}, out_{out}, err_{err} {}`,`Run` 依次读 `MatchMode`、人机时读 `Difficulty`、读先手,`while (game_.Undo()) {}` 清局,再调用 `PlayMatch`。`main` 只有构造、`Run`、捕获异常三件事。
|
|
17
|
+
|
|
18
|
+
## 选择
|
|
19
|
+
|
|
20
|
+
选着层负责「从合法候选里挑一个」,不拥有对局,也不改对局。它持有自己的策略状态(难度、随机数引擎),向规则层要一个合法着法。
|
|
21
|
+
|
|
22
|
+
- 对外只暴露一个选择入口,例如 `ChoosePosition(Game&)`;随机选与搜索选留在私有函数里,调用方不需要知道难度分支怎样实现。
|
|
23
|
+
- 返回 `std::optional<Position>`:没有合法候选是正常结果,不返回哨兵坐标,不抛异常;终局由调用方先判断,也可以由选择入口防御性返回空。
|
|
24
|
+
- 选择函数在返回时保持对局与调用前一致。搜索用同一局「试走一步、递归、撤回」,不复制整局;复制把 O(1) 的撤回变成 O(n) 的拷贝,搜索树一深就被放大。
|
|
25
|
+
- 难度是封闭枚举(如 `Difficulty::kNormal` / `kHard`),不是 `bool` 或整数;新增一档必须同时补 `switch` 分支,让 `-Wswitch` 与 `std::unreachable()` 兜住漏档。
|
|
26
|
+
- 没有第二个真实实现之前不加抽象基类或策略工厂;先把一个具体类写对,调用方只依赖它的公开接口。
|
|
27
|
+
- 选着层不读终端、不打印、不读环境;随机源由构造函数注入或播种,测试用固定种子复现同一序列。
|
|
28
|
+
|
|
29
|
+
tictactoe 的选着是 `Computer`(`games/tictactoe/tictactoe/include/computer.h`、`src/computer.cpp`):`Difficulty{kNormal, kHard}`、`std::mt19937 engine_`、`ChoosePosition`,私有 `ChooseRandomPosition` / `ChooseBestPosition` / `Minimax` / `ScoreFinishedGame`;写搜索时发现 `Game` 需要公开 `Undo()` 与 `IsInProgress()`,于是回到规则层补接口,而不是在电脑里复制一份棋盘。
|
|
30
|
+
|
|
31
|
+
## 规则
|
|
32
|
+
|
|
33
|
+
规则层只依赖标准库和领域类型,回答「这一手是否合法、这一局是否结束、终局分数是多少」。
|
|
34
|
+
|
|
35
|
+
- 不读终端、不碰 GUI/Web/Python、不读当前时间、环境变量或工作目录;这些由会话或调用方注入。
|
|
36
|
+
- 组合优先:`Game` 拥有 `Board`,`Board` 拥有方格;封闭取值用 `enum class`;结果跟随产生它的层(`PlaceResult`、`GameResult`)。
|
|
37
|
+
- 查询与变更分开;一个函数只做一件事;`Undo()` 只撤回受影响的一处,既服务「换局清盘」,也服务搜索回溯。
|
|
38
|
+
- 信息隐藏按「哪些决定会变」分解,不按流程图分解;接口只暴露稳定契约,私有实现留在 `.cpp` 或私有头。
|
|
39
|
+
- 终局评分是规则查询:`ScoreFinishedGame(const Game&)` 读 `GameResult` 与 `move_count`,X 胜为正、O 胜为负、进行中或平局为 0;不把分数塞进 `GameResult`,也不在电脑里重复判断胜负。
|
|
40
|
+
|
|
41
|
+
## 构建
|
|
42
|
+
|
|
43
|
+
构建边界让「规则库」「会话程序」「测试」各自独立编译,依赖方向与源码一致。
|
|
44
|
+
|
|
45
|
+
- 规则库只编规则与选着:`add_library(tictactoe STATIC src/square.cpp src/board.cpp src/game.cpp src/computer.cpp)`,不含 `terminal.cpp`。
|
|
46
|
+
- 程序目标编会话与入口:`games/tictactoe/apps/CMakeLists.txt` 的 `app` 编 `src/main.cpp` 与 `../tictactoe/src/terminal.cpp`,再 `target_link_libraries(app PRIVATE tictactoe)`;会话层不进库,库也不包含 `<iostream>` 或打印。
|
|
47
|
+
- 测试目标按被测层取源文件:规则测试链规则库;终端测试直接编 `../src/terminal.cpp` 并链规则库,避免为了测试把会话塞进库。
|
|
48
|
+
- `target_link_libraries` 的硬性要求只有第一个参数已由 `add_executable` 或 `add_library` 创建;被链接的库目标可以在本次 configure 稍后出现,CMake 会自动补 ordering dependency。项目仍约定先定义库、再定义程序,这是可读性与顺序一致,不是 CMake 规范。
|
|
49
|
+
- 构建树的 RPATH 与安装树分开:`BUILD_RPATH` 可用 `$ORIGIN/../lib` 指向构建产物;`INSTALL_RPATH` 为空时安装后的可执行文件不会沿用构建树路径。
|
|
50
|
+
- 程序边界由 `main` 定义;库不定义 `main`。头文件自包含、每个符号由直接包含的头导出(SF.11 与 IWYU)是编译单元边界,不是链接边界,放在本节作为「库能独立编译」的旁证,不替代目标依赖规则。
|
|
51
|
+
|
|
52
|
+
tictactoe 的四份 CMake 是这条边界的实物:`games/tictactoe/tictactoe/CMakeLists.txt` 的库不含终端源,`games/tictactoe/apps/CMakeLists.txt` 才有 `app`,`games/tictactoe/tictactoe/tests/CMakeLists.txt` 的 `terminal_test` 直接编 `../src/terminal.cpp`,根 `games/tictactoe/CMakeLists.txt` 只按顺序加子目录。
|
|
53
|
+
|
|
54
|
+
## 迭代记录
|
|
55
|
+
|
|
56
|
+
这条原则不是一次设计完的,而是每轮实现暴露缺口后回改接口:
|
|
57
|
+
|
|
58
|
+
- 先写 `terminal.h`,把会话入口、三股流、开局问题定成接口;`main.cpp` 只装配。
|
|
59
|
+
- 再写 `computer.h`,把 `Difficulty`、两种构造、`ChoosePosition` 定成选着接口;实现搜索时补 `Game::Undo` / `IsInProgress` 的公开契约。
|
|
60
|
+
- 最后写 `terminal.cpp` 与 CMake,把会话、规则库、测试拆成三个构建目标;测试迁移时把入口与 `app` 目标移入 `apps/`,会话逻辑没有进入静态库。
|
|
61
|
+
|
|
62
|
+
## 依据
|
|
63
|
+
|
|
64
|
+
会话与设计方法:
|
|
65
|
+
|
|
66
|
+
- Wirth, N. *Program Development by Stepwise Refinement*. CACM 14(4), 1971, doi:10.1145/362575.362577。原文页本次不可达;分解与逐步细化的转引见 Cornell 页。
|
|
67
|
+
- Gries, D. *Stepwise refinement*. Cornell JavaHyperText. https://www.cs.cornell.edu/courses/JavaAndDS/stepwise/stepwise.html
|
|
68
|
+
- Freeman, S. & Pryce, N. *Growing Object-Oriented Software Guided by Tests* 目录:Ch4 “First, Test a Walking Skeleton”、Ch5 “Develop from the Inputs to the Outputs”、Ch17 “Teasing Apart Main”。https://growing-object-oriented-software.com/toc.html (书中没有字面 “outside-in” 章节,正文不把它当章节名。)
|
|
69
|
+
- Cockburn, A. *Hexagonal architecture (Ports & Adapters)*. https://alistair.cockburn.us/hexagonal-architecture/
|
|
70
|
+
- Martin, R. C. *The Clean Architecture*. https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html
|
|
71
|
+
- Dijkstra, E. W. *Notes on Structured Programming*, EWD249, 1970. https://www.cs.utexas.edu/~EWD/transcriptions/EWD02xx/EWD249/EWD249.html
|
|
72
|
+
- Parnas, D. L. *On the criteria to be used in decomposing systems into modules*. CACM 15(12), 1972, doi:10.1145/361598.361623。原文页本次不可达;按「会变的决定分解」的转引使用,正文不冒充读过原文。
|
|
73
|
+
|
|
74
|
+
选着实例:
|
|
75
|
+
|
|
76
|
+
- Stockfish `src/main.cpp`、`src/uci.h`:main 装配命令行与 UCI 引擎,UCI 层持有引擎并接收 position/go 命令。https://github.com/official-stockfish/Stockfish
|
|
77
|
+
- python-chess engine 文档:`engine.play(board, limit)` 返回着法,调用方推进棋盘。https://python-chess.readthedocs.io/en/latest/engine.html
|
|
78
|
+
- OpenSpiel `spiel_bots.h`:`virtual Action Step(const State& state) = 0;`。https://github.com/google-deepmind/open_spiel
|
|
79
|
+
- boardgame.io `src/ai/bot.ts`:`play(state, playerID)` 返回动作,不返回新状态。https://github.com/boardgameio/boardgame.io
|
|
80
|
+
- cutechess-cli `projects/cli/src/main.cpp`:CLI 只装配 Engine 与赛制。https://github.com/cutechess/cutechess
|
|
81
|
+
|
|
82
|
+
规则与构建:
|
|
83
|
+
|
|
84
|
+
- C++ Core Guidelines Enum.3(优先 `enum class`)、C.4(只在需要直接访问表示时做成员)。https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines
|
|
85
|
+
- CMake `target_link_libraries`:第一个参数必须已创建;被链接目标可由项目稍后创建并自动补 ordering dependency。https://cmake.org/cmake/help/latest/command/target_link_libraries.html
|
|
86
|
+
- CMake `BUILD_RPATH` 与 `INSTALL_RPATH`。https://cmake.org/cmake/help/latest/prop_tgt/BUILD_RPATH.html、https://cmake.org/cmake/help/latest/prop_tgt/INSTALL_RPATH.html
|
|
87
|
+
- Craig Scott, “add_subdirectory() and target_link_libraries() ordering”, CMake Discourse, 2026-03-05:只有第一个参数必须已定义;先定义后引用是推荐写法。https://discourse.cmake.org/t/add-subdirectory-and-target-link-libraries-ordering/15539
|
|
88
|
+
- C++ Core Guidelines SF.11(头文件自包含)与 include-what-you-use 的 “Why include what you use?”。https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rs-contained、https://github.com/include-what-you-use/include-what-you-use/blob/master/docs/WhyIWYU.md
|
|
89
|
+
- ISO C++ [basic.start.main]:程序只能有一个全局 `main`。https://eel.is/c++draft/basic.start.main
|
package/skills/developing-cpp/references/exceptions/{throw-rules.md → 01-exception-rules.md}
RENAMED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
<!-- 读取时机:抛异常或写异常契约时;读完能回答:异常与错误;预算:一次只开这一份(207 行),不自动串联。 -->
|
|
1
2
|
# 异常与错误
|
|
2
3
|
|
|
3
4
|
本文件是**开发阶段**的源码写作指导,只在 `developing-cpp` 被打开时阅读。插件运行时不编译 C++、不执行示例,也不把这里的规定送进任何运行时流程;它只是一份随技能分发的 Markdown 参考。
|
|
@@ -17,7 +18,7 @@ E.1 要求尽早决定错误处理策略:先问调用方还能不能继续、
|
|
|
17
18
|
|
|
18
19
|
策略一旦定下,声明、实现、测试和入口都用同一套;不要在同一层里一半返回错误码、一半抛异常。
|
|
19
20
|
|
|
20
|
-
判断是否算「混用」,看函数合同而不是看返回类型:查询必须产出一个值、失败时产不出(如 `GetSquare`
|
|
21
|
+
判断是否算「混用」,看函数合同而不是看返回类型:查询必须产出一个值、失败时产不出(如 `GetSquare` 坐标越界),就抛;写入操作把「这一手写不进」当作正常结果、调用方本来就要分支(如 `Game::Place` 返回 `PlaceResult`,玩家、终局与占用各有一个取值),就用返回值。动作可能因为多个前置条件失败、而调用方需要区分原因时,返回 `enum class` 而不是 `bool` 加输出参数。两者并存时,公共头必须把「拒绝」与「抛」的边界各写一行;没有这行注释才算混用。
|
|
21
22
|
|
|
22
23
|
## 什么时候抛
|
|
23
24
|
|
|
@@ -27,68 +28,64 @@ E.2:函数不能完成它被分配的任务时,就抛异常;构造函数
|
|
|
27
28
|
|
|
28
29
|
## 检查次序
|
|
29
30
|
|
|
30
|
-
|
|
31
|
+
同一个函数既有前置条件、又有正常结果时,先判断这次调用还能不能进行,再校验调用方送来的参数,最后把能交给下层的工作交给下层。次序按「哪条判断决定这一步能否进行」排:
|
|
31
32
|
|
|
32
|
-
1.
|
|
33
|
-
2.
|
|
34
|
-
3.
|
|
35
|
-
4.
|
|
33
|
+
1. 整局状态:对局是否结束由 `Game::GetResult` 回答,已结束返回 `kFinishedGame`,玩家、坐标、棋盘与历史都不再检查。对局已经无法继续时,再谈「轮到谁」没有意义。
|
|
34
|
+
2. 本层状态的前置条件:这一步是否轮到该玩家。对局自己记着轮次,这一条用返回值 `kInvalidPlayer` 答,调用方按结果分支,不抛。
|
|
35
|
+
3. 下层的检查与剩余的写入条件:读取目标格时由 `Board::GetSquare` 抛出 `std::out_of_range`;方格已有值返回 `kOccupiedSquare`。不要为了提前抛出而在 `Game` 里重复 `Board::IsInRange`。
|
|
36
|
+
4. 执行操作并返回 `kPlaced`:换方、写入棋盘、记录历史,三步按成员声明顺序做;构造函数预留了整盘手数的容量,写入阶段不分配内存。
|
|
36
37
|
|
|
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 只要求尽量校验输入、不规定校验函数返回什么。所以它是本工作流的约定,理由是状态不依赖参数,先判可以直接短路,也不会把终局后的落子误报成玩家错误。
|
|
38
41
|
|
|
39
42
|
参数之间按固定的语义顺序排,不要求与声明顺序一致;关键是同一函数内不来回换,注释、测试和消息都按这个次序。
|
|
40
43
|
|
|
41
44
|
```cpp
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
if (board_.GetStatus() != BoardStatus::kInProgress) {
|
|
48
|
-
return PlaceResult::kDenied;
|
|
45
|
+
PlaceResult Game::Place(Move move) {
|
|
46
|
+
// 先判终局,再判玩家与方格:失败结果的优先级由此决定。
|
|
47
|
+
if (!IsInProgress()) {
|
|
48
|
+
return PlaceResult::kFinishedGame;
|
|
49
49
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
if (result == Square::PlaceResult::kOccupied) {
|
|
54
|
-
return PlaceResult::kRejected;
|
|
50
|
+
|
|
51
|
+
if (current_player_ != move.player) {
|
|
52
|
+
return PlaceResult::kInvalidPlayer;
|
|
55
53
|
}
|
|
56
|
-
|
|
57
|
-
|
|
54
|
+
|
|
55
|
+
const SquareValue target_square_value = PlayerToSquareValue(move.player);
|
|
56
|
+
const Square target_square = board_.GetSquare(move.position);
|
|
57
|
+
if (!target_square.IsEmpty()) {
|
|
58
|
+
return PlaceResult::kOccupiedSquare;
|
|
58
59
|
}
|
|
59
|
-
|
|
60
|
+
|
|
61
|
+
current_player_ = PlayerToOpponent(current_player_);
|
|
62
|
+
board_.SetSquare(move.position, target_square_value);
|
|
63
|
+
history_.push_back(move);
|
|
64
|
+
return PlaceResult::kPlaced;
|
|
60
65
|
}
|
|
61
66
|
```
|
|
62
67
|
|
|
63
68
|
### 重复的入口检查
|
|
64
69
|
|
|
65
|
-
同一层有多个入口重复同一个 `if (!IsInRange(...)) throw ...`
|
|
70
|
+
同一层有多个入口重复同一个 `if (!IsInRange(...)) throw ...` 时,把检查收进一个函数。`std::source_location` 放在默认参数里取调用点,函数名也由它提供:
|
|
66
71
|
|
|
67
72
|
```cpp
|
|
68
|
-
void Board::CheckInRange(Position pos, std::
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
std::string_view file = location.file_name();
|
|
76
|
-
if (const std::size_t separator = file.find_last_of("/\\");
|
|
77
|
-
separator != std::string_view::npos) {
|
|
78
|
-
file.remove_prefix(separator + 1);
|
|
73
|
+
void Board::CheckInRange(Position pos, std::source_location location) {
|
|
74
|
+
if (!IsInRange(pos)) {
|
|
75
|
+
const std::string message = std::format(
|
|
76
|
+
"position {} out of range for {}x{} board, at {} ({}:{}:{})", pos, kRowCount,
|
|
77
|
+
kColumnCount, location.function_name(), location.file_name(), location.line(),
|
|
78
|
+
location.column());
|
|
79
|
+
throw std::out_of_range{message};
|
|
79
80
|
}
|
|
80
|
-
|
|
81
|
-
throw std::out_of_range{std::format(
|
|
82
|
-
"{}: position {} is out of range for board {}x{} ({}:{})", function, pos, kRowCount,
|
|
83
|
-
kColumnCount, file, location.line())};
|
|
84
81
|
}
|
|
85
82
|
```
|
|
86
83
|
|
|
87
|
-
声明写成 `static void CheckInRange(Position pos, std::
|
|
84
|
+
声明写成 `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 抛出。`,本行只写 `CheckInRange(pos)`。
|
|
88
85
|
|
|
89
|
-
`source_location` 是 C++20
|
|
86
|
+
`source_location` 是 C++20 设施;`line()` 与 `column()` 从 1 起算。测试只断言值、范围、文件名与消息里出现的函数名,源码移动后测试不会失效。
|
|
90
87
|
|
|
91
|
-
依据:[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
|
|
88
|
+
依据:[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` 的取值由适配层保证;坐标越界属于调用方的程序错误,不是对局里的正常分支,所以仍抛出。
|
|
92
89
|
|
|
93
90
|
## 异常类型
|
|
94
91
|
|
|
@@ -110,7 +107,7 @@ class ParseError : public std::runtime_error {
|
|
|
110
107
|
|
|
111
108
|
- 返回值:两种模式完全一致。函数用返回值报告可恢复失败时,Debug 不得改成抛异常,Release 也不得把异常改成错误码。
|
|
112
109
|
- 标准异常:两种模式的抛出条件相同。越界抛 `std::out_of_range`,前置条件被破坏抛 `std::logic_error`;项目若定义 `Error` 基类,派生类型同时派生自 `std::exception`,Debug 的 `what()` 可带 `file:line`,Release 只留值。
|
|
113
|
-
- `Check`:项目保留这种断言时,两种模式都检查,失败时打印诊断并终止;用于 invariant 已被破坏、继续执行没有意义的场合。没有 `Check`
|
|
110
|
+
- `Check`:项目保留这种断言时,两种模式都检查,失败时打印诊断并终止;用于 invariant 已被破坏、继续执行没有意义的场合。没有 `Check` 的项目:两种模式都必须执行的检查用 `std::terminate`(失败前先打印诊断),只在开发期做的自检用 `assert`,不另写包装。
|
|
114
111
|
- `DebugCheck`:项目保留时只在 Debug 生效,`NDEBUG` 后展开为空;用于代价高、只在开发期值得做的自检。不得用它承担 Release 也必须执行的检查。
|
|
115
112
|
|
|
116
113
|
`Check` 不是 `[[noreturn]]`:条件成立时正常返回。`DebugCheck` 的参数在 Release 不求值,不能放入有副作用的表达式。
|
|
@@ -148,7 +145,7 @@ int main() {
|
|
|
148
145
|
|
|
149
146
|
## 消息
|
|
150
147
|
|
|
151
|
-
`what()` 是给程序员看的诊断文本,句式是「标签: 完整句」。标签写明来源,句子主语是失败的值或对象,谓语说明哪里不对,实际值直接插进句子,限度用 `for` 接在句末。标签是标识符,成员写 `Type::Function
|
|
148
|
+
`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`,不要用分号堆叠。
|
|
152
149
|
|
|
153
150
|
英文消息只用 `:`、`()`、`,`、`=` 四个 ASCII 符号:冒号紧贴标签、后接一个空格;括号只放次要值或补充说明;逗号前不空、后一个空格;等号两侧不空。不用第二个冒号、句号、问号、感叹号、破折号或 `×`;句子小写开头,句末不加句号;尺寸写 `3x3`,`out of range` 不加连字符。中文消息按中文标点,但句式与「值在前、限度在后」的顺序不变。
|
|
154
151
|
|
|
@@ -158,33 +155,31 @@ int main() {
|
|
|
158
155
|
|
|
159
156
|
`what()` 不是分支接口;调用方要数字就放进类型成员,不解析字符串。入口可以在消息前加面向用户的前缀,库代码不打印日志。
|
|
160
157
|
|
|
161
|
-
写进 `throw`
|
|
158
|
+
写进 `throw` 时,短消息直接内联;整条 `throw` 一行写不下、需要折行时,先把消息存进 `const std::string message`,再 `throw ...{message};`。公共函数的消息带标签:
|
|
162
159
|
|
|
163
160
|
```cpp
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
kColumnCount)};
|
|
161
|
+
const std::string message = std::format("ParseConfig: line {} has no key", line_number);
|
|
162
|
+
throw ParseError{message};
|
|
167
163
|
```
|
|
168
164
|
|
|
169
|
-
|
|
165
|
+
跨行时先把消息存进 `const std::string message`,再写一行 `throw`:`throw` 行只表达抛什么,格式串与参数留在上一句,读者不必先跨过消息才能看到异常类型的结束。消息没有插值时仍写一行 `throw std::logic_error{"ReadConfig: the first line is empty"};`。私有检查函数由 `source_location` 取调用点,不带标签,句式见上文「重复的入口检查」。
|
|
170
166
|
|
|
171
|
-
常用库把消息的构造放进异常类型或辅助函数:spdlog 的 `spdlog_ex` 在构造函数里写 `
|
|
167
|
+
常用库把消息的构造放进异常类型或辅助函数:spdlog 的 `spdlog_ex` 在构造函数里写 `msg_`,CLI11 的 `Error` 族在静态工厂里拼好句子,POCO 的 `Exception` 在构造函数里存消息;调用处因此只有一行 `throw ...(...)`。本工作流在整条 `throw` 跨行时引入 `const std::string message`(需要 `#include <string>`),变量名固定,不写 `std::move`;异常类型构造时复制消息。
|
|
172
168
|
|
|
173
|
-
Rust 把失败分成两类:`Result` 表示可预期的失败,用 `?` 传播;`panic!` 只表示程序里的 bug(invariant
|
|
169
|
+
Rust 把失败分成两类:`Result` 表示可预期的失败,用 `?` 传播;`panic!` 只表示程序里的 bug(invariant 已被破坏),消息同样在调用点构造;跨行时先在调用点拼好再抛出。C++ 的异常同时承担这两种职责,本工作流因此把「调用方无法继续」写成异常,把「可恢复的拒绝」写成返回值。
|
|
174
170
|
|
|
175
|
-
|
|
176
|
-
throw std::out_of_range{std::format(
|
|
177
|
-
"Board::GetSquare: position {} is out of range for board {}x{}", pos, kRowCount,
|
|
178
|
-
kColumnCount)};
|
|
171
|
+
这两条是本工作流的选择,不是普遍更优:Google C++ Style Guide 整体禁用异常,只保留「用户输入不应引发异常」这一条共识;Core Guidelines E.27 的说法是「不能抛异常时改用错误码系统」。C++23 项目也可以把可预期的失败写成 `std::expected<T, E>`,或用带字段的 struct 一次返回多个值;out 参数在本工作流里一律不用。
|
|
179
172
|
|
|
180
|
-
|
|
181
|
-
|
|
173
|
+
```cpp
|
|
174
|
+
const std::string message =
|
|
175
|
+
std::format("ReadConfig: {} has no header because the first line is empty", path);
|
|
176
|
+
throw ParseError{message};
|
|
182
177
|
|
|
183
|
-
throw std::logic_error{"
|
|
184
|
-
throw std::
|
|
178
|
+
throw std::logic_error{"ReadConfig: the first line is empty"};
|
|
179
|
+
throw std::format_error{"Position: format specifier is not supported, use {} instead"};
|
|
185
180
|
```
|
|
186
181
|
|
|
187
|
-
反例:`
|
|
182
|
+
反例:`Game::Place: the move is not acceptable (player={}); the game is in progress`。标签已经说明是 `Game::Place`,句子又用 `the move` 当空泛主语,实际值还放在括号里;读者要在标签、主语和括号之间来回拼。
|
|
188
183
|
|
|
189
184
|
## 声明注释
|
|
190
185
|
|
|
@@ -202,11 +197,11 @@ Config ReadConfig(const std::filesystem::path& path);
|
|
|
202
197
|
- [ ] 错误策略在接口设计时已确定,同一层没有混用错误码和异常。
|
|
203
198
|
- [ ] 构造函数失败时抛异常,没有留下半初始化对象。
|
|
204
199
|
- [ ] 异常类型有语义,没有抛内建类型、字符串或裸指针。
|
|
205
|
-
- [ ] `throw` 直接接收 `std::format(...)
|
|
200
|
+
- [ ] 一行写得下的 `throw` 直接接收 `std::format(...)` 或字面量;跨行时先建 `const std::string message`,再 `throw` 变量。
|
|
206
201
|
- [ ] 按值抛出、按 `const` 引用捕获;重抛用 `throw;`。
|
|
207
202
|
- [ ] 析构、`swap`、异常类型的拷贝/移动不抛。
|
|
208
203
|
- [ ] 没有用异常做正常分支,也没有在每个函数里捕获所有异常。
|
|
209
|
-
- [ ] 本层负责的参数校验与状态检查排在正常结果返回之前;非法参数没有被 `
|
|
204
|
+
- [ ] 本层负责的参数校验与状态检查排在正常结果返回之前;非法参数没有被 `kXWin`、`kOWin` 或 `kDraw` 之类的终局结果掩盖。
|
|
210
205
|
- [ ] 入口有统一捕获,消息可定位,退出码符合项目合同。
|
|
211
206
|
- [ ] 公共声明的 `@throws` 与实际抛出类型一致,且 Debug/Release 的抛出条件相同。
|
|
212
207
|
- [ ] 每个 `noexcept` 函数的函数体不调用 `at()` 或其他会抛的接口。
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
<!-- 读取时机:抛异常或写异常契约时;读完能回答:正反例:Debug 与 Release 的三层合同;预算:一次只开这一份(38 行),不自动串联。 -->
|
|
1
2
|
# 正反例:Debug 与 Release 的三层合同
|
|
2
3
|
|
|
3
4
|
**差**:同一个函数在 Debug 抛异常、在 Release 返回 false;调用方被迫按构建模式写两套分支。
|
|
@@ -6,7 +7,7 @@
|
|
|
6
7
|
#ifdef NDEBUG
|
|
7
8
|
return false;
|
|
8
9
|
#else
|
|
9
|
-
throw std::out_of_range{row, col};
|
|
10
|
+
throw std::out_of_range{std::format("position ({}, {}) is out of range", row, col)};
|
|
10
11
|
#endif
|
|
11
12
|
```
|
|
12
13
|
|
|
@@ -25,13 +26,13 @@ void Place(Position pos, Player player);
|
|
|
25
26
|
**差**:用 `assert` 承担 Release 也必须执行的 invariant 检查;`NDEBUG` 后检查消失。
|
|
26
27
|
|
|
27
28
|
```cpp
|
|
28
|
-
assert(game.
|
|
29
|
+
assert(game.IsInProgress());
|
|
29
30
|
```
|
|
30
31
|
|
|
31
|
-
**好**:两种模式都必须检查的 invariant 用 `Check`(项目保留时),失败时打印诊断并终止;只在开发期值得做、Release 可以省掉的自检用 `DebugCheck
|
|
32
|
+
**好**:两种模式都必须检查的 invariant 用 `Check`(项目保留时),失败时打印诊断并终止;只在开发期值得做、Release 可以省掉的自检用 `DebugCheck`。没有这两个宏时,两种模式都必须执行的检查用 `std::terminate`(失败前先打印诊断),只在开发期做的自检用 `assert`。
|
|
32
33
|
|
|
33
34
|
```cpp
|
|
34
|
-
Check(game.
|
|
35
|
+
Check(game.IsInProgress(), "Computer::ChoosePosition: game is already over");
|
|
35
36
|
DebugCheck(index < history_.size(), "history index out of range");
|
|
36
37
|
```
|
|
37
38
|
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
<!-- 读取时机:抛异常或写异常契约时;读完能回答:正反例:异常类型与消息;预算:一次只开这一份(24 行),不自动串联。 -->
|
|
1
2
|
# 正反例:异常类型与消息
|
|
2
3
|
|
|
3
4
|
**差**:抛字符串,或把来源、条件和值揉成一句口语;调用方接到的 `what()` 既不稳定,也无法定位是哪一步失败。
|
|
@@ -13,4 +14,12 @@ throw std::out_of_range{std::format("GetTask: 下标 {} 越界", index)};
|
|
|
13
14
|
throw std::out_of_range{std::format("GetTask: index {} is out of range", index)};
|
|
14
15
|
```
|
|
15
16
|
|
|
16
|
-
|
|
17
|
+
消息需要跨行时,先存进变量再抛出:
|
|
18
|
+
|
|
19
|
+
```cpp
|
|
20
|
+
const std::string message =
|
|
21
|
+
std::format("GetTask: index {} is out of range for list of size {}", index, size);
|
|
22
|
+
throw std::out_of_range{message};
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
消息写操作和关键值;英文消息小写开头、句末不加句号,中文消息按中文标点,句式不变;实际值用 `{}` 直接放进句子,不要写成 `(index={})`。`std::format` 只是构造消息的方式,句式见 `references/exceptions/01-exception-rules.md` 的「消息」节。项目若定义 `Error` 基类,按该文件先定基类再派生,调用方捕获基类接住整族。
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
<!-- 读取时机:入口表点名或判不准时;读完能回答:每份条文的读取时机、产出与预算;预算:一次只开这一份,按需再开单份。 -->
|
|
2
|
+
# 条文索引
|
|
3
|
+
|
|
4
|
+
先读本页,再按主题或语义开一份;不要一次打开多份。
|
|
5
|
+
|
|
6
|
+
## comments
|
|
7
|
+
|
|
8
|
+
| 文件 | 读取时机 | 读完能回答 | 预算 |
|
|
9
|
+
| --- | --- | --- | --- |
|
|
10
|
+
| `comments/01-comment-rules.md` | 写或改注释时 | 注释:写什么 | 一次只开这一份(197 行),不自动串联。 |
|
|
11
|
+
| `comments/02-style.md` | 写或改注释时 | 注释的结构、版式与措辞 | 一次只开这一份(77 行),不自动串联。 |
|
|
12
|
+
| `comments/examples/01-declaration-class.md` | 写或改注释的示例时 | 正反例:类型注释:概念与约定,不是功能清单 | 一次只开这一份(65 行),不自动串联。 |
|
|
13
|
+
| `comments/examples/02-declaration-constructor.md` | 写或改注释的示例时 | 正反例:构造函数与析构函数:标签 + 职责 | 一次只开这一份(22 行),不自动串联。 |
|
|
14
|
+
| `comments/examples/03-declaration-enum.md` | 写或改注释的示例时 | 正反例:枚举 | 一次只开这一份(47 行),不自动串联。 |
|
|
15
|
+
| `comments/examples/04-declaration-file.md` | 写或改注释的示例时 | 正反例:文件头与注释边界 | 一次只开这一份(66 行),不自动串联。 |
|
|
16
|
+
| `comments/examples/05-declaration-function.md` | 写或改注释的示例时 | 正反例:函数声明注释 | 一次只开这一份(115 行),不自动串联。 |
|
|
17
|
+
| `comments/examples/06-declaration-member.md` | 写或改注释的示例时 | 正反例:数据成员 | 一次只开这一份(23 行),不自动串联。 |
|
|
18
|
+
| `comments/examples/07-declaration-predicate.md` | 写或改注释的示例时 | 正反例:布尔查询写主体是否状态 | 一次只开这一份(14 行),不自动串联。 |
|
|
19
|
+
| `comments/examples/08-declaration-struct.md` | 写或改注释的示例时 | 正反例:完整声明:后置条件与轴向 | 一次只开这一份(18 行),不自动串联。 |
|
|
20
|
+
| `comments/examples/09-implementation-body.md` | 写或改注释的示例时 | 正反例:实现注释 | 一次只开这一份(94 行),不自动串联。 |
|
|
21
|
+
| `comments/examples/10-implementation-todo.md` | 写或改注释的示例时 | 正反例:TODO | 一次只开这一份(16 行),不自动串联。 |
|
|
22
|
+
| `comments/glossary/01-comment-terms.md` | 统一注释术语时 | 注释术语与用词 | 一次只开这一份(92 行),不自动串联。 |
|
|
23
|
+
|
|
24
|
+
## design
|
|
25
|
+
|
|
26
|
+
| 文件 | 读取时机 | 读完能回答 | 预算 |
|
|
27
|
+
| --- | --- | --- | --- |
|
|
28
|
+
| `design/examples/01-behavior-sentence.md` | 看设计案例时 | 正反例:行为句 | 一次只开这一份(14 行),不自动串联。 |
|
|
29
|
+
| `design/examples/02-coordinate-table.md` | 看设计案例时 | 正反例:固定坐标用表 | 一次只开这一份(31 行),不自动串联。 |
|
|
30
|
+
| `design/examples/03-header-place.md` | 看设计案例时 | 正反例:头文件位置 | 一次只开这一份(26 行),不自动串联。 |
|
|
31
|
+
| `design/examples/04-library-boundary.md` | 看设计案例时 | 正反例:边界 | 一次只开这一份(30 行),不自动串联。 |
|
|
32
|
+
| `design/examples/05-library-split.md` | 看设计案例时 | 正反例:单文件何时拆库 | 一次只开这一份(7 行),不自动串联。 |
|
|
33
|
+
| `design/examples/06-move-result.md` | 看设计案例时 | 正反例:落子结果用返回值回答 | 一次只开这一份(41 行),不自动串联。 |
|
|
34
|
+
| `design/examples/07-preview-read.md` | 看设计案例时 | 正反例:推演一步读原数据 | 一次只开这一份(36 行),不自动串联。 |
|
|
35
|
+
| `design/01-project-design.md` | 设计类型、接口或目录时 | 领域建模与项目设计 | 一次只开这一份(160 行),不自动串联。 |
|
|
36
|
+
| `design/session-layer.md` | 设计会话、选着、规则与构建边界时 | 会话层与程序边界 | 一次只开这一份(88 行),不自动串联。 |
|
|
37
|
+
|
|
38
|
+
## exceptions
|
|
39
|
+
|
|
40
|
+
| 文件 | 读取时机 | 读完能回答 | 预算 |
|
|
41
|
+
| --- | --- | --- | --- |
|
|
42
|
+
| `exceptions/examples/01-throw-boundary.md` | 抛异常或写异常契约时 | 正反例:边界 | 一次只开这一份(28 行),不自动串联。 |
|
|
43
|
+
| `exceptions/examples/02-throw-build-mode.md` | 抛异常或写异常契约时 | 正反例:Debug 与 Release 的三层合同 | 一次只开这一份(38 行),不自动串联。 |
|
|
44
|
+
| `exceptions/examples/03-throw-catch.md` | 抛异常或写异常契约时 | 正反例:捕获 | 一次只开这一份(21 行),不自动串联。 |
|
|
45
|
+
| `exceptions/examples/04-throw-destructor.md` | 抛异常或写异常契约时 | 正反例:析构 | 一次只开这一份(21 行),不自动串联。 |
|
|
46
|
+
| `exceptions/examples/05-throw-format.md` | 抛异常或写异常契约时 | 正反例:`std::format` 与 `std::print` | 一次只开这一份(13 行),不自动串联。 |
|
|
47
|
+
| `exceptions/examples/06-throw-message.md` | 抛异常或写异常契约时 | 正反例:异常类型与消息 | 一次只开这一份(24 行),不自动串联。 |
|
|
48
|
+
| `exceptions/examples/07-throw-when.md` | 抛异常或写异常契约时 | 正反例:什么时候抛 | 一次只开这一份(27 行),不自动串联。 |
|
|
49
|
+
| `exceptions/01-exception-rules.md` | 抛异常或写异常契约时 | 异常与错误 | 一次只开这一份(207 行),不自动串联。 |
|
|
50
|
+
|
|
51
|
+
## language
|
|
52
|
+
|
|
53
|
+
| 文件 | 读取时机 | 读完能回答 | 预算 |
|
|
54
|
+
| --- | --- | --- | --- |
|
|
55
|
+
| `language/glossary/01-language-terms.md` | 查语言细节时 | 术语 | 一次只开这一份(88 行),不自动串联。 |
|
|
56
|
+
| `language/language-basics/01-objects.md` | 查语言细节时 | 值与对象 | 一次只开这一份(15 行),不自动串联。 |
|
|
57
|
+
| `language/language-basics/02-initialization.md` | 查语言细节时 | 初始化与列表初始化 | 一次只开这一份(26 行),不自动串联。 |
|
|
58
|
+
| `language/language-basics/03-deduction.md` | 查语言细节时 | `auto` 与类型推导 | 一次只开这一份(12 行),不自动串联。 |
|
|
59
|
+
| `language/language-basics/04-const.md` | 查语言细节时 | `const`、`constexpr`、`consteval`、`constinit` | 一次只开这一份(25 行),不自动串联。 |
|
|
60
|
+
| `language/language-basics/05-expressions.md` | 查语言细节时 | 表达式与比较 | 一次只开这一份(31 行),不自动串联。 |
|
|
61
|
+
| `language/language-basics/06-functions.md` | 查语言细节时 | 函数、参数与返回值 | 一次只开这一份(17 行),不自动串联。 |
|
|
62
|
+
| `language/language-basics/07-classes.md` | 查语言细节时 | 类、结构体与特殊成员 | 一次只开这一份(15 行),不自动串联。 |
|
|
63
|
+
| `language/language-basics/08-raii.md` | 查语言细节时 | RAII、所有权与移动 | 一次只开这一份(15 行),不自动串联。 |
|
|
64
|
+
| `language/language-basics/09-templates.md` | 查语言细节时 | 模板、概念与 CTAD | 一次只开这一份(26 行),不自动串联。 |
|
|
65
|
+
| `language/language-basics/10-language-checklist.md` | 查语言细节时 | 检查清单 | 一次只开这一份(11 行),不自动串联。 |
|
|
66
|
+
| `language/performance/01-measure.md` | 查语言细节时 | 测量顺序 | 一次只开这一份(22 行),不自动串联。 |
|
|
67
|
+
| `language/performance/02-complexity.md` | 查语言细节时 | 复杂度与数据结构 | 一次只开这一份(13 行),不自动串联。 |
|
|
68
|
+
| `language/performance/03-memory.md` | 查语言细节时 | 内存与缓存 | 一次只开这一份(14 行),不自动串联。 |
|
|
69
|
+
| `language/performance/04-undefined-behavior.md` | 查语言细节时 | 未定义行为与安全 | 一次只开这一份(28 行),不自动串联。 |
|
|
70
|
+
| `language/performance/05-pitfalls.md` | 查语言细节时 | 常见性能陷阱 | 一次只开这一份(17 行),不自动串联。 |
|
|
71
|
+
| `language/performance/06-false-optimizations.md` | 查语言细节时 | 不要做的“优化” | 一次只开这一份(13 行),不自动串联。 |
|
|
72
|
+
| `language/performance/07-checklist.md` | 查语言细节时 | 检查清单 | 一次只开这一份(8 行),不自动串联。 |
|
|
73
|
+
| `language/standard-library/01-containers.md` | 查语言细节时 | 容器选择 | 一次只开这一份(29 行),不自动串联。 |
|
|
74
|
+
| `language/standard-library/02-access.md` | 查语言细节时 | 访问与边界 | 一次只开这一份(14 行),不自动串联。 |
|
|
75
|
+
| `language/standard-library/03-iteration.md` | 查语言细节时 | 迭代与失效 | 一次只开这一份(18 行),不自动串联。 |
|
|
76
|
+
| `language/standard-library/04-algorithms.md` | 查语言细节时 | 算法优先于手写循环 | 一次只开这一份(26 行),不自动串联。 |
|
|
77
|
+
| `language/standard-library/05-string.md` | 查语言细节时 | `std::string` 与 `std::string_view` | 一次只开这一份(13 行),不自动串联。 |
|
|
78
|
+
| `language/standard-library/06-format.md` | 查语言细节时 | `std::format` 与输出 | 一次只开这一份(116 行),不自动串联。 |
|
|
79
|
+
| `language/standard-library/07-time.md` | 查语言细节时 | 时间 | 一次只开这一份(13 行),不自动串联。 |
|
|
80
|
+
| `language/standard-library/08-random.md` | 查语言细节时 | 随机 | 一次只开这一份(14 行),不自动串联。 |
|
|
81
|
+
| `language/standard-library/09-files.md` | 查语言细节时 | 文件 | 一次只开这一份(14 行),不自动串联。 |
|
|
82
|
+
| `language/standard-library/10-library-checklist.md` | 查语言细节时 | 检查清单 | 一次只开这一份(9 行),不自动串联。 |
|
|
83
|
+
| `language/value-types/01-width.md` | 查语言细节时 | 宽度、范围与 `std::size_t` | 一次只开这一份(17 行),不自动串联。 |
|
|
84
|
+
| `language/value-types/02-signed.md` | 查语言细节时 | 有符号、无符号与溢出 | 一次只开这一份(26 行),不自动串联。 |
|
|
85
|
+
| `language/value-types/03-integer-roles.md` | 查语言细节时 | 项目对整型角色的约定 | 一次只开这一份(38 行),不自动串联。 |
|
|
86
|
+
| `language/value-types/04-enum.md` | 查语言细节时 | 枚举类 | 一次只开这一份(24 行),不自动串联。 |
|
|
87
|
+
| `language/value-types/05-strong-type.md` | 查语言细节时 | 强类型包装 | 一次只开这一份(17 行),不自动串联。 |
|
|
88
|
+
| `language/value-types/06-const-method.md` | 查语言细节时 | 成员函数尾部的 `const` | 一次只开这一份(22 行),不自动串联。 |
|
|
89
|
+
| `language/value-types/07-nodiscard.md` | 查语言细节时 | `[[nodiscard]]` | 一次只开这一份(15 行),不自动串联。 |
|
|
90
|
+
| `language/value-types/08-comparison.md` | 查语言细节时 | 比较运算符 | 一次只开这一份(10 行),不自动串联。 |
|
|
91
|
+
| `language/value-types/09-subscript.md` | 查语言细节时 | 下标与 `at` | 一次只开这一份(15 行),不自动串联。 |
|
|
92
|
+
| `language/value-types/10-float-bool.md` | 查语言细节时 | 浮点与布尔 | 一次只开这一份(13 行),不自动串联。 |
|
|
93
|
+
| `language/value-types/11-checklist.md` | 查语言细节时 | 检查清单 | 一次只开这一份(8 行),不自动串联。 |
|
|
94
|
+
|
|
95
|
+
## layout
|
|
96
|
+
|
|
97
|
+
| 文件 | 读取时机 | 读完能回答 | 预算 |
|
|
98
|
+
| --- | --- | --- | --- |
|
|
99
|
+
| `layout/01-source-layout.md` | 排声明顺序时 | 源码布局 | 一次只开这一份(78 行),不自动串联。 |
|
|
100
|
+
| `layout/02-file-template.md` | 排声明顺序时 | 文件模板 | 一次只开这一份(137 行),不自动串联。 |
|
|
101
|
+
| `layout/03-declaration-order.md` | 排声明顺序时 | 声明顺序 | 一次只开这一份(150 行),不自动串联。 |
|
|
102
|
+
|
|
103
|
+
## naming
|
|
104
|
+
|
|
105
|
+
| 文件 | 读取时机 | 读完能回答 | 预算 |
|
|
106
|
+
| --- | --- | --- | --- |
|
|
107
|
+
| `naming/examples/01-naming-accessor.md` | 命名示例时 | 正反例:访问器 | 一次只开这一份(28 行),不自动串联。 |
|
|
108
|
+
| `naming/examples/02-naming-basic.md` | 命名示例时 | 正反例:基本规则 | 一次只开这一份(3 行),不自动串联。 |
|
|
109
|
+
| `naming/examples/03-naming-current-prefix.md` | 命名示例时 | 正反例:`current_` 前缀 | 一次只开这一份(19 行),不自动串联。 |
|
|
110
|
+
| `naming/examples/04-naming-local.md` | 命名示例时 | 正反例:局部名字 | 一次只开这一份(45 行),不自动串联。 |
|
|
111
|
+
| `naming/examples/05-naming-lookup.md` | 命名示例时 | 正反例:查找与读取 | 一次只开这一份(18 行),不自动串联。 |
|
|
112
|
+
| `naming/examples/06-naming-lower-layer.md` | 命名示例时 | 正反例:下层类型不要借用上层词汇 | 一次只开这一份(45 行),不自动串联。 |
|
|
113
|
+
| `naming/examples/07-naming-multiple-return.md` | 命名示例时 | 正反例:多返回值 | 一次只开这一份(20 行),不自动串联。 |
|
|
114
|
+
| `naming/examples/08-naming-player-value.md` | 命名示例时 | 正反例:玩家与方格的值 | 一次只开这一份(15 行),不自动串联。 |
|
|
115
|
+
| `naming/examples/09-naming-predicate.md` | 命名示例时 | 正反例:谓词与 Lambda | 一次只开这一份(18 行),不自动串联。 |
|
|
116
|
+
| `naming/examples/10-naming-rename.md` | 命名示例时 | 正反例:改名检查 | 一次只开这一份(5 行),不自动串联。 |
|
|
117
|
+
| `naming/examples/11-naming-responsibility.md` | 命名示例时 | 正反例:名字与职责 | 一次只开这一份(19 行),不自动串联。 |
|
|
118
|
+
| `naming/examples/12-naming-role.md` | 命名示例时 | 正反例:语义角色 | 一次只开这一份(21 行),不自动串联。 |
|
|
119
|
+
| `naming/examples/13-naming-state-change.md` | 命名示例时 | 正反例:状态变更:动作与结果枚举 | 一次只开这一份(69 行),不自动串联。 |
|
|
120
|
+
| `naming/glossary/01-naming-terms.md` | 统一命名术语时 | 命名术语与用词 | 一次只开这一份(26 行),不自动串联。 |
|
|
121
|
+
| `naming/glossary/02-position.md` | 统一命名术语时 | position 与 pos | 一次只开这一份(23 行),不自动串联。 |
|
|
122
|
+
| `naming/glossary/03-selection.md` | 判不准选择类函数用 Choose、Find 还是 Try 时 | 选择、查找与尝试 | 一次只开这一份(22 行),不自动串联。 |
|
|
123
|
+
| `naming/glossary/04-square.md` | 统一命名术语时 | square 与 SquareValue | 一次只开这一份(47 行),不自动串联。 |
|
|
124
|
+
| `naming/glossary/05-scoring.md` | 统一命名术语时 | 终局评分与搜索分 | 一次只开这一份(26 行),不自动串联。 |
|
|
125
|
+
| `naming/glossary/06-difficulty.md` | 统一命名术语时 | 电脑难度与对局模式 | 一次只开这一份(25 行),不自动串联。 |
|
|
126
|
+
| `naming/01-identifier-names.md` | 命名或统一术语时 | 命名 | 一次只开这一份(127 行),不自动串联。 |
|
|
127
|
+
|
|
128
|
+
## style
|
|
129
|
+
|
|
130
|
+
| 文件 | 读取时机 | 读完能回答 | 预算 |
|
|
131
|
+
| --- | --- | --- | --- |
|
|
132
|
+
| `style/01-google-cpp-style.md` | 查格式与写法时 | Google C++ 风格:本技能采用的版本 | 一次只开这一份(93 行),不自动串联。 |
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
<!-- 读取时机:查语言细节时;读完能回答:术语;预算:一次只开这一份(88 行),不自动串联。 -->
|
|
1
2
|
# 术语
|
|
2
3
|
|
|
3
|
-
代码标识符用英文;注释和说明用中文时,同一个概念只用一种译法。下表是本项目的默认译法,项目技能可以增补,但不要改已有词的含义。标识符怎么拼(大小写、前缀、语义角色)见 `references/naming/identifier-names.md`;本表只管术语译法与近义词辨析。
|
|
4
|
+
代码标识符用英文;注释和说明用中文时,同一个概念只用一种译法。下表是本项目的默认译法,项目技能可以增补,但不要改已有词的含义。标识符怎么拼(大小写、前缀、语义角色)见 `references/naming/01-identifier-names.md`;本表只管术语译法与近义词辨析。
|
|
4
5
|
|
|
5
6
|
| 英文 | 中文 | 说明 |
|
|
6
7
|
| --- | --- | --- |
|
|
@@ -84,5 +85,5 @@
|
|
|
84
85
|
- **声明 vs 定义**:头文件里多是声明;函数体、全局变量初始化是定义。
|
|
85
86
|
- **参数 vs 实参**:声明里叫参数(parameter),调用点叫实参(argument);中文口语都叫参数时,写文档要区分。
|
|
86
87
|
- **接口 vs 实现**:公共头是接口,`.cpp` 是实现;不要用「接口」指整个类。
|
|
87
|
-
- **异常 vs 错误**:异常是语言机制,错误是结果;是否用异常表达错误由接口合同和 invariant 决定,不能把两者当同义词。规则见 `references/exceptions/
|
|
88
|
+
- **异常 vs 错误**:异常是语言机制,错误是结果;是否用异常表达错误由接口合同和 invariant 决定,不能把两者当同义词。规则见 `references/exceptions/01-exception-rules.md`。
|
|
88
89
|
- **格式化 vs 排版**:`std::format` 组合消息;clang-format 排版源码。不要混用「格式化」指代两者。
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
<!-- 读取时机:查语言细节时;读完能回答:值与对象;预算:一次只开这一份(15 行),不自动串联。 -->
|
|
1
2
|
# 值与对象
|
|
2
3
|
|
|
3
4
|
这里只写“换一台机器、换一个编译器仍然成立”的 C++11–C++23 语言规则;示例默认 C++23,C++11/14/17/20 项目按声明降级。项目专属类型、协议字段和构建目录不在本组条文。
|
|
@@ -6,7 +7,7 @@
|
|
|
6
7
|
- 变量有名字、类型、存储期和作用域。局部变量在离开作用域时销毁;`static` 局部变量在首次执行到声明时初始化,程序结束时销毁。
|
|
7
8
|
- 声明让名字可见,定义分配存储或给出函数体。非 `inline` 的变量与函数一个程序只能定义一次;`inline` 函数/变量与模板可以出现在多个翻译单元,但定义必须一致。头文件只放声明,定义放 `.cpp`,例外是 `inline` 与模板。
|
|
8
9
|
- 引用是已有对象的别名,必须初始化,不能重新绑定;指针可以为空,也可以改指向。接口优先用引用表达“必须存在”,用指针表达“可以不存在”。
|
|
9
|
-
- `*this` 不用于复制当前对象:要一份副本就直说语义(如“推演落子”只读原数据,或显式写一个复制函数)。`return *this`
|
|
10
|
+
- `*this` 不用于复制当前对象:要一份副本就直说语义(如“推演落子”只读原数据,或显式写一个复制函数)。`return *this` 仍只属于赋值运算符,值类型没有这种运算符。
|
|
10
11
|
|
|
11
12
|
## 依据
|
|
12
13
|
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
<!-- 读取时机:查语言细节时;读完能回答:初始化与列表初始化;预算:一次只开这一份(26 行),不自动串联。 -->
|
|
1
2
|
# 初始化与列表初始化
|
|
2
3
|
|
|
3
4
|
默认用花括号初始化,只在需要拷贝初始化或 `auto` 推导时用 `=`;`name = {{` 只用于多行聚合常量表:
|
|
@@ -11,7 +12,7 @@ auto total = 0.0; // 本工作流统一用 = 推导
|
|
|
11
12
|
|
|
12
13
|
- `kLines` 这类多行聚合常量表写 `name = {{`;单个对象和空初始化仍写 `count{}`,不写 `=`;不把其他花括号初始化改成 `=`。
|
|
13
14
|
- `T value{}` 做值初始化:内置类型得 0,类类型调用默认构造函数。
|
|
14
|
-
- `auto x{1}`
|
|
15
|
+
- `auto x{1}` 的推导类型在 C++17 前后不同:C++17 起是 `int`,之前是 `std::initializer_list<int>`;`{}` 与 `initializer_list` 的重载也容易误读,本工作流统一写 `auto x = 1`。
|
|
15
16
|
- 花括号禁止窄化转换:`int x{3.5}` 编译失败,`int x = 3.5` 静默截断。需要截断时显式 `static_cast<int>(3.5)`。
|
|
16
17
|
- `std::vector<int> values(3)` 是三个 0;`values{3}` 是一个元素 3。两种括号语义不同,不要凭习惯混用。
|
|
17
18
|
- 类成员在声明处写默认成员初始化(NSDMI),一律用花括号:标量 `count_{0}`,数组 `items_{}`,容器与类类型成员写空 `{}`(如 `std::vector<int> values_{};`、`Board board_{};`)。
|