# Cpp Expert

> Expert modern C++ (17/20/23): safe, performant code, RAII, move semantics, templates/concepts, and UB diagnosis. Trigger keywords: C++, C++17, C++20, C++23, smart pointer, unique_ptr, RAII, move semantics, rvalue, template, concept, STL, undefined behavior, constexpr, memory, dangling. Use for C++ implementation, optimization, generic programming, or UB/memory issues.

- **Type:** Skill
- **Install:** `agentstack add skill-miaoge-ge-coding-agent-skills-cpp-expert`
- **Verified:** Yes — security-reviewed for prompt injection and unsafe behavior
- **Seller:** [Miaoge-Ge](https://agentstack.voostack.com/s/miaoge-ge)
- **Installs:** 0
- **Category:** [Agent Skills](https://agentstack.voostack.com/c/agent-skills)
- **Latest version:** 0.1.0
- **License:** MIT
- **Upstream author:** [Miaoge-Ge](https://github.com/Miaoge-Ge)
- **Source:** https://github.com/Miaoge-Ge/coding-agent-skills/tree/main/plugins/cpp-expert/skills/cpp-expert

## Install

```sh
agentstack add skill-miaoge-ge-coding-agent-skills-cpp-expert
```

Requires the [AgentStack CLI](https://agentstack.voostack.com/docs/cli). Works with Claude Code, Cursor, and any MCP-compatible agent.

## About

# C++ Expert

> RAII owns every resource; values move, don't copy. Make ownership explicit, lean on the STL, and treat undefined behavior as a bug to prevent — not a surprise to debug.

## When to Use
- Writing or reviewing C++.
- Modern features (C++17/20/23), templates/metaprogramming, STL.
- Performance/memory tuning; smart pointers and ownership.
- Compile errors, crashes, leaks, or undefined behavior (UB).

## When NOT to Use
- Another language → relevant language skill.
- Contest algorithms (language incidental) → `competitive-programming-expert`.
- System/architecture design → `software-architect`.

## Core Principles

### 1. Modern by default
- Target C++17/20 unless constrained. Prefer `auto`, range-based `for`, structured bindings, `std::optional`/`variant`/`string_view`/`span`.
- Constrain templates with **Concepts** (C++20) or `static_assert` so errors surface at the call site, not deep in instantiation.

### 2. Ownership & RAII
- No owning raw pointers. `std::unique_ptr` for sole ownership, `shared_ptr` only when ownership is genuinely shared; create with `make_unique`/`make_shared`. Raw pointers/references are non-owning observers.
- Every resource (memory, file, lock, socket) is owned by an object that releases it in its destructor. Locks via `std::scoped_lock`/`lock_guard`.
- Follow the Rule of 0 (no manual special members) — or Rule of 5 if you manage a resource directly.

### 3. Value semantics & performance
- Pass `const&` for read-only, by value + `std::move` to sink, `string_view`/`span` for non-owning views. Rely on (N)RVO — return by value.
- Avoid premature `shared_ptr` (atomic refcount cost) and needless copies; `emplace_back`, `reserve`. Choose containers deliberately (`vector` by default). `constexpr`/`consteval` for compile-time work. Profile before micro-optimizing.

### 4. Avoid UB proactively
- Dangling refs/views, use-after-move, out-of-bounds, signed overflow, uninitialized reads, iterator invalidation, data races. Build with warnings (`-Wall -Wextra`), sanitizers (ASan/UBSan/TSan), and tools (clang-tidy).

## Common Mistakes
- **Owning raw `new`/`delete`** → leaks/double-free; use smart pointers + RAII.
- **Returning `string_view`/reference to a local/temporary** → dangling.
- **Using a moved-from object** beyond a valid-but-unspecified state.
- **`shared_ptr` everywhere** → atomic overhead + cycles (use `weak_ptr` to break).
- **C-style casts / `reinterpret_cast`** → use `static_cast`/`dynamic_cast`.
- **Iterator/reference invalidation** after container growth/erase.
- **`std::endl` in loops** → forced flush; use `'\n'`.

## Examples

**RAII + move-only ownership (C++17)**
```cpp
#include 
#include 
#include 

class Connection {
public:
    explicit Connection(std::string dsn) : dsn_(std::move(dsn)) { /* open */ }
    ~Connection() noexcept { /* close */ }
    Connection(const Connection&) = delete;            // non-copyable
    Connection& operator=(const Connection&) = delete;
    Connection(Connection&&) noexcept = default;        // movable
    Connection& operator=(Connection&&) noexcept = default;
private:
    std::string dsn_;
};

auto make_conn(std::string dsn) {
    return std::make_unique(std::move(dsn)); // ownership is explicit
}
```

**Concept-constrained template (clear errors, no overflow)**
```cpp
#include 
template 
constexpr T midpoint(T a, T b) noexcept { return a + (b - a) / 2; }
```

## See Also
- `competitive-programming-expert` — STL-heavy solutions and constant-factor tuning.
- `performance-expert` — profiling and allocation reduction.
- `rust-expert` — comparing systems-language ownership models.
- `rules/cpp-style-guide.md` — enforceable style rules.

## Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

- **Author:** [Miaoge-Ge](https://github.com/Miaoge-Ge)
- **Source:** [Miaoge-Ge/coding-agent-skills](https://github.com/Miaoge-Ge/coding-agent-skills)
- **License:** MIT

Install and usage instructions live in the source repository linked above.

## Pricing

- **Free** — Free

## Security capabilities

Automated source analysis of v0.1.0 — what this tool can access:

- **Network access:** no
- **Filesystem access:** no
- **Shell / process execution:** no
- **Environment & secrets:** no
- **Dynamic code execution:** no

*"Yes" means the capability is present in the source — more access means more to trust, not that it is unsafe.*


## Versions

- **0.1.0** — security scan: passed — Imported from the upstream source.

## Links

- Listing page: https://agentstack.voostack.com/l/skill-miaoge-ge-coding-agent-skills-cpp-expert
- Seller: https://agentstack.voostack.com/s/miaoge-ge
- Browse the marketplace: https://agentstack.voostack.com/browse

---
Listed on AgentStack — the marketplace for AI agent skills and MCP servers. Every listing is security-reviewed. Creators keep 70%.
