Command Palette

Search for a command to run...

Back to skills
skill № 27321

lang-cpp

Use when the user asks about modern C++ (C++20, C++23), RAII, smart pointers, move semantics, templates and concepts, std::span, coroutines, CMake or…

FreeNo executable scripts
j4flmaoby j4flmao★ 47 GitHub stars

Install lang-cpp in Claude Code & Claude Desktop

Sign up to install this skill

Create a free account to reveal the install command and save the skill to your library.

  • Reveal the one-line install command
  • Save skills to your synced library
  • Get notified when skills update
Sign up freeI already have an account

About this skill

Modern C++

Purpose

C++ offers zero-overhead abstractions and full control, at the price of undefined behaviour and lifetime bugs when used carelessly. This skill applies the modern subset — RAII everywhere, value semantics, no owning raw pointers, concepts over SFINAE — and the tooling that catches what the type system cannot.

Agent Protocol

Trigger

"C++", "modern C++", "C++20", "C++23", "RAII", "smart pointers", "move semantics", "templates", "concepts", "CMake", "rule of five", "std::span", "coroutines".

Input Context

  • Standard level and compilers (GCC, Clang, MSVC) and platforms
  • Codebase conventions (exceptions on/off, allocator rules, Core Guidelines adoption)
  • Performance/ABI constraints (library boundaries, embedded, real-time)

Output Artifact

C++ code, CMake snippets, or review findings with the rule each one violates. No file unless requested.

Response Format

Std: {C++20}  Compiler: {clang 19 / gcc 14 / MSVC 17.x}
Change: {what and why}
Code: {snippet}
Ownership/lifetime: {who owns what}
Verify: {tests + sanitizers + clang-tidy checks}

1. Ownership

Situation Use
Single owner, heap object std::unique_ptr<T> via std::make_unique<T>(...)
Shared ownership (genuinely shared lifetime) std::shared_ptr<T> via std::make_shared — costs atomic refcounts
Non-owning access T&, const T&, T* (nullable), std::span<T>, std::string_view
Break shared_ptr cycles std::weak_ptr<T>
Value with optional presence std::optional<T>
  • No new/delete in application code. A raw pointer means "borrowed, does not own".
  • string_view/span must not outlive the data they view — never return one to a local or temporary.

2. Rule of Zero / Five

  • Prefer the rule of zero: members that manage themselves (std::vector, std::unique_ptr, std::string) so the compiler-generated special members are correct.
  • If you write any of destructor, copy ctor/assignment, move ctor/assignment, consider all five; mark moves noexcept so containers use them during reallocation.
class FileHandle {
public:
    explicit FileHandle(const char* path) : f_(std::fopen(path, "rb")) {
        if (!f_) throw std::system_error(errno, std::generic_category(), path);
    }
    ~FileHandle() { if (f_) std::fclose(f_); }
    FileHandle(FileHandle&& o) noexcept : f_(std::exchange(o.f_, nullptr)) {}
    FileHandle& operator=(FileHandle&& o) noexcept {
        if (this != &o) { if (f_) std::fclose(f_); f_ = std::exchange(o.f_, nullptr); }
        return *this;
    }
    FileHandle(const FileHandle&) = delete;
    FileHandle& operator=(const FileHandle&) = delete;
    std::FILE* get() const noexcept { return f_; }
private:
    std::FILE* f_;
};

(Or: std::unique_ptr<std::FILE, decltype(&std::fclose)>.)

3. Value and Move Semantics

  • Pass cheap types by value, read-only large types by const T&, sink parameters by value then std::move into the member.
  • std::move is only a cast; the moved-from object must stay valid but unspecified — do not use its value afterwards.
  • Return by value: copy elision is guaranteed for prvalues (C++17) and NRVO is usual; do not write return std::move(local);.

4. Templates and Concepts (C++20)

#include <concepts>
#include <ranges>

template <std::ranges::input_range R>
    requires std::integral<std::ranges::range_value_t<R>>
auto sum(const R& r) {
    std::ranges::range_value_t<R> total{};
    for (auto v : r) total += v;
    return total;
}
  • Concepts give readable errors and replace most SFINAE/enable_if.
  • Static polymorphism with CRTP or concepts where virtual dispatch cost matters (see references/crtp-static-polymorphism.md); otherwise virtual functions are fine.
  • Keep heavy templates out of widely included headers; consider explicit instantiation to cut build times.

5. Error Handling

  • Exceptions for exceptional failures in code that can unwind; std::expected<T, E> (C++23) or std::optional for expected, local failures; error codes at C/ABI boundaries.
  • Destructors and move operations must not throw. Mark non-throwing functions noexcept.
  • If exceptions are disabled (-fno-exceptions, common in games/embedded), use expected-style returns consistently.

6. Concurrency

  • std::jthread (C++20, joins on destruction, stop tokens), std::mutex with std::scoped_lock (deadlock-free multi-lock), std::atomic with explicit memory orders only when profiled.
  • Data races are UB. Share immutable data freely; protect mutable shared state; prefer message passing/task systems for complex pipelines.
  • Coroutines (C++20) are a language mechanism; use a library (e.g. Asio, cppcoro-style task types, std::generator in C++23) rather than hand-writing promise types.

7. Standard Library Picks

std::vector by default (contiguous, cache friendly); std::array for fixed size; std::unordered_map (or a flat/open-addressing map from Abseil/Boost for spee

Full SKILL.md on GitHub

Allowed tools

Tools this skill is permitted to call.

No restriction — this skill can use any tool.

Bundled files

  • references/cpp-advanced.md
  • references/cpp-fundamentals.md
  • references/cpp-testing.md
  • references/crtp-static-polymorphism.md

FAQ

What does the lang-cpp skill do?

Use when the user asks about modern C++ (C++20, C++23), RAII, smart pointers, move semantics, templates and concepts, std::span, coroutines, CMake or undefined behavior in C++. Produces idiomatic C++ with ownership rules and sanitizer builds. Not for firmware C++ subsets (use c-cpp-patterns) or plain C (use lang-c).

How do I install the lang-cpp skill?

Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.

Does the lang-cpp skill run scripts?

No, this skill is instructions only (SKILL.md) with no executable scripts.

Related skills

Compare lang-cpp with