@chengzhao-dev/dsh-cpp-workflow 0.7.1 → 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.
Files changed (88) hide show
  1. package/README.en.md +16 -10
  2. package/README.md +16 -10
  3. package/cordis.patch.yml +6 -6
  4. package/lib/client.js +2 -2
  5. package/lib/index.js +45 -37
  6. package/lib/workspace.js +10 -17
  7. package/package.json +1 -1
  8. package/skills/building-cpp/SKILL.md +3 -3
  9. package/skills/building-cpp/assets/templates/projects/common/running-build.sh +0 -0
  10. package/skills/building-cpp/references/environment.md +1 -1
  11. package/skills/building-cpp/scripts/checking-environment.mjs +11 -13
  12. package/skills/building-cpp/steps/01-build.md +3 -2
  13. package/skills/building-cpp/steps/02-tooling.md +2 -2
  14. package/skills/developing-cpp/SKILL.md +10 -7
  15. package/skills/developing-cpp/references/comments/comment-rules.md +56 -42
  16. package/skills/developing-cpp/references/comments/comment-style.md +8 -12
  17. package/skills/developing-cpp/references/comments/examples/declaration-class.md +15 -16
  18. package/skills/developing-cpp/references/comments/examples/declaration-constructor.md +1 -1
  19. package/skills/developing-cpp/references/comments/examples/declaration-enum.md +17 -17
  20. package/skills/developing-cpp/references/comments/examples/declaration-file.md +3 -3
  21. package/skills/developing-cpp/references/comments/examples/declaration-function.md +30 -22
  22. package/skills/developing-cpp/references/comments/examples/declaration-member.md +3 -3
  23. package/skills/developing-cpp/references/comments/examples/declaration-predicate.md +1 -1
  24. package/skills/developing-cpp/references/comments/examples/declaration-struct.md +3 -3
  25. package/skills/developing-cpp/references/comments/examples/implementation-body.md +34 -17
  26. package/skills/developing-cpp/references/comments/glossary/comment-terms.md +30 -31
  27. package/skills/developing-cpp/references/design/examples/coordinate-table.md +3 -3
  28. package/skills/developing-cpp/references/design/examples/header-place.md +1 -1
  29. package/skills/developing-cpp/references/design/examples/move-result.md +31 -22
  30. package/skills/developing-cpp/references/design/examples/preview-read.md +6 -10
  31. package/skills/developing-cpp/references/design/project-design.md +17 -14
  32. package/skills/developing-cpp/references/editing/01-edit-operations.md +1 -1
  33. package/skills/developing-cpp/references/exceptions/examples/throw-build-mode.md +4 -4
  34. package/skills/developing-cpp/references/exceptions/examples/throw-message.md +8 -0
  35. package/skills/developing-cpp/references/exceptions/throw-rules.md +62 -59
  36. package/skills/developing-cpp/references/language/language-basics/01-objects.md +1 -1
  37. package/skills/developing-cpp/references/language/language-basics/02-initialization.md +1 -1
  38. package/skills/developing-cpp/references/language/language-basics/04-const.md +4 -4
  39. package/skills/developing-cpp/references/language/language-basics/05-expressions.md +8 -3
  40. package/skills/developing-cpp/references/language/language-basics/06-functions.md +2 -0
  41. package/skills/developing-cpp/references/language/standard-library/02-access.md +1 -1
  42. package/skills/developing-cpp/references/language/standard-library/06-format.md +13 -5
  43. package/skills/developing-cpp/references/language/value-types/04-enum.md +1 -1
  44. package/skills/developing-cpp/references/language/value-types/07-nodiscard.md +1 -1
  45. package/skills/developing-cpp/references/language/value-types/08-comparison.md +1 -1
  46. package/skills/developing-cpp/references/layout/01-source-layout.md +2 -3
  47. package/skills/developing-cpp/references/layout/02-file-template.md +7 -7
  48. package/skills/developing-cpp/references/layout/03-declaration-order.md +12 -9
  49. package/skills/developing-cpp/references/naming/examples/naming-accessor.md +11 -0
  50. package/skills/developing-cpp/references/naming/examples/naming-local.md +45 -0
  51. package/skills/developing-cpp/references/naming/examples/naming-lookup.md +1 -1
  52. package/skills/developing-cpp/references/naming/examples/naming-lower-layer.md +20 -13
  53. package/skills/developing-cpp/references/naming/examples/naming-player-value.md +4 -5
  54. package/skills/developing-cpp/references/naming/examples/naming-predicate.md +1 -1
  55. package/skills/developing-cpp/references/naming/examples/naming-role.md +1 -1
  56. package/skills/developing-cpp/references/naming/examples/naming-state-change.md +38 -25
  57. package/skills/developing-cpp/references/naming/glossary/naming-terms.md +23 -0
  58. package/skills/developing-cpp/references/naming/glossary/position.md +23 -0
  59. package/skills/developing-cpp/references/naming/glossary/square.md +47 -0
  60. package/skills/developing-cpp/references/naming/identifier-names.md +24 -19
  61. package/skills/developing-cpp/references/style/google-cpp-style.md +4 -4
  62. package/skills/developing-cpp/scripts/checking-comments.mjs +9 -1
  63. package/skills/developing-cpp/scripts/checking-declaration-order.mjs +27 -1
  64. package/skills/developing-cpp/steps/03-declaration.md +4 -4
  65. package/skills/developing-cpp/steps/04-implementation.md +8 -8
  66. package/skills/engineering/SKILL.md +7 -7
  67. package/skills/engineering/references/cmake/linking/03-undefined-reference.md +1 -1
  68. package/skills/engineering/references/cmake/modules/functions.md +1 -1
  69. package/skills/engineering/references/cmake/{standard/standards → standards}/README.md +10 -10
  70. package/skills/engineering/references/cmake/{standard/standards → standards}/c++23/README.md +3 -3
  71. package/skills/engineering/references/cmake/{standard → standards}/cxx-standard.md +3 -3
  72. package/skills/engineering/references/cmake/targets/examples/target.md +1 -1
  73. package/skills/engineering/references/cmake/targets/rules.md +1 -1
  74. package/skills/engineering/references/cmake/testing/ctest.md +1 -1
  75. package/skills/engineering/references/formatting/02-clang-format.md +1 -1
  76. package/skills/engineering/references/formatting/scripts/checking-encoding.mjs +6 -7
  77. package/skills/engineering/steps/01-targets.md +7 -6
  78. package/skills/engineering/steps/03-check.md +3 -2
  79. /package/skills/engineering/references/cmake/{standard/standards → standards}/c++11/README.md +0 -0
  80. /package/skills/engineering/references/cmake/{standard/standards → standards}/c++11/cmake-snippet.cmake +0 -0
  81. /package/skills/engineering/references/cmake/{standard/standards → standards}/c++14/README.md +0 -0
  82. /package/skills/engineering/references/cmake/{standard/standards → standards}/c++14/cmake-snippet.cmake +0 -0
  83. /package/skills/engineering/references/cmake/{standard/standards → standards}/c++17/README.md +0 -0
  84. /package/skills/engineering/references/cmake/{standard/standards → standards}/c++17/cmake-snippet.cmake +0 -0
  85. /package/skills/engineering/references/cmake/{standard/standards → standards}/c++20/README.md +0 -0
  86. /package/skills/engineering/references/cmake/{standard/standards → standards}/c++20/cmake-snippet.cmake +0 -0
  87. /package/skills/engineering/references/cmake/{standard/standards → standards}/c++23/cmake-snippet.cmake +0 -0
  88. /package/skills/engineering/references/cmake/{standard → standards}/examples/standard.md +0 -0
@@ -13,4 +13,12 @@ throw std::out_of_range{std::format("GetTask: 下标 {} 越界", index)};
13
13
  throw std::out_of_range{std::format("GetTask: index {} is out of range", index)};
14
14
  ```
15
15
 
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
+
16
24
  消息写操作和关键值;英文消息小写开头、句末不加句号,中文消息按中文标点,句式不变;实际值用 `{}` 直接放进句子,不要写成 `(index={})`。`std::format` 只是构造消息的方式,句式见 `references/exceptions/throw-rules.md` 的「消息」节。项目若定义 `Error` 基类,按该文件先定基类再派生,调用方捕获基类接住整族。
@@ -17,7 +17,7 @@ E.1 要求尽早决定错误处理策略:先问调用方还能不能继续、
17
17
 
18
18
  策略一旦定下,声明、实现、测试和入口都用同一套;不要在同一层里一半返回错误码、一半抛异常。
19
19
 
20
- 判断是否算「混用」,看函数合同而不是看返回类型:查询必须产出一个值、失败时产不出(如 `GetSquare` 坐标越界),就抛;写入操作把「这次不写」当作正常结果、调用方本来就要分支(如 `Board::Place` 返回 `Square::PlaceResult`,`Square::Place` 返回 `kOccupied`),就用返回值。动作可能因为多个前置条件失败、而调用方需要区分原因时,返回 `enum class` 而不是 `bool` 加输出参数。两者并存时,公共头必须把「拒绝」与「抛」的边界各写一行;没有这行注释才算混用。
20
+ 判断是否算「混用」,看函数合同而不是看返回类型:查询必须产出一个值、失败时产不出(如 `GetSquare` 坐标越界),就抛;写入操作把「这一手写不进」当作正常结果、调用方本来就要分支(如 `Game::Place` 返回 `PlaceResult`,玩家、终局、空值与占用各有一个取值),就用返回值。动作可能因为多个前置条件失败、而调用方需要区分原因时,返回 `enum class` 而不是 `bool` 加输出参数。两者并存时,公共头必须把「拒绝」与「抛」的边界各写一行;没有这行注释才算混用。
21
21
 
22
22
  ## 什么时候抛
23
23
 
@@ -27,68 +27,73 @@ E.2:函数不能完成它被分配的任务时,就抛异常;构造函数
27
27
 
28
28
  ## 检查次序
29
29
 
30
- 同一个函数既有前置条件、又有正常结果时,先处理本层负责的前置条件,再返回正常结果,最后把能交给下层的工作交给下层。次序按「这一层是否拥有这个判断」排:
30
+ 同一个函数既有前置条件、又有正常结果时,先判断这次调用还能不能进行,再校验调用方送来的参数,最后把能交给下层的工作交给下层。次序按「哪条判断决定这一步能否进行」排:
31
31
 
32
- 1. 本层状态的前置条件:这一步是否轮到该玩家、对象是否已初始化。前置条件不成立是调用方的程序错误,抛 `std::logic_error`。
33
- 2. 正常业务结果:对局已结束、目标格已占用。它们是函数要返回的结果,不是异常。
34
- 3. 下层自己的检查:坐标范围、格子是否为空由 `Board::Place` 判断;不要为了提前抛出而在 `Game` 里重复 `Board::IsInRange`。
35
- 4. 执行操作并返回结果。
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`:换方、写入棋盘、记录历史,三步按成员声明顺序做;构造函数预留了整盘手数的容量,写入阶段不分配内存。
36
37
 
37
- 本层负责的抛出检查必须排在「返回正常失败」之前。否则终局会先返回 `kDenied`,把「不是当前玩家」一起吞掉,调用方拿不到真正的调用错误。属于下层的检查不在此列:对局已结束时 `Game` 不再进入棋盘,越界坐标随 `kDenied` 一起返回,这是正常的短路,不是遗漏。
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
- Game::PlaceResult Game::Place(Move move) {
43
- if (current_player_ != move.player) {
44
- throw std::logic_error{std::format(
45
- "Game::Place: current player {} cannot accept move {}", current_player_, move)};
45
+ PlaceResult Game::Place(Move move) {
46
+ // 检查对局是否仍在进行。
47
+ if (GetResult() != GameResult::kInProgress) {
48
+ return PlaceResult::kFinishedGame;
46
49
  }
47
- if (board_.GetStatus() != BoardStatus::kInProgress) {
48
- return PlaceResult::kDenied;
50
+
51
+ // 检查落子玩家是否为当前玩家。
52
+ if (current_player_ != move.player) {
53
+ return PlaceResult::kInvalidPlayer;
49
54
  }
50
- // 坐标范围由 Board 抛出;占用与空值由 Board::Place 返回。
51
- const Square::PlaceResult result =
52
- board_.Place(move.position, PlayerToSquareValue(move.player));
53
- if (result == Square::PlaceResult::kOccupied) {
54
- return PlaceResult::kRejected;
55
+
56
+ // 检查要写入的值是否为空。
57
+ const SquareValue target_square_value = PlayerToSquareValue(move.player);
58
+ if (target_square_value == SquareValue::kEmpty) {
59
+ return PlaceResult::kEmptySquare;
55
60
  }
56
- if (result == Square::PlaceResult::kIgnored) {
57
- throw std::logic_error{"Game::Place: board rejected an empty value"};
61
+
62
+ // 检查目标方格是否为空。
63
+ const Square target_square = board_.GetSquare(move.position);
64
+ if (!target_square.IsEmpty()) {
65
+ return PlaceResult::kOccupiedSquare;
58
66
  }
59
- // 记录这一步并轮到对方。
67
+
68
+ current_player_ = PlayerToOpponent(current_player_);
69
+ board_.SetSquare(move.position, target_square_value);
70
+ history_.push_back(move);
71
+ return PlaceResult::kPlaced;
60
72
  }
61
73
  ```
62
74
 
63
75
  ### 重复的入口检查
64
76
 
65
- 同一层有多个入口重复同一个 `if (!IsInRange(...)) throw ...` 时,把检查收进私有函数。函数名由调用方传入,`std::source_location` 只在默认参数里取调用点:
77
+ 同一层有多个入口重复同一个 `if (!IsInRange(...)) throw ...` 时,把检查收进一个函数。`std::source_location` 放在默认参数里取调用点,函数名也由它提供:
66
78
 
67
79
  ```cpp
68
- void Board::CheckInRange(Position pos, std::string_view function,
69
- std::source_location location) {
70
- if (IsInRange(pos)) {
71
- return;
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);
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};
79
88
  }
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
89
  }
85
90
  ```
86
91
 
87
- 声明写成 `static void CheckInRange(Position pos, std::string_view function, std::source_location location = std::source_location::current());`。`source_location::current()` 作为默认参数在调用点求值,所以不把 `source_location` 加进公共接口;标签仍由调用点显式传入,如 `CheckInRange(pos, "Board::Place")`。
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)`。
88
93
 
89
- `source_location` 是 C++20 设施,GCC、Clang 与 MSVC 都提供;差异在 `file_name()` 与 `function_name()` 的内容。`function_name()` 可能给出完整签名或限定名,所以消息标签仍显式传入;`file_name()` 可能是相对或绝对路径,先取 basename。Clang 16 起 `line()` 才稳定指向调用点,MSVC 18.4 起才修复默认参数里直接调用 `.function_name()` 返回空串的问题;传整个 `source_location` 对象可避开后者。这样三个平台都输出 `board.cpp:行号`,测试只断言文件名与函数名,不断言具体行号。
94
+ `source_location` 是 C++20 设施;`line()` 与 `column()` 从 1 起算。测试只断言值、范围、文件名与消息里出现的函数名,源码移动后测试不会失效。
90
95
 
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 表达。这里只把 `Game` 自己拥有的状态前置条件写成异常;`Player` 的取值由适配层保证,坐标范围由 `Board` 拥有,重复检查会让同一规则出现两份,消息与异常类型还可能分叉。参数非法时返回一个业务结果,会让调用方以为「只是不能下这里」,而不是「这手棋本身不成立」。
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` 的取值由适配层保证;坐标越界属于调用方的程序错误,不是对局里的正常分支,所以仍抛出。
92
97
 
93
98
  ## 异常类型
94
99
 
@@ -110,7 +115,7 @@ class ParseError : public std::runtime_error {
110
115
 
111
116
  - 返回值:两种模式完全一致。函数用返回值报告可恢复失败时,Debug 不得改成抛异常,Release 也不得把异常改成错误码。
112
117
  - 标准异常:两种模式的抛出条件相同。越界抛 `std::out_of_range`,前置条件被破坏抛 `std::logic_error`;项目若定义 `Error` 基类,派生类型同时派生自 `std::exception`,Debug 的 `what()` 可带 `file:line`,Release 只留值。
113
- - `Check`:项目保留这种断言时,两种模式都检查,失败时打印诊断并终止;用于 invariant 已被破坏、继续执行没有意义的场合。没有 `Check` 的项目直接用 `assert` 或 `std::terminate` 表达同一边界,不另写包装。
118
+ - `Check`:项目保留这种断言时,两种模式都检查,失败时打印诊断并终止;用于 invariant 已被破坏、继续执行没有意义的场合。没有 `Check` 的项目:两种模式都必须执行的检查用 `std::terminate`(失败前先打印诊断),只在开发期做的自检用 `assert`,不另写包装。
114
119
  - `DebugCheck`:项目保留时只在 Debug 生效,`NDEBUG` 后展开为空;用于代价高、只在开发期值得做的自检。不得用它承担 Release 也必须执行的检查。
115
120
 
116
121
  `Check` 不是 `[[noreturn]]`:条件成立时正常返回。`DebugCheck` 的参数在 Release 不求值,不能放入有副作用的表达式。
@@ -148,7 +153,7 @@ int main() {
148
153
 
149
154
  ## 消息
150
155
 
151
- `what()` 是给程序员看的诊断文本,句式是「标签: 完整句」。标签写明来源,句子主语是失败的值或对象,谓语说明哪里不对,实际值直接插进句子,限度用 `for` 接在句末。标签是标识符,成员写 `Type::Function`,自由函数只写函数名,冒号后一个空格;默认参数用 `std::source_location::current()` 在调用点求值时,标签写调用方显式传入的 `Type::Function`,不解析 `function_name()` 的完整签名。先写哪个值失败,再写失败条件,最后写限度;语言由项目约定,下面的示例用英文。句子要完整:主语和谓语都在,不写 `board position is out of range` 这种重复标签类型的主语,也不把值塞进括号,不写 Error / Failed 或空泛的「失败」。实际值用 `{}` 直接放进句子,不再写 `(name=value)`;没有实际值就不写值,没有限度就不写 `for`。需要原因时用 `because`,不要用分号堆叠。
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`,不要用分号堆叠。
152
157
 
153
158
  英文消息只用 `:`、`()`、`,`、`=` 四个 ASCII 符号:冒号紧贴标签、后接一个空格;括号只放次要值或补充说明;逗号前不空、后一个空格;等号两侧不空。不用第二个冒号、句号、问号、感叹号、破折号或 `×`;句子小写开头,句末不加句号;尺寸写 `3x3`,`out of range` 不加连字符。中文消息按中文标点,但句式与「值在前、限度在后」的顺序不变。
154
159
 
@@ -158,33 +163,31 @@ int main() {
158
163
 
159
164
  `what()` 不是分支接口;调用方要数字就放进类型成员,不解析字符串。入口可以在消息前加面向用户的前缀,库代码不打印日志。
160
165
 
161
- 写进 `throw` 时,直接把 `std::format(...)` 放在异常类型后面,不先建消息变量:
166
+ 写进 `throw` 时,短消息直接内联;整条 `throw` 一行写不下、需要折行时,先把消息存进 `const std::string message`,再 `throw ...{message};`。公共函数的消息带标签:
162
167
 
163
168
  ```cpp
164
- throw std::out_of_range{std::format(
165
- "Board::GetSquare: position {} is out of range for board {}x{}", pos, kRowCount,
166
- kColumnCount)};
169
+ const std::string message = std::format("ParseConfig: line {} has no key", line_number);
170
+ throw ParseError{message};
167
171
  ```
168
172
 
169
- 跨行时只把格式串与参数续到下一行:`throw`、异常类型与 `std::format(` 留在同一行,读者先看到抛什么,再看到消息。消息没有插值时写一行 `throw std::logic_error{"ChooseSquare: game is already over"};`。
173
+ 跨行时先把消息存进 `const std::string message`,再写一行 `throw`:`throw` 行只表达抛什么,格式串与参数留在上一句,读者不必先跨过消息才能看到异常类型的结束。消息没有插值时仍写一行 `throw std::logic_error{"ChooseSquare: game is already over"};`。私有检查函数由 `source_location` 取调用点,不带标签,句式见上文「重复的入口检查」。
170
174
 
171
- 常用库把消息的构造放进异常类型或辅助函数:spdlog 的 `spdlog_ex` 在构造函数里写 `_msg`,CLI11 的 `Error` 族在静态工厂里拼好句子,POCO 的 `Exception` 在构造函数里存消息;调用处因此只有一行 `throw ...(...)`。本工作流不额外引入消息变量,也不为消息的 `const` 与 `std::move` 单独做决定。
175
+ 常用库把消息的构造放进异常类型或辅助函数:spdlog 的 `spdlog_ex` 在构造函数里写 `msg_`,CLI11 的 `Error` 族在静态工厂里拼好句子,POCO 的 `Exception` 在构造函数里存消息;调用处因此只有一行 `throw ...(...)`。本工作流在整条 `throw` 跨行时引入 `const std::string message`(需要 `#include <string>`),变量名固定,不写 `std::move`;异常类型构造时复制消息。
172
176
 
173
- Rust 把失败分成两类:`Result` 表示可预期的失败,用 `?` 传播;`panic!` 只表示程序里的 bug(invariant 已被破坏),消息同样在调用点构造,长消息由格式化器折行。C++ 的异常同时承担这两种职责,本工作流因此把「调用方无法继续」写成异常,把「可恢复的拒绝」写成返回值。
177
+ Rust 把失败分成两类:`Result` 表示可预期的失败,用 `?` 传播;`panic!` 只表示程序里的 bug(invariant 已被破坏),消息同样在调用点构造;跨行时先在调用点拼好再抛出。C++ 的异常同时承担这两种职责,本工作流因此把「调用方无法继续」写成异常,把「可恢复的拒绝」写成返回值。
174
178
 
175
- ```cpp
176
- throw std::out_of_range{std::format(
177
- "Board::GetSquare: position {} is out of range for board {}x{}", pos, kRowCount,
178
- kColumnCount)};
179
+ 这两条是本工作流的选择,不是普遍更优:Google C++ Style Guide 整体禁用异常,只保留「用户输入不应引发异常」这一条共识;Core Guidelines E.27 的说法是「不能抛异常时改用错误码系统」。C++23 项目也可以把可预期的失败写成 `std::expected<T, E>`,或用带字段的 struct 一次返回多个值;out 参数在本工作流里一律不用。
179
180
 
180
- throw std::logic_error{std::format(
181
- "Game::Place: board rejected move {} because the cell state is empty", move)};
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};
182
185
 
183
186
  throw std::logic_error{"ChooseSquare: game is already over"};
184
- throw std::logic_error{"ChooseSquare: there is no empty square left to place"};
187
+ throw std::format_error{"Position: format specifier is not supported, use {} instead"};
185
188
  ```
186
189
 
187
- 反例:`Board::GetSquare: board position is out of range (position={}); board is {}x{}`。标签已经说明是 `Board::GetSquare`,句子又用 `board position` 当主语,实际值还放在括号里;读者要在标签、主语和括号之间来回拼。
190
+ 反例:`Game::Place: the move is not acceptable (player={}); the game is in progress`。标签已经说明是 `Game::Place`,句子又用 `the move` 当空泛主语,实际值还放在括号里;读者要在标签、主语和括号之间来回拼。
188
191
 
189
192
  ## 声明注释
190
193
 
@@ -202,11 +205,11 @@ Config ReadConfig(const std::filesystem::path& path);
202
205
  - [ ] 错误策略在接口设计时已确定,同一层没有混用错误码和异常。
203
206
  - [ ] 构造函数失败时抛异常,没有留下半初始化对象。
204
207
  - [ ] 异常类型有语义,没有抛内建类型、字符串或裸指针。
205
- - [ ] `throw` 直接接收 `std::format(...)`;跨行时只把格式串与参数续行,不为消息单独建变量。
208
+ - [ ] 一行写得下的 `throw` 直接接收 `std::format(...)` 或字面量;跨行时先建 `const std::string message`,再 `throw` 变量。
206
209
  - [ ] 按值抛出、按 `const` 引用捕获;重抛用 `throw;`。
207
210
  - [ ] 析构、`swap`、异常类型的拷贝/移动不抛。
208
211
  - [ ] 没有用异常做正常分支,也没有在每个函数里捕获所有异常。
209
- - [ ] 本层负责的参数校验与状态检查排在正常结果返回之前;非法参数没有被 `kDenied` 之类的业务结果掩盖。
212
+ - [ ] 本层负责的参数校验与状态检查排在正常结果返回之前;非法参数没有被 `kXWin`、`kOWin` 或 `kDraw` 之类的终局结果掩盖。
210
213
  - [ ] 入口有统一捕获,消息可定位,退出码符合项目合同。
211
214
  - [ ] 公共声明的 `@throws` 与实际抛出类型一致,且 Debug/Release 的抛出条件相同。
212
215
  - [ ] 每个 `noexcept` 函数的函数体不调用 `at()` 或其他会抛的接口。
@@ -6,7 +6,7 @@
6
6
  - 变量有名字、类型、存储期和作用域。局部变量在离开作用域时销毁;`static` 局部变量在首次执行到声明时初始化,程序结束时销毁。
7
7
  - 声明让名字可见,定义分配存储或给出函数体。非 `inline` 的变量与函数一个程序只能定义一次;`inline` 函数/变量与模板可以出现在多个翻译单元,但定义必须一致。头文件只放声明,定义放 `.cpp`,例外是 `inline` 与模板。
8
8
  - 引用是已有对象的别名,必须初始化,不能重新绑定;指针可以为空,也可以改指向。接口优先用引用表达“必须存在”,用指针表达“可以不存在”。
9
- - `*this` 不用于复制当前对象:要一份副本就直说语义(如“推演落子”只读原数据,或显式写一个复制函数)。`return *this` 仍只属于赋值运算符,井字棋没有这种运算符。
9
+ - `*this` 不用于复制当前对象:要一份副本就直说语义(如“推演落子”只读原数据,或显式写一个复制函数)。`return *this` 仍只属于赋值运算符,值类型没有这种运算符。
10
10
 
11
11
  ## 依据
12
12
 
@@ -11,7 +11,7 @@ auto total = 0.0; // 本工作流统一用 = 推导
11
11
 
12
12
  - `kLines` 这类多行聚合常量表写 `name = {{`;单个对象和空初始化仍写 `count{}`,不写 `=`;不把其他花括号初始化改成 `=`。
13
13
  - `T value{}` 做值初始化:内置类型得 0,类类型调用默认构造函数。
14
- - `auto x{1}` 在 C++17 起合法,但 `{}` 与 `initializer_list` 的重载容易误读;本工作流统一写 `auto x = 1`。
14
+ - `auto x{1}` 的推导类型在 C++17 前后不同:C++17 起是 `int`,之前是 `std::initializer_list<int>`;`{}` 与 `initializer_list` 的重载也容易误读,本工作流统一写 `auto x = 1`。
15
15
  - 花括号禁止窄化转换:`int x{3.5}` 编译失败,`int x = 3.5` 静默截断。需要截断时显式 `static_cast<int>(3.5)`。
16
16
  - `std::vector<int> values(3)` 是三个 0;`values{3}` 是一个元素 3。两种括号语义不同,不要凭习惯混用。
17
17
  - 类成员在声明处写默认成员初始化(NSDMI),一律用花括号:标量 `count_{0}`,数组 `items_{}`,容器与类类型成员写空 `{}`(如 `std::vector<int> values_{};`、`Board board_{};`)。
@@ -5,21 +5,21 @@
5
5
  | `const` | 初始化后不能通过这个名字修改 | 局部变量、参数、成员函数尾部 |
6
6
  | `constexpr` | 能在编译期求值(满足条件时) | 常量、函数、构造函数 |
7
7
  | `consteval` | 必须在编译期求值 | 编译期校验函数 |
8
- | `constinit` | 静态存储期变量在编译期完成初始化 | 全局/静态变量 |
8
+ | `constinit` | 静态或线程存储期变量在编译期完成初始化 | 全局、静态与 `thread_local` 变量 |
9
9
  | `inline constexpr` | 头文件里可共享的编译期常量,避免 ODR 问题 | 命名空间作用域常量 |
10
10
 
11
11
  - 共享常量写在头文件的命名空间作用域:`inline constexpr int kMaxRows = 3;`,不要用 `#define`。`.cpp` 里非 `inline`、非 `extern` 的命名空间作用域 `constexpr` 已有内部链接。
12
- - `constexpr` 函数的定义要放在头文件里,否则其他翻译单元无法在编译期调用。
12
+ - `constexpr` 函数在首次声明处隐式 `inline`,用到它的翻译单元都必须看见定义,所以定义放头文件;只写在 `.cpp` 里会让其他翻译单元的调用成为不要求诊断的不合规程序。
13
13
  - 不要给所有函数加 `constexpr`;只有确实需要在编译期使用、且实现满足约束时才加。
14
14
  - `constinit` 只保证初始化时机,不保证不可修改;需要不可修改时同时写 `const`。
15
- - C++20 起 `constexpr` 函数可以包含循环、局部变量和部分动态分配,但不要借此把运行期逻辑塞进编译期。
15
+ - C++14 起 `constexpr` 函数就可以包含循环、局部变量与 `if`/`switch`;C++20 起还允许编译期分配,但分配要在同一次求值内释放。也不要把运行期逻辑塞进编译期。
16
16
  - 局部变量默认不加 `const`:短生命周期的局部值只读一次,`const` 不改变存储与运行行为。把 `const` 留给接口契约——`const` 引用参数、`const` 成员函数、指向只读数据的指针与视图,以及确实要防止后续误写的变量。Core Guidelines 的 Con.1 主张默认不可变;本仓取 Google 风格的做法,只在接口层要求 `const`。
17
17
 
18
18
  ## 依据
19
19
 
20
20
  - [ISO C++ `[dcl.constexpr]`](https://eel.is/c++draft/dcl.constexpr):`constexpr`、`consteval` 的求值时机与函数约束。
21
21
  - [cppreference: `constexpr`](https://en.cppreference.com/w/cpp/language/constexpr):常量表达式、`constinit` 与 `consteval` 的对照。
22
- - [cppreference: `constinit`](https://en.cppreference.com/w/cpp/language/constinit):静态存储期初始化时机,不改变可修改性。
22
+ - [cppreference: `constinit`](https://en.cppreference.com/w/cpp/language/constinit):静态与线程存储期的初始化时机,不改变可修改性。
23
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
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
25
  - [C++ Core Guidelines: Con.1](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#rconst-immutable):主张默认不可变;本仓只在接口处采用这一主张,局部变量按上一条处理。
@@ -1,8 +1,10 @@
1
1
  # 表达式与比较
2
2
 
3
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()`,不写 `throw` 兜底。
5
- - 条件是比较范围、布尔组合,或只有 1–2 个分支,或「失败提前返回、成功落到末尾」的守卫时用 `if`,末尾直接写成功路径。
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 个语义各自独立的值才各起名字。
6
8
  - 不依赖隐式穿透;确实需要穿透时写 `[[fallthrough]]`。
7
9
  - 混合优先级的算术表达式加括号:`(a + b) * c`,不依赖读者记忆优先级。`readability-math-missing-parentheses` 会检查。
8
10
  - 区间用半开区间 `[low, high)`;判空用 `empty()`,不要用 `size() == 0`。
@@ -16,8 +18,11 @@
16
18
  - [cppreference: Expressions](https://en.cppreference.com/w/cpp/language/expressions):运算符分类、短路与序列点规则。
17
19
  - [C++ Core Guidelines: ES.41](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#s-expr):避免容易误读的表达式,必要时加括号。
18
20
  - [Style Guide for Writing Comparisons in Conditions](https://www.enyo.de/fw/notes/style-guide-conditions.html):变化最快、最不确定的一方写在比较左边;常量前置已无必要,且常掩盖本意。
19
- - [clang-tidy `readability-constant-operand-order`](https://github.com/llvm/llvm-project/pull/167158):现代代码库倾向统一写 `x == 0`,不写 `0 == x`。
21
+ - [提议中的 clang-tidy 检查 `readability-constant-operand-order`](https://github.com/llvm/llvm-project/pull/167158):仍为 open 的 PR #167158,尚未合入;其方向是现代代码库统一写 `x == 0`,不写 `0 == x`。
20
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`),不把常量前置。
21
23
  - [C++ Core Guidelines: ES.70、ES.78、ES.79](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#s-expr):有选择时优先 `switch`;对常量比较,通常比 `if`-`then`-`else` 链更容易优化,并能提示枚举是否覆盖全;不依赖隐式穿透,`default` 只处理常见情况。
22
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`)。
23
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 个以上同类值用数组加下标:没有一手规范规定这一点,属于本工作流的项目约定;理由是同一段循环只写一次,新增一项不必再起一个名字。
@@ -7,9 +7,11 @@
7
7
  - 单参数构造函数加 `explicit`,除非确实需要隐式转换;转换运算符也加 `explicit`。
8
8
  - 不在虚函数上写默认参数;默认参数在静态类型处绑定,派生类改默认值会造成意外。
9
9
  - 函数只做一件事;超过一屏时先找可命名的子步骤,分阶段与注释写法见 `steps/04-implementation.md`。
10
+ - lambda 捕获按需列名,不默认写 `[this]`:只在确实要读写成员时才捕获 `this`,只读个别值时按名捕获,纯计算写 `[]`。需要成员访问时优先把要用的值取到局部变量再捕获;捕获 `this` 的 lambda 不得比对象活得更久,异步或存储回调前先复制需要的值。
10
11
 
11
12
  ## 依据
12
13
 
13
14
  - [ISO C++ `[dcl.fct]`](https://eel.is/c++draft/dcl.fct):函数声明、参数与返回类型。
14
15
  - [cppreference: Functions](https://en.cppreference.com/w/cpp/language/functions):重载、默认参数与返回类型推导。
15
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 不能比对象活得更久。
@@ -1,7 +1,7 @@
1
1
  # 访问与边界
2
2
 
3
3
  - `operator[]` 不检查边界,越界是未定义行为;`at` 检查并抛 `std::out_of_range`。规则与家族差异见 `references/language/value-types/09-subscript.md`。
4
- - `vector::front()` / `back()` / `data()` 在空容器上未定义;先 `empty()`。
4
+ - `vector::front()` / `back()` 在空容器上未定义;`data()` 在空容器上返回的指针不能解引用。先 `empty()`。
5
5
  - `map::operator[]` 缺键会插入默认值;只读查询用 `find`、`contains`(C++20)或 `at`。
6
6
  - `string_view` 不保证以 `\0` 结尾;传给需要 C 字符串的 API 前先转成 `std::string`。
7
7
  - `span` 不拥有元素,也不延长生命周期;不要从临时容器构造后保存。
@@ -33,7 +33,7 @@
33
33
 
34
34
  ```cpp
35
35
  template <>
36
- struct std::formatter<tictactoe::Player> {
36
+ struct std::formatter<game::Player> {
37
37
  constexpr auto parse(std::format_parse_context& ctx) {
38
38
  if (ctx.begin() != ctx.end() && *ctx.begin() != '}') {
39
39
  throw std::format_error("Player: format specifier is not supported, use {} instead");
@@ -42,11 +42,11 @@ struct std::formatter<tictactoe::Player> {
42
42
  }
43
43
 
44
44
  template <typename FormatContext>
45
- auto format(tictactoe::Player player, FormatContext& ctx) const {
45
+ auto format(game::Player player, FormatContext& ctx) const {
46
46
  switch (player) {
47
- case tictactoe::Player::kX:
47
+ case game::Player::kX:
48
48
  return std::format_to(ctx.out(), "X");
49
- case tictactoe::Player::kO:
49
+ case game::Player::kO:
50
50
  return std::format_to(ctx.out(), "O");
51
51
  }
52
52
  return std::format_to(ctx.out(), "Player({})", static_cast<int>(player));
@@ -80,7 +80,7 @@ return std::format_to(ctx.out(), "Player({:d})", std::to_underlying(player));
80
80
 
81
81
  // C++20
82
82
  return std::format_to(ctx.out(), "Player({:d})",
83
- static_cast<std::underlying_type_t<tictactoe::Player>>(player));
83
+ static_cast<std::underlying_type_t<game::Player>>(player));
84
84
  ```
85
85
 
86
86
  ## 标准、提案与生态
@@ -94,6 +94,14 @@ return std::format_to(ctx.out(), "Player({:d})",
94
94
  - LLVM `formatv`:自定义用 `format_provider<T>` 特化;文档没有给出枚举专用入口。
95
95
  - 结论:C++23 项目继续用 `std::formatter` 特化;数值回退优先 `static_cast<int>`(值域小)或 `std::to_underlying` + `{:d}`(值域大或底层不定);不要为枚举继承 `formatter<std::string_view>`。
96
96
 
97
+ ## fmt 与 `std::format` 的关系
98
+
99
+ - `std::format` 的接口来自 fmt 作者主导的 P0645,格式串语法与格式化语义同源;读 fmt 文档能直接解释标准库的行为。
100
+ - fmt 是超集:C++23 之前就提供 `print`/`println`、`memory_buffer`、命名参数与运行期格式串;标准库到 C++23 才补上 `std::print`,其余仍没有。
101
+ - 本项目是 C++23,clang 21 与 libc++ 21 的 `std::format` 已可用,不引入第三方依赖;fmt 只作为参考实现出现在阅读清单里。
102
+ - 只有工具链停在 C++20 及更早、或确实要用 fmt 专有接口时才换成 `fmt::format`。
103
+ - 换 fmt 要同时改构建(`target_link_libraries` 或 FetchContent)与编译数据库;只放头文件不接构建,clangd 仍会报未声明。
104
+
97
105
  ## 是否继承 `std::formatter<std::string_view>`
98
106
 
99
107
  - 不需要。启用的特化只要满足 BasicFormatter:本身可默认构造、拷贝、赋值、析构、交换,并提供 `parse` 与 `format`;标准不要求继承任何 formatter。
@@ -13,7 +13,7 @@
13
13
  - 需要固定布局、序列化或前向声明时写底层类型;本项目的封闭状态码默认写 `: std::uint8_t`,普通枚举用默认底层类型。改动枚举值或底层类型要同步序列化、测试与注释。
14
14
  - 枚举值的行尾注释要么全写要么全不写;两种句式与禁止项见 `references/comments/comment-rules.md`「枚举项与 `struct` 短字段:两种句式」。
15
15
  - 需要“编号”与“名字”两套表示时,写显式映射函数,例如 `TaskStateToName` / `TaskStateFromName`,不要在枚举和整数之间来回隐式转换。
16
- - 编号按“正常优先”排:0 表示正常态——空、进行中或成功;失败与异常态随后,按检查顺序排列。带缺省语义的枚举(`kEmpty`、`kInProgress`)和返回结果的枚举(`kSuccess`、`kRejected`)因此共用同一条约定。身份枚举按领域编号,如 `Player::kX = 1`、`Player::kO = 2`,不套用 0 = 正常态。
16
+ - 编号按“正常优先”排:0 表示正常态——空、进行中或成功;失败与异常态随后,按固定顺序排列。带缺省语义的枚举(`kEmpty`、`kInProgress`)和返回结果的枚举(`kPlaced`、`kOccupiedSquare`)因此共用同一条约定。身份枚举按领域编号,如 `Player::kX = 1`、`Player::kO = 2`,不套用 0 = 正常态。
17
17
  - 编号只用于输出和既有接口;要从编号找回名字,仍走显式映射函数,不拿编号做算术。
18
18
 
19
19
  ## 标准与推荐
@@ -12,4 +12,4 @@
12
12
 
13
13
  - ISO C++ `[dcl.attr.nodiscard]`:`[[nodiscard]]` 的位置与可应用的对象。https://eel.is/c++draft/dcl.attr.nodiscard
14
14
  - cppreference `[[nodiscard]]`:函数、类与枚举上的用法。https://en.cppreference.com/w/cpp/language/attributes/nodiscard
15
- - 本项目的 Clang 验证(C++23):类内 `[[nodiscard]] friend`、`friend [[nodiscard]]`、参数列表后的写法都会被拒绝;只有返回类型前的普通函数声明可加。不要把“能编译”当成标准保证。
15
+ - 本项目的 Clang 验证(C++23):类内 `[[nodiscard]] friend`、`friend [[nodiscard]]`、参数列表后的写法都会被拒绝;类内只有返回类型前的普通函数声明可加。类声明本身在 C++17 起可标 `[[nodiscard]]`,构造函数在 C++20 起可标。不要把“能编译”当成标准保证。
@@ -7,4 +7,4 @@
7
7
 
8
8
  - ISO C++ `[class.compare.default]`:默认 `operator==` 与 `operator<=>` 的逐成员语义。https://eel.is/c++draft/class.compare.default
9
9
  - ISO C++ `[expr.eq]`:相等比较的类型与结果规则。https://eel.is/c++draft/expr.eq
10
- - 需要排序或三路比较时写 `auto operator<=>(const T&) const = default;`,不要手写四个关系运算符;`<=>` 不自动提供 `==`,需要同时默认 `==`。只给真正会比较的值类型提供比较,实体类不因成员可比较就获得比较运算符。https://en.cppreference.com/w/cpp/language/operator_comparison
10
+ - 需要排序或三路比较时写 `auto operator<=>(const T&) const = default;`,不要手写四个关系运算符;默认的 `<=>` 会同时隐式声明默认的 `==`(`!=` 再由 `==` 改写),只有自己手写 `<=>` 时才要单独提供 `==`。默认 `<=>` 的推导类型 `std::strong_ordering` 来自 `<compare>`,使用前先包含它。只给真正会比较的值类型提供比较,实体类不因成员可比较就获得比较运算符。https://en.cppreference.com/w/cpp/language/operator_comparison
@@ -7,7 +7,7 @@
7
7
  ```text
8
8
  <component>/
9
9
  include/
10
- <name>.h 公共接口,从源码根写完整路径包含
10
+ <name>.h 公共接口,包含时写裸文件名
11
11
  src/
12
12
  <name>.cpp 实现,可以包含私有头
13
13
  <detail>.h 私有头,只被本组件 src 包含
@@ -53,7 +53,7 @@ app/
53
53
 
54
54
  ## 头文件契约
55
55
 
56
- 自包含、共享常量、`using namespace`、模板实现与 `<iostream>` 的规则见 `references/style/google-cpp-style.md`「头文件」和 `references/layout/02-file-template.md`。本文只强调一条边界:公共头只暴露稳定契约,测试只包含公共头。
56
+ 自包含、共享常量、`using namespace`、模板实现与包含顺序的规则见 `references/style/google-cpp-style.md`「头文件」;扩展名与槽位见 `references/layout/02-file-template.md`。库不包含 `<iostream>` 的边界见本文「程序」节。本文只强调一条:公共头只暴露稳定契约,测试只包含公共头。
57
57
 
58
58
  ## 测试
59
59
 
@@ -76,4 +76,3 @@ app/
76
76
 
77
77
  - [Google C++ Style Guide: File Names](https://google.github.io/styleguide/cppguide.html#File_Names):文件名全小写,`_` 或 `-` 分隔。
78
78
  - [C++ Core Guidelines: NL.27](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#rl-file-suffix):代码文件用 `.cpp`,接口文件用 `.h`。
79
- - [C++ Core Guidelines: SF.1](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#rs-file-suffix):按文件职责区分实现与接口。
@@ -1,6 +1,6 @@
1
1
  # 文件模板
2
2
 
3
- 本文件是头文件与实现文件槽位的唯一权威。`references/style/google-cpp-style.md` 只保留「路径式守卫、不用 `#pragma once`」的决定;`references/layout/01-source-layout.md` 只定文件放在哪;`steps/03-declaration.md` 指向这里。类内声明顺序仍只写在 `references/layout/03-declaration-order.md`,本文件不复制。
3
+ 本文件是头文件与实现文件槽位的唯一权威。`references/style/google-cpp-style.md` 的头文件节保留文件级规则(守卫选择、自包含、包含顺序),槽位细节以本文件为准;`references/layout/01-source-layout.md` 只定文件放在哪;`steps/03-declaration.md` 指向这里。类内声明顺序仍只写在 `references/layout/03-declaration-order.md`,本文件不复制。
4
4
 
5
5
  ## 选择扩展名
6
6
 
@@ -22,18 +22,18 @@
22
22
 
23
23
  - `PROJECT` 是源码树顶的项目名,全大写;`PATH` 是相对源码树顶的目录;`FILE` 是文件名;非字母数字改成下划线。
24
24
  - 末尾固定 `_H_`;`#endif` 后两个空格,再写 `//` 和同一个宏。
25
- - 井字棋源码树顶是 `games/tictactoe/`:
25
+ - 源码树顶是 `my_project/`:
26
26
 
27
27
  | 文件 | 守卫 |
28
28
  | --- | --- |
29
- | `tictactoe/include/board.h` | `TICTACTOE_INCLUDE_BOARD_H_` |
30
- | `tictactoe/include/game.h` | `TICTACTOE_INCLUDE_GAME_H_` |
31
- | `tictactoe/include/computer.h` | `TICTACTOE_INCLUDE_COMPUTER_H_` |
29
+ | `my_project/include/board.h` | `MY_PROJECT_INCLUDE_BOARD_H_` |
30
+ | `my_project/include/game.h` | `MY_PROJECT_INCLUDE_GAME_H_` |
31
+ | `my_project/include/computer.h` | `MY_PROJECT_INCLUDE_COMPUTER_H_` |
32
32
  | `apps/include/cli.h` | `APPS_INCLUDE_CLI_H_` |
33
- | `tictactoe/tests/computer_test_support.h` | `TICTACTOE_TESTS_COMPUTER_TEST_SUPPORT_H_` |
33
+ | `my_project/tests/computer_test_support.h` | `MY_PROJECT_TESTS_COMPUTER_TEST_SUPPORT_H_` |
34
34
 
35
35
  - 通用示例:`taskflow/include/task_table.h` → `TASKFLOW_INCLUDE_TASK_TABLE_H_`。
36
- - 不用 `TICTACTOE_BOARD_H` 这类省略路径的名字;两个同名头会撞宏。
36
+ - 不用 `MY_PROJECT_BOARD_H` 这类省略路径的名字;两个同名头会撞宏。
37
37
 
38
38
  ## `.h` 与 `.hpp`
39
39
 
@@ -30,22 +30,23 @@ class TaskTable {
30
30
  // 6. 数据成员
31
31
  private:
32
32
  Tasks tasks_{};
33
- bool is_open_ = false;
33
+ bool is_open_{false};
34
34
  };
35
35
  ```
36
36
 
37
- - `struct` 的数据成员放最前;有 invariant 时改用 `class`。
37
+ - `struct` 的数据成员紧跟类型之后;有 invariant 时改用 `class`。
38
38
  - `public:` 在前,`private:` 在后;`private:` 前空一行,数据成员之间不空行。
39
39
  - 静态常量用 `static constexpr`;跨文件共享的常量放命名空间并 `inline constexpr`。
40
40
  - 工厂放在构造函数之前;返回 `TaskTable` 的工厂用 `Create` 开头,名字说明创建的是什么状态。
41
41
  - 用户声明的特殊成员函数集中放第 4 组;`= default` 也写出来,读者才知道是故意默认。
42
- - 不要把数据成员插在函数之间;数据成员永远在最后一组。
42
+ - 不要把数据成员插在函数之间;数据成员放在最后一组,`struct` 是唯一例外(紧跟类型,见上)。
43
43
 
44
44
  ### 数据成员之间
45
45
 
46
46
  - 数据成员按初始化依赖、逻辑分组和阅读顺序排列,不按字母序。初始化依赖是硬约束:先声明的成员先初始化,初始化器只能安全使用已经完成初始化的成员。
47
- - 聚合类型的字段顺序参与聚合初始化与指定初始化器,也是标准布局的一部分;重排要同步所有 `T{a, b}` 调用点,并检查序列化或 ABI 假设。
47
+ - 聚合类型的字段顺序参与聚合初始化与指定初始化器,也决定对象布局与 ABI;它不是 standard-layout 的成立条件。重排要同步所有 `T{a, b}` 调用点,并检查序列化或 ABI 假设。
48
48
  - 先主值、后限定值,同类字段放一起。例如落子记录先放位置、再放玩家,读作「在哪、谁」。
49
+ - 互不依赖的成员按「读的时候先要哪样」排:`Game` 依次是 `current_player_`、`board_`、`history_`,读作「轮到谁、棋盘什么样、走过哪些手」;`board_` 是落子改动的主体,`history_` 是它的变更日志,所以排在最后。
49
50
  - Google 的 Declaration Order 与 C++ Core Guidelines 的 NL.16 只规定访问级别和组间顺序,都没有字母序要求;沿用现有语义顺序即可。
50
51
  - 团队若另定字母序,只作为语义相同时的最后兜底并写进项目规则,不和语义顺序混用。
51
52
 
@@ -54,9 +55,11 @@ class TaskTable {
54
55
  Google、LLVM、OpenCV 都没有规定这一组内部的次序;本工作流补上:
55
56
 
56
57
  - 有比较运算符时,开头是 `operator==`:成员默认比较和 `friend` 都放这里,然后才是谓词 `Is` / `Has` / `Can`、`Get`、普通函数按使用先后。`friend` 不再放到这一组末尾。
58
+ - `CheckInRange` 是诊断函数,紧挨 `IsInRange` 写,排在 `Get` 之前;它是本条谓词顺序的例外,见 `references/exceptions/throw-rules.md`。
57
59
  - 多个直接返回成员的 `Get` 按对应数据成员的声明顺序排列,不按调用频率或字母序:成员依次是 `current_player_`、`board_`、`history_`,getter 就依次是 `GetCurrentPlayer()`、`GetBoard()`、`GetHistory()`;成员顺序调整时 getter 一起改,一个类里只留一套顺序。
58
- - 直接返回成员的 `Get` 按成员顺序;没有独立成员的派生查询排在直接 `Get` 之后,按「先局部元素、后整体状态」阅读,不插进直接 `Get` 之间。例:`Board` 只有 `squares_`,`GetSquare()` 直接取一格,先;`GetStatus()` 扫描全部格子才得到状态,后。
60
+ - 直接返回成员的 `Get` 按成员顺序;没有独立成员的派生查询排在直接 `Get` 之后,按「先局部元素、后整体状态」阅读,不插进直接 `Get` 之间。例:`Game` 的 `GetResult()` 扫描棋盘后才得到结果,排在直接返回成员的 `GetCurrentPlayer()`、`GetBoard()`、`GetHistory()` 之后。
59
61
  - 成对的局部/整体操作沿用同一方向:先 `ClearSquare(pos)` 清一格,再 `Clear()` 清整盘;局部操作在前,整体操作在后,不按调用频率互换。
62
+ - `constexpr` / `consteval` 函数不单独提前:它们属于「其他函数」组,按上面的语义次序排。`constexpr` 与 `consteval` 隐含 `inline`,这才使头文件就地定义可行;它们不决定声明位置;就地定义的短函数也留在该组,不因为写在头文件里就挪到类的最前。
60
63
  - 比较运算符的签名与 `[[nodiscard]]` 规则见 `references/language/value-types/`,这里不另写一套。
61
64
  - 同一类型上的谓词留在类内,不因为实现只转调公开函数就拆成类外自由函数。
62
65
  - `friend` 类型仍跟其他类型放在一起,和 `friend` 函数不是同一组。
@@ -77,11 +80,11 @@ namespace taskflow {
77
80
 
78
81
  inline constexpr int kTaskCount = 16;
79
82
 
80
- enum class TaskState : std::uint8_t { kPending, kActive, kDone };
83
+ enum class TaskState : std::uint8_t { kPending = 0, kActive = 1, kDone = 2 };
81
84
 
82
85
  struct TaskRecord {
83
- int id = 0;
84
- TaskState state = TaskState::kPending;
86
+ int id{0};
87
+ TaskState state{TaskState::kPending};
85
88
  };
86
89
 
87
90
  [[nodiscard]] bool Contains(int id);
@@ -132,7 +135,7 @@ class TaskTable {
132
135
  ## 依据
133
136
 
134
137
  - [Google C++ Style Guide: Declaration Order](https://google.github.io/styleguide/cppguide.html#Declaration_Order):类内先 public、再 protected、最后 private;同节内类型、常量、函数、数据成员的先后。
135
- - [Google C++ Style Guide: Vertical Whitespace](https://google.github.io/styleguide/cppguide.html#Vertical_Whitespace):垂直留白越少越好;函数体首尾不留空行,函数体内不随意加空行;空行主要留给多重 `if-else` 这类分支块。
138
+ - [Google C++ Style Guide: Vertical Whitespace](https://google.github.io/styleguide/cppguide.html#Vertical_Whitespace):垂直留白越少越好;空行用来分隔逻辑分组,函数体首尾不留空行,函数体内不随意加空行。
136
139
  - [C++ Core Guidelines: NL.16](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#rl-order):类型、构造/赋值/析构、函数、数据成员的传统次序。
137
140
  - [cppreference: 构造函数](https://en.cppreference.com/w/cpp/language/constructor):非静态数据成员按声明顺序初始化,与 mem-initializer 列表顺序无关。
138
141
  - [LLVM Coding Standards](https://llvm.org/docs/CodingStandards.html):只要求每个 `.cpp` 先包含定义其接口的头文件,没有规定成员定义的先后;上一条「与头文件声明同序」因此是本工作流约定,不冒充外部标准。
@@ -14,4 +14,15 @@ void set_current_order(Order order);
14
14
  void AdvanceOrder(); // 领域动作,不是 setter
15
15
  ```
16
16
 
17
+ Google 允许访问器与成员同名(`int count()`、`void set_count(int)`),本项目统一 `Get` + PascalCase,示例按本项目规则判为差。
18
+
17
19
  查询统一 `Get` + PascalCase;修改用领域动词。只有真正需要替换值时,才考虑 `Set` 形式,并说明为什么不是动作。
20
+
21
+ 返回集合时给只读视图,接口不绑定内部容器:
22
+
23
+ ```cpp
24
+ /// 获取落子历史。
25
+ ///
26
+ /// @return 只读视图,按落子先后排列,不复制落子记录。
27
+ [[nodiscard]] std::span<const Move> GetHistory() const; // 不写 const std::vector<Move>&
28
+ ```