{"slug":"cpp-2","title":"cpp","summary":"Use when writing modern C++ (17/20/23). Covers RAII, smart-pointer ownership, move semantics, ranges, concepts, and eliminating undefined behavior with sanitizers. Trigger on \"segfault in my c++ code\", \"should this be a unique_ptr or shared_ptr\", \"get rid of the raw new and delet","platform":"Claude","tags":[],"authorName":"LLM Mart","authorSlug":"llm-mart","score":0,"source":"github","price":null,"verified":false,"createdAt":"2026-11-09T16:52:45.180305Z","repo":{"url":"https://github.com/nimadorostkar/Claude-Skills-collection","stars":34,"forks":4,"license":"MIT","updatedAt":"2026-11-08T15:41:45Z"},"bodyHtml":"<hr>\n<h2>name: cpp\ndescription: Use when writing modern C++ (17/20/23). Covers RAII, smart-pointer ownership, move semantics, ranges, concepts, and eliminating undefined behavior with sanitizers. Trigger on \"segfault in my c++ code\", \"should this be a unique_ptr or shared_ptr\", \"get rid of the raw new and delete\", \"asan reports heap-use-after-free\".\nmetadata:\ncategory: languages\nversion: 1.0.0\ntags: [cpp, raii, move-semantics, concepts, sanitizers]</h2>\n<h1>C++</h1>\n<h2>Purpose</h2>\n<p>Write C++ where resources are owned by objects, ownership is visible in the type, and undefined behavior is actively hunted with tooling rather than hoped away.</p>\n<h2>When to Use</h2>\n<ul>\n<li>Writing or reviewing C++17 and later.</li>\n<li>Modernizing code that uses raw <code>new</code>/<code>delete</code> or manual resource cleanup.</li>\n<li>Diagnosing memory corruption, leaks, or data races.</li>\n<li>Designing value types with correct copy/move semantics.</li>\n<li>Optimizing hot paths where allocation or copying dominates.</li>\n</ul>\n<h2>Capabilities</h2>\n<ul>\n<li>RAII and ownership modeling with <code>unique_ptr</code>, <code>shared_ptr</code>, and value semantics.</li>\n<li>Move semantics, the rule of zero/five, and copy elision.</li>\n<li>Ranges, views, and algorithm composition.</li>\n<li>Concepts for constrained templates and readable errors.</li>\n<li>Sanitizer-driven debugging: ASan, UBSan, TSan.</li>\n</ul>\n<h2>Inputs</h2>\n<ul>\n<li>Source, build system (CMake preferred), and target standard.</li>\n<li>Platform and toolchain constraints.</li>\n<li>Performance budget for hot paths.</li>\n</ul>\n<h2>Outputs</h2>\n<ul>\n<li>Code with no raw owning pointers and no manual <code>delete</code>.</li>\n<li>A CMake configuration with warnings-as-errors and sanitizer build types.</li>\n<li>Benchmarks for any change justified on performance grounds.</li>\n</ul>\n<h2>Workflow</h2>\n<ol>\n<li><strong>Establish ownership</strong> — Every resource has exactly one owner. Express it in the type: value, <code>unique_ptr</code>, or a documented non-owning <code>T*</code>/<code>span</code>.</li>\n<li><strong>Apply the rule of zero</strong> — If a class needs a destructor, it probably should not also hold business logic.</li>\n<li><strong>Constrain templates</strong> — Use concepts; unconstrained templates produce unreadable errors and accept nonsense.</li>\n<li><strong>Sanitize</strong> — Build and run the test suite under ASan+UBSan, then TSan separately.</li>\n<li><strong>Measure before optimizing</strong> — Profile, change one thing, benchmark again.</li>\n</ol>\n<h2>Best Practices</h2>\n<ul>\n<li>Prefer values and references. Reach for <code>shared_ptr</code> only when ownership is genuinely shared — it is not a default.</li>\n<li>Pass by <code>const&amp;</code> for large read-only inputs; pass by value and <code>std::move</code> when you intend to store.</li>\n<li>Never return a reference or <code>string_view</code> to a temporary. This is the most common lifetime bug in modern C++.</li>\n<li>Compile with <code>-Wall -Wextra -Wpedantic -Werror</code>. Warnings in C++ are usually latent bugs.</li>\n<li>Use <code>std::span</code> instead of pointer+length parameter pairs.</li>\n<li>Mark single-argument constructors <code>explicit</code> unless implicit conversion is genuinely desired.</li>\n</ul>\n<h2>Examples</h2>\n<p><strong>RAII wrapper with clear ownership:</strong></p>\n<pre><code>class FileHandle {\npublic:\n    explicit FileHandle(const std::filesystem::path&amp; path)\n        : file_(std::fopen(path.c_str(), \"rb\")) {\n        if (!file_) {\n            throw std::system_error(errno, std::generic_category(), path.string());\n        }\n    }\n\n    FileHandle(FileHandle&amp;&amp;) noexcept = default;\n    FileHandle&amp; operator=(FileHandle&amp;&amp;) noexcept = default;\n    FileHandle(const FileHandle&amp;) = delete;\n    FileHandle&amp; operator=(const FileHandle&amp;) = delete;\n\n    ~FileHandle() { if (file_) std::fclose(file_); }\n\n    [[nodiscard]] std::FILE* get() const noexcept { return file_; }\n\nprivate:\n    std::FILE* file_;\n};\n</code></pre>\n<p><strong>Constrained template with concepts:</strong></p>\n<pre><code>template &lt;std::ranges::input_range R&gt;\n    requires std::totally_ordered&lt;std::ranges::range_value_t&lt;R&gt;&gt;\nauto median(R&amp;&amp; range) {\n    std::vector values(std::ranges::begin(range), std::ranges::end(range));\n    if (values.empty()) throw std::invalid_argument(\"median of empty range\");\n    auto mid = values.begin() + std::ssize(values) / 2;\n    std::ranges::nth_element(values, mid);\n    return *mid;\n}\n</code></pre>\n<h2>Notes</h2>\n<ul>\n<li>ASan and TSan cannot be combined in a single build. Run them as separate CI jobs.</li>\n<li>Copy elision is guaranteed for prvalues since C++17 — returning a local by value costs nothing.</li>\n<li><code>shared_ptr</code> cycles leak. Break them with <code>weak_ptr</code> on the back-reference.</li>\n</ul>\n","files":[{"path":"SKILL.md","sizeBytes":4204,"isText":true}],"reviewScore":null,"reviewSummary":null,"trust":{"provenance":"trusted-source-unreviewed","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow.","bodySource":null},"bodyLocked":false,"purchaseUrl":null,"sourceUrl":null,"report":{"provenance":"trusted-source-unreviewed","screen":{"ran":true,"outcome":"clean","suspicious":0,"notes":0,"hiddenCharacters":false},"virusScan":{"engine":"clamav","status":"clean","scannedAt":"2026-11-09T16:52:54.301728Z","sha256":"1F0855BE9D2D59FB5EFFC5A048B3EFEC5D0DFBF718876451C6D1599912EF75B5","sizeBytes":2146},"review":null,"source":{"repositoryUrl":"https://github.com/nimadorostkar/Claude-Skills-collection","path":"skills/languages/cpp","license":"MIT","commit":"1691932edc17063d712a85d02bcc7a1cd3791057","subtreeSha":"41C10E9563D6239BFE03D0A27FD09A49B5BD582058C873F1756A7D18767165A3","lastSyncedAt":"2026-11-09T16:52:45.174974Z"},"reviewedAt":"2026-11-09T16:53:14.128865Z","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow."},"install":[{"target":"skills-cli","command":"npx skills add https://github.com/nimadorostkar/Claude-Skills-collection/tree/main/skills/languages/cpp"},{"target":"claude-code","command":"claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install nimadorostkar-claude-skills-collection@llmmart"},{"target":"git","command":"git clone https://github.com/nimadorostkar/Claude-Skills-collection.git"}]}