Cpp modern pro

From findscripter/everything-skills · View interactive version

Cpp modern pro is an AI agent skill from the GitHub repository findscripter/everything-skills. 当用 C++11/14/17/20/23 编写或重构代码、需要内存安全与高性能时使用;产出符合 Core Guidelines 的惯用代码、CMakeLists、单测与基准、Sanitizer 干净输出;不适用于 C 语言、嵌入式裸机或非 C++ 任务。触发词:现代 C++、RAII、智能指针、移动语义、STL 算法、模板

How to use this skill

Skill file

何时使用

适用:

不该用(负边界):

步骤

  1. 澄清目标、约束与输入:C++ 标准版本、目标平台/编译器、性能或安全指标。
  2. 优先栈分配与 RAII,避免手工 new/delete 管理内存。
  3. 确需堆分配时使用智能指针:独占用 unique_ptr,共享所有权才用 shared_ptr。
  4. 遵循 Rule of Zero/Three/Five:能不写特殊成员函数就不写(Rule of Zero 优先)。
  5. 贯彻 const 正确性,能编译期求值的用 constexpr;C++20 起用 concepts 约束模板。
  6. 用 STL 算法替代裸循环(std::transform/std::accumulate/ranges 等)。
  7. 移动语义与完美转发:转移所有权用 std::move,转发引用用 std::forward。
  8. 并发用 std::thread/std::atomic/std::mutex,明确异常安全保证(基本/强/不抛)。
  9. 用 perf、VTune 做性能剖析后再优化,避免凭感觉调优。
  10. 交付前用 ASan/TSan 跑出干净输出,并补单测与基准。

指令

示例

智能指针与 RAII(独占所有权,Rule of Zero):

#include <memory>
#include <vector>

struct Widget {
    explicit Widget(int id) : id_(id) {}
    int id_;
};

// 优先栈/容器管理生命周期,无需手写析构
std::vector<std::unique_ptr<Widget>> make_widgets() {
    std::vector<std::unique_ptr<Widget>> v;
    v.push_back(std::make_unique<Widget>(1));
    return v; // 移动返回,零拷贝
}

STL 算法替代裸循环:

#include <numeric>
#include <vector>

long long sum_squares(const std::vector<int>& xs) {
    return std::accumulate(xs.begin(), xs.end(), 0LL,
        [](long long acc, int x) { return acc + 1LL * x * x; });
}

CMake 与 Sanitizer(开发期开启 ASan):

cmake_minimum_required(VERSION 3.16)
project(demo CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_compile_options(-Wall -Wextra)
# 调试构建开启地址消毒器
add_compile_options($<$<CONFIG:Debug>:-fsanitize=address>)
add_link_options($<$<CONFIG:Debug>:-fsanitize=address>)

注意事项

互见


采编自 sickn33/antigravity-awesome-skills(MIT)。

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.