Files
tsl-devkit/docs/cpp/code_style.md
T
csh ac794a6a70 Squashed 'docs/standards/playbook/' changes from e504a68..b529012
b529012  test(tests): avoid hardcoded missing path
07583c8 🐛 fix(tests): make doc link check awk-portable
6244aa3 🔧 chore(ci): relax gitattributes check
31dd0c5 🐛 fix(scripts): require existing project root
15d4d63 🔧 chore(ci): install python3-pip
90c6313 🔧 chore(ci): run tests in a single job
d84eff0 🔧 chore(ci): make actions base url configurable
395598d 🔧 chore(ci): fix yaml step names
4ae2733  feat(sync_standards): auto-detect existing languages
e97fb00  feat(sync_standards): default gitattributes to append
da0ef2b  test: add automated tests and ci workflow
99bef30 🎨 style(markdown): format all markdown files with prettier
7e96bc8 ♻️ refactor(tsl): split function.md into 44 modular files
0d6b2a0 📝 docs(readme): add quick decision table and TL;DR for distribution methods
3b2188f 🎨 style(markdown): format markdown files with prettier
41fc43b 📝 docs(tsl): document {Unit.}Type source annotation
3dceaf7 📝 docs(tsl): clarify tsf-only top-level rules and type annotations
3a63829 📝 docs(skills): add create-plan skill
f02a707 🐛 fix(scripts): escape markdown backticks in vendor_playbook
064aa92 🐛 fix(scripts): correct bat scripts
4881feb 🔧 chore(gitattributes): enforce crlf for bat files
1fa3e2a 🐛 fix(scripts): escape parentheses in sync_standards output
3958cad 📝 docs(vendor_playbook): mention ci templates
9d059cf  feat(templates): add gitea ci example
a52bb24 📝 docs(codex_skills): normalize markdown formatting
cc8ad4c 📝 docs(skills): slim commit-message
2a98e15 🐛 fix(skills): quote YAML descriptions
8f78d22 🐛 fix(sync_standards): generate minimal AGENTS.md
5547665  feat(skills): add commit-message suggestion skill
3fe8bd7 🔧 chore(sync_standards): create AGENTS.md on sync
283d311  feat(playbook): add syntax book and codex skills tooling
5b97ed5 📝 docs(tsl): clarify syntax references
27e0700  feat(skills): add pdf/docx/pptx/xlsx wrapper workflows
5551363  feat(skills): add debugging and bulk refactor workflows
1d4f548  feat(skills): add built-in workflow skills
c0895dd 📝 docs(skills): add anthropics document-skills integration
cf9f80d 🔧 chore(playbook): add Claude Code skills guide and align python templates

git-subtree-dir: docs/standards/playbook
git-subtree-split: b529012c59c5d67c3073884d7b617763647417fd
2026-01-08 15:56:36 +08:00

81 lines
3.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# C++ 代码风格(Code Style
本章节规定 C++C++23,含 Modules)代码的结构与格式约定。
## 1. 文件与组织
### 1.1 目录建议(项目落地)
推荐将 C++ 代码放在独立目录(多语言项目便于隔离规则与工具):
```txt
cpp/
include/ # 对外头文件(如项目仍使用头文件边界)
src/ # 实现文件与 module 实现单元
modules/ # module interface / partition(如使用 modules
tests/ # (可选)测试
CMakeLists.txt
```
若项目不采用 `cpp/` 根目录,也应保持 include/src/modules 分层清晰。
### 1.2 文件扩展名约定
- 头文件:`.h`Google 风格;若项目已统一 `.hpp`,以项目既有为准)
- 源文件:`.cc`(优先;`main` 入口建议使用 `main.cc`;若项目已统一
`.cpp`,以项目既有为准)
- Module Interface Unit`.cppm`(推荐统一)
- Module Partition`.cppm`(在文件名中体现分区,例如 `foo_part.cppm`
### 1.3 Modules 约定(C++23
- Module 名称建议使用“点分层级”组织,且每一段使用 `lower_snake_case`
- 示例:`my_project.net.http`
- 文件路径与 module 名建议可映射(便于检索):
- `cpp/modules/my_project/net/http.cppm`
`export module my_project.net.http;`
- 避免在同一模块内混用“头文件边界”和“模块边界”造成可见性混乱;对外 API 优先通过
`export` 暴露。
- 工程约束:新增/删除/重命名 `.cppm`(或变更
`export module ...`)时,必须同步更新构建系统的模块清单(例如 CMake 的
`target_sources(FILE_SET CXX_MODULES ...)`),否则容易出现“本地能编、CI/他人机器不能编”的漂移。
## 2. 格式(Formatting
### 2.1 clang-format(必选)
- 本仓库/标准默认:`clang-format` + Google 风格。
- 建议在项目根目录提供 `.clang-format` 并纳入 CI/本地钩子。
- 列宽建议与本 Playbook 其他语言保持一致:默认 100(可按项目调整,但要全仓一致)。
- 模板:`templates/cpp/.clang-format`(参考
`tsl-devkit/lsp-server/.clang-format` 并做了最小化泛化)。
### 2.2 通用格式约定
- 缩进使用空格,单次缩进 4 空格(Google)。
- 不做“空格对齐排版”(避免 diff 噪音),用缩进表达结构。
- 头文件 include 顺序建议(从上到下):
1. 本文件对应头(如有)
2. C 标准库 / C++ 标准库
3. 第三方库
4. 项目内其他头/模块导入
## 3. 头文件与可见性
- 头文件应可被独立 include(自包含)。
- 对外接口放在 `include/`,实现细节放 `src/`
- 避免在头文件中引入沉重依赖;可用前置声明或 PImpl(按项目需要)。
## 4. 错误处理与资源管理
- 默认使用 RAII 管理资源;避免裸 `new/delete`
- 禁止吞异常/忽略错误;失败路径必须可观测(返回值/异常/日志其一或按项目约定)。
## 5. Windows 支持
- 本规范假设 **不做原生 Windows 开发环境支持**Windows 产物通过
**Linux 上的 Clang 交叉编译工具链**获得(工具链路径/三元组等由项目的 Conan
profile 或 toolchain 文件定义)。
- 如需条件编译,优先用标准特性检测与最小化条件编译,并写清动机(例如 ABI/平台差异)。
- 对路径、换行、编码、大小写敏感等行为要明确约束并在文档/测试中覆盖。