cpp-advanced-practices

A set of C++ coding guidelines covering tests, safer memory and file handling, and object pools. It includes Catch2 test examples, buffer-overflow checks, file locks, and reusable object storage.

In plain words
What is it for?
Use it when writing or reviewing C++ code, adding Catch2 unit tests, handling buffers and files, or applying object-pool and design-pattern techniques.
Why use it?
It gives developers concrete patterns for reducing common C++ bugs involving memory, resources, and untested behavior.

Cursor rule for Cursor

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/wangqiqi/cursor-ai-rules/cpp-advanced-practices
Clone the repo
git clone --depth 1 https://github.com/wangqiqi/cursor-ai-rules

Made for: Cursor.

Per session 0 Nothing until a file matches its globs; then the whole rule loads.
When invoked 616 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00000 $0.00616
Opus 5 $0.00000 $0.00308
Sonnet 5 $0.00000 $0.00123
Haiku 4.5 $0.00000 $0.00062

Measured 2d ago against content hash 3d689b237e0c, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

cpp-advanced-practices scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

.cursor/rules/tech/cpp-advanced-practices.mdc · 90 lines

What it actually says

C++ 最佳实践 (Practices)

本规则由 @cpp-advanced 引用

Catch2 单元测试

#include <catch2/catch_test_macros.hpp>

TEST_CASE("MathUtils - Basic", "[math]") {
    SECTION("Addition") { REQUIRE(utils.add(2, 3) == 5); }
    SECTION("Division by zero") { REQUIRE_THROWS_AS(utils.divide(1.0, 0.0), std::domain_error); }
}

安全实践

void safeCopy(const char* src, char* dst, size_t dstSize) {
    size_t srcLen = std::strlen(src);
    if (srcLen >= dstSize) throw std::length_error("Buffer overflow");
    std::memcpy(dst, src, srcLen + 1);
}

class FileLock {
    int fd_;
    bool locked_;
public:
    explicit FileLock(int fd) : fd_(fd), locked_(false) {
        if (lockf(fd_, F_LOCK, 0) == -1) throw std::system_error(...);
        locked_ = true;
    }
    ~FileLock() { if (locked_) lockf(fd_, F_ULOCK, 0); }
};

对象池

template<typename T>
class ObjectPool {
    std::vector<std::unique_ptr<T>> pool_;
    std::vector<T*> available_;
public:
    template<typename... Args>
    T* acquire(Args&&... args) {
        if (!available_.empty()) {
            T* obj = available_.back();
            available_.pop_back();
            return new (obj) T(std::forward<Args>(args)...);
        }
        pool_.push_back(std::make_unique<T>(std::forward<Args>(args)...));
        return pool_.back().get();
    }
    void release(T* obj) { obj->~T(); available_.push_back(obj); }
};

设计模式 (策略)

class SortStrategy {
public:
    virtual void sort(std::vector<int>& data) = 0;
};
class Sorter {
    std::unique_ptr<SortStrategy> strategy_;
public:
    void setStrategy(std::unique_ptr<SortStrategy> s) { strategy_ = std::move(s); }
    void sort(std::vector<int>& data) { if (strategy_) strategy_->sort(data); }
};

原则

  • MUST 使用智能指针管理资源
  • MUST 进行边界检查
  • PREFER C++11/14,C++17+ 待项目成熟

引用: @cpp-advanced

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 90 lines · 0 tokens per session scan A 3d689b237e0c

Subscribe to this mod's changes

cpp-advanced-practices is a cursor rule published in the GitHub repository wangqiqi/cursor-ai-rules (15 stars, last pushed 3mo ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 616 tokens. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.