OpenParable
Skip to content
← Back to skills

Mcpp Style Ref

Development
mcpp-style-ref

Mcpp Style Ref is an Agent Skill for development published in the openxlings/xlings repository on GitHub: a SKILL.md instruction file an AI agent can load to do this task.

为 mcpp 项目应用 Modern/Module C++ (C++23) 编码风格。适用于编写或审查带模块的 C++ 代码、命名标识符、组织 .cppm/.cpp 文件,或用户提及 mcpp、module C++、现代 C++ 风格时。

View on GitHub ↗
QUICK START

How to use this skill

Bring this guide into your coding agent with a prompt tailored to the tool you use.

  1. Open your project in Codex.
  2. Copy and paste the prompt below.
  3. Review files and risks before approving any action.
Prompt to paste
Inspect the Agent Skill at https://github.com/openxlings/xlings/blob/HEAD/.agents/skills/mcpp-style-ref/SKILL.md and all supporting files. Report provenance, permissions, scripts, dependencies, and security risks. Propose an exact installation into .agents/skills/mcpp-style-ref, but wait for my approval before writing files or running scripts.

Copying this prompt does not install or run the skill. Review third-party files before use.

mcpp-style-ref

mcpp 项目的 Modern/Module C++ 风格参考。C++23,使用 import std。

快速参考

命名

种类风格示例
类型/类PascalCase(大驼峰)StyleRef, HttpServer
对象/成员camelCase(小驼峰)fileName, configText
函数snake_case(下划线)load_config_file(), parse_()
私有_ 后缀fileName_, parse_()
常量UPPER_SNAKEMAX_SIZE, DEFAULT_TIMEOUT
全局g 前缀gStyleRef
命名空间全小写mcpplibs, mylib

模块基础

  • 使用 import std 替代 #include <print> 和 #include <xxx>
  • 使用 .cppm 作为模块接口;分离实现时用 .cpp
  • export module module_name; — 模块声明
  • export import :partition; — 导出分区
  • import :partition; — 内部分区(不导出)

模块结构

// .cppm
export module a;

export import a.b;
export import :a2;   // 可导出分区

import std;
import :a1;          // 内部分区

模块命名

  • 模块:topdir.subdir.filename(如 a.b, a.c)
  • 分区:module_name:partition(如 a:a1, a.b:b1)
  • 用目录路径区分同名:a/c.cppm → a.c,b/c.cppm → b.c

类布局

class StyleRef {
private:
    std::string fileName_;  // 数据成员带 _ 后缀

public:  // Big Five
    StyleRef() = default;
    StyleRef(const StyleRef&) = default;
    // ...

public:  // 公有接口
    void load_config_file(std::string fileName);  // 函数 snake_case,参数 camelCase

private:
    void parse_(std::string config);  // 私有函数以 _ 结尾
};

实践规则

  • 初始化:用 {} — int n { 42 },std::vector<int> v { 1, 2, 3 }
  • 字符串:只读参数用 std::string_view
  • 错误:用 std::optional / std::expected 替代 int 错误码
  • 内存:用 std::unique_ptr、std::shared_ptr;避免裸 new/delete
  • RAII:将资源与对象生命周期绑定
  • auto:用于迭代器、lambda、复杂类型;需要明确表达意图时保留显式类型
  • 宏:优先用 constexpr、inline、concept 替代宏

接口与实现

两种写法均支持。

写法 A:合并 — 接口与实现同在一个 .cppm 中:

// mylib.cppm
export module mylib;

export int add(int a, int b) {
    return a + b;
}

写法 B:分离 — 接口在 .cppm,实现在 .cpp(编译期隐藏实现):

// error.cppm(接口)
export module error;

export struct Error {
    void test();
};
// error.cpp(实现)
module error;

import std;

void Error::test() {
    std::println("Hello");
}

简单模块用写法 A;需隐藏实现或减少编译依赖时用写法 B。

项目环境配置

安装 xlings 包管理器后,获取 GCC 15 工具链:

Linux/MacOS

curl -fsSL https://raw.githubusercontent.com/d2learn/xlings/refs/heads/main/tools/other/quick_install.sh | bash

Windows - PowerShell

irm https://raw.githubusercontent.com/d2learn/xlings/refs/heads/main/tools/other/quick_install.ps1 | iex

然后安装工具链(仅linux, 其中windows默认用msvc):

xlings install gcc@15 -y

xlings详细信息可参考 xlings 文档。

示例项目创建

参考本仓库 src/ 目录结构:

  • mcpp.toml:声明 C++23 模块目标、工具链和依赖
  • add_files("main.cpp")、add_files("**.cppm") 添加源文件
  • 可执行目标与静态库目标分离(如 mcpp-style-ref 主程序、error 静态库)

构建:

mcpp build
mcpp test

适用场景

  • 编写新的 C++ 模块代码(.cppm、.cpp)
  • 审查或重构 mcpp 项目中的 C++ 代码
  • 用户询问「mcpp 风格」「module C++ 风格」或「现代 C++ 惯例」

更多资源