Skip to content

Commit f558c67

Browse files
authored
Initial commit
0 parents  commit f558c67

File tree

18 files changed

+1339
-0
lines changed

18 files changed

+1339
-0
lines changed

.agents/skills/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# mcpp-style-ref Agent Skills
2+
3+
用于指导 Agent 在编写或审查 Modern/Module C++ 代码时遵循 mcpp-style-ref 规范的技能。
4+
5+
## 可用技能
6+
7+
| 技能 | 说明 |
8+
|------|------|
9+
| [mcpp-style-ref](mcpp-style-ref/SKILL.md) | 面向 mcpp 项目的 Modern/Module C++ (C++23) 命名、模块化与实践规则 |
10+
11+
## 使用方式
12+
13+
要在 Cursor 中使用,请将技能软链接或复制到项目的 `.cursor/skills/`
14+
15+
```bash
16+
mkdir -p .cursor/skills
17+
ln -s ../../skills/mcpp-style-ref .cursor/skills/mcpp-style-ref
18+
```
19+
20+
或安装为个人技能:
21+
22+
```bash
23+
ln -s /path/to/mcpp-style-ref/skills/mcpp-style-ref ~/.cursor/skills/mcpp-style-ref
24+
```
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
---
2+
name: mcpp-style-ref
3+
description: 为 mcpp 项目应用 Modern/Module C++ (C++23) 编码风格。适用于编写或审查带模块的 C++ 代码、命名标识符、组织 .cppm/.cpp 文件,或用户提及 mcpp、module C++、现代 C++ 风格时。
4+
---
5+
6+
# mcpp-style-ref
7+
8+
mcpp 项目的 Modern/Module C++ 风格参考。C++23,使用 `import std`
9+
10+
## 快速参考
11+
12+
### 命名
13+
14+
| 种类 | 风格 | 示例 |
15+
|------|------|------|
16+
| 类型/类 | PascalCase(大驼峰) | `StyleRef`, `HttpServer` |
17+
| 对象/成员 | camelCase(小驼峰) | `fileName`, `configText` |
18+
| 函数 | snake_case(下划线) | `load_config_file()`, `parse_()` |
19+
| 私有 | `_` 后缀 | `fileName_`, `parse_()` |
20+
| 常量 | UPPER_SNAKE | `MAX_SIZE`, `DEFAULT_TIMEOUT` |
21+
| 全局 | `g` 前缀 | `gStyleRef` |
22+
| 命名空间 | 全小写 | `mcpplibs`, `mylib` |
23+
24+
### 模块基础
25+
26+
- 使用 `import std` 替代 `#include <print>``#include <xxx>`
27+
- 使用 `.cppm` 作为模块接口;分离实现时用 `.cpp`
28+
- `export module module_name;` — 模块声明
29+
- `export import :partition;` — 导出分区
30+
- `import :partition;` — 内部分区(不导出)
31+
32+
### 模块结构
33+
34+
```
35+
// .cppm
36+
export module a;
37+
38+
export import a.b;
39+
export import :a2; // 可导出分区
40+
41+
import std;
42+
import :a1; // 内部分区
43+
```
44+
45+
### 模块命名
46+
47+
- 模块:`topdir.subdir.filename`(如 `a.b`, `a.c`
48+
- 分区:`module_name:partition`(如 `a:a1`, `a.b:b1`
49+
- 用目录路径区分同名:`a/c.cppm``a.c``b/c.cppm``b.c`
50+
51+
### 类布局
52+
53+
```cpp
54+
class StyleRef {
55+
private:
56+
std::string fileName_; // 数据成员带 _ 后缀
57+
58+
public: // Big Five
59+
StyleRef() = default;
60+
StyleRef(const StyleRef&) = default;
61+
// ...
62+
63+
public: // 公有接口
64+
void load_config_file(std::string fileName); // 函数 snake_case,参数 camelCase
65+
66+
private:
67+
void parse_(std::string config); // 私有函数以 _ 结尾
68+
};
69+
```
70+
71+
### 实践规则
72+
73+
- **初始化**:用 `{}` — `int n { 42 }`,`std::vector<int> v { 1, 2, 3 }`
74+
- **字符串**:只读参数用 `std::string_view`
75+
- **错误**:用 `std::optional` / `std::expected` 替代 int 错误码
76+
- **内存**:用 `std::unique_ptr`、`std::shared_ptr`;避免裸 `new`/`delete`
77+
- **RAII**:将资源与对象生命周期绑定
78+
- **auto**:用于迭代器、lambda、复杂类型;需要明确表达意图时保留显式类型
79+
- **宏**:优先用 `constexpr`、`inline`、`concept` 替代宏
80+
81+
### 接口与实现
82+
83+
两种写法均支持。
84+
85+
**写法 A:合并** — 接口与实现同在一个 `.cppm` 中:
86+
87+
```cpp
88+
// mylib.cppm
89+
export module mylib;
90+
91+
export int add(int a, int b) {
92+
return a + b;
93+
}
94+
```
95+
96+
**写法 B:分离** — 接口在 `.cppm`,实现在 `.cpp`(编译期隐藏实现):
97+
98+
```cpp
99+
// error.cppm(接口)
100+
export module error;
101+
102+
export struct Error {
103+
void test();
104+
};
105+
```
106+
107+
```cpp
108+
// error.cpp(实现)
109+
module error;
110+
111+
import std;
112+
113+
void Error::test() {
114+
std::println("Hello");
115+
}
116+
```
117+
118+
简单模块用写法 A;需隐藏实现或减少编译依赖时用写法 B。
119+
120+
## 项目环境配置
121+
122+
安装 xlings 包管理器后,获取 GCC 15 工具链:
123+
124+
#### Linux/MacOS
125+
126+
```bash
127+
curl -fsSL https://raw.githubusercontent.com/d2learn/xlings/refs/heads/main/tools/other/quick_install.sh | bash
128+
```
129+
130+
#### Windows - PowerShell
131+
132+
```bash
133+
irm https://raw.githubusercontent.com/d2learn/xlings/refs/heads/main/tools/other/quick_install.ps1 | iex
134+
```
135+
136+
然后安装工具链(仅linux, 其中windows默认用msvc):
137+
138+
```bash
139+
xlings install gcc@15 -y
140+
```
141+
142+
> xlings详细信息可参考 [xlings](https://github.com/d2learn/xlings) 文档。
143+
144+
## 示例项目创建
145+
146+
参考本仓库 `src/` 目录结构:
147+
148+
- `xmake.lua`:配置 `set_languages("c++23")`、`set_policy("build.c++.modules", true)`
149+
- `add_files("main.cpp")`、`add_files("**.cppm")` 添加源文件
150+
- 可执行目标与静态库目标分离(如 `mcpp-style-ref` 主程序、`error` 静态库)
151+
152+
构建:
153+
154+
```bash
155+
xmake build
156+
xmake run
157+
```
158+
159+
## 适用场景
160+
161+
- 编写新的 C++ 模块代码(`.cppm``.cpp`
162+
- 审查或重构 mcpp 项目中的 C++ 代码
163+
- 用户询问「mcpp 风格」「module C++ 风格」或「现代 C++ 惯例」
164+
165+
## 更多资源
166+
167+
- 完整参考:[reference.md](reference.md)
168+
- mcpp-style-ref 仓库:[github.com/mcpp-community/mcpp-style-ref](https://github.com/mcpp-community/mcpp-style-ref)
169+
- 项目说明:[../../README.md](../../README.md)
170+
- 示例项目:[src/](../../../src)
171+
- xlings 包管理器:[github.com/d2learn/xlings](https://github.com/d2learn/xlings)
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
# mcpp-style-ref 参考
2+
3+
来自 [mcpp-style-ref](https://github.com/mcpp-community/mcpp-style-ref) 的详细风格规则。
4+
5+
## 一、标识符命名
6+
7+
### 1.0 类型 — PascalCase(大驼峰)
8+
9+
```cpp
10+
struct StyleRef {
11+
using FileNameType = std::string;
12+
};
13+
```
14+
15+
### 1.1 对象/数据成员 — camelCase(小驼峰)
16+
17+
```cpp
18+
struct StyleRef {
19+
std::string fileName;
20+
};
21+
StyleRef mcppStyle;
22+
```
23+
24+
### 1.2 函数 — snake_case(下划线)
25+
26+
```cpp
27+
void load_config_file(const std::string& fileName);
28+
void parse_();
29+
int max_retry_count();
30+
```
31+
32+
### 1.3 私有 — `_` 后缀
33+
34+
私有的数据成员和函数使用 `_` 后缀:
35+
36+
```cpp
37+
private:
38+
std::string fileName_;
39+
void parse_(const std::string& config);
40+
```
41+
42+
### 1.4 空格
43+
44+
运算符两侧加空格以增强可读性:`T x { ... }``int n { 42 }`
45+
46+
### 1.5 其他
47+
48+
- 常量:`MAX_SIZE``DEFAULT_TIMEOUT`
49+
- 全局:`gStyleRef``g_debug`
50+
- 模板命名:遵循类/函数命名风格
51+
52+
---
53+
54+
## 二、模块化
55+
56+
### 模块文件结构
57+
58+
```cpp
59+
module; // 可选的全局模块片段
60+
#include <xxx> // 需要传统头文件时
61+
62+
export module module_name;
63+
// export import :partition;
64+
// import :partition;
65+
66+
import std;
67+
import xxx;
68+
69+
export int add(int a, int b) {
70+
return a + b;
71+
}
72+
```
73+
74+
### .cppm 与 .h/.hpp
75+
76+
使用 `.cppm` 作为模块接口。用 `export` 关键字导出:
77+
78+
```cpp
79+
export module mcpplibs;
80+
81+
export int add(int a, int b) {
82+
return a + b;
83+
}
84+
```
85+
86+
### 接口与实现
87+
88+
合并(全部在 .cppm)与分离(.cppm + .cpp)均有效。
89+
90+
**合并于 .cppm** — 见上方「.cppm 与 .h/.hpp」:导出与实现在同一文件。
91+
92+
**方式一:命名空间隔离**
93+
94+
```cpp
95+
export module mcpplibs;
96+
97+
namespace mcpplibs_impl {
98+
int add(int a, int b) { return a + b; }
99+
}
100+
101+
export namespace mcpplibs {
102+
using mcpplibs_impl::add;
103+
};
104+
```
105+
106+
**方式二:分离(.cppm + .cpp)**
107+
108+
- `.cppm`:仅接口 — `export module error;` + `export struct Error { void test(); };`
109+
- `.cpp`:实现 — `module error;` + 函数体
110+
111+
简单模块用合并;需隐藏实现或减少编译依赖时用分离。
112+
113+
### 多文件模块
114+
115+
```
116+
a/
117+
├── a1.cppm # module a:a1(内部分区)
118+
├── a2.cppm # export module a:a2
119+
├── b/
120+
│ ├── b1.cppm # export module a.b:b1
121+
│ └── b2.cppm # export module a.b:b2
122+
├── b.cppm # export module a.b
123+
└── c.cppm # module a.c
124+
a.cppm # export module a
125+
```
126+
127+
- **可导出分区**:`export module a:a2;` — 可被重新导出
128+
- **内部分区**:`module a:a1;` — 不导出,仅模块内部使用
129+
130+
```cpp
131+
// a.cppm
132+
export module a;
133+
export import :a2;
134+
import :a1;
135+
```
136+
137+
### 向前兼容
138+
139+
将传统 C/C++ 头文件封装到兼容模块中:
140+
141+
```cpp
142+
module;
143+
144+
#include <lua.h>
145+
// ...
146+
147+
export module lua;
148+
149+
export namespace lua {
150+
using lua_State = ::lua_State;
151+
// ...
152+
}
153+
```
154+
155+
### 其他
156+
157+
- 优先用 `constexpr` 替代宏
158+
- 模板的静态成员:使用 `inline static`(C++17)确保单一定义
159+
160+
---
161+
162+
## 三、实践参考
163+
164+
### auto
165+
166+
用于迭代器、lambda、复杂类型。显式类型更清晰时避免使用。
167+
168+
### 花括号初始化
169+
170+
`int n { 42 }`、`std::vector<int> v { 1, 2, 3 }`、`Point p { 10, 20 }`。
171+
172+
### 智能指针
173+
174+
`std::make_unique`、`std::make_shared`;避免裸 `new`/`delete`。
175+
176+
### string_view
177+
178+
用于只读字符串参数。不拥有数据,调用方需保证底层数据有效。
179+
180+
### optional / expected
181+
182+
- `std::optional`:可有可无的值
183+
- `std::expected`(C++23):成功返回值或错误
184+
185+
### RAII
186+
187+
将资源与对象生命周期绑定。使用 `std::fstream`、`std::lock_guard` 等。

.gitattributes

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
*.cppm linguist-language=cpp
2+
*.cppm linguist-detectable=true
3+
*.cppm linguist-documentation=false
4+
5+
*.mpp linguist-language=cpp
6+
*.mpp linguist-detectable=true
7+
*.mpp linguist-documentation=false

0 commit comments

Comments
 (0)