Skip to content
Skills Library
Programming · Building & coding

programming-cpp

/programming-cpp

Modern C++ practices for AI coding agents: architecture, RAII, value semantics, dependency and library selection. Use when writing or reviewing C++ code.

Author
Muvon ↗
Licence
Apache-2.0
Last repository change
12.11.2026

Assessment and security

100/ 100Class: good
Format25 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content35 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
2
Scripts
0
HTML comments
0
URLs in SKILL.md
10
Deprecation

No signals detected. Repository changed on 12.11.2026, without references to retired models or APIs.

Licence

Apache-2.0, otwarta: można używać i zmieniać, z zachowaniem licencji i informacji o zmianach.

What it does and when to use it

Modern C++ practices for AI coding agents: architecture, RAII, value semantics, dependency and library selection. Use when writing or reviewing C++ code.

Original author description

Modern C++ practices for AI coding agents: architecture, RAII, value semantics, dependency and library selection. Use when writing or reviewing C++ code.

How it works

  1. Overview
  2. Mental model
  3. Modern features, gated by support
  4. Ownership and lifetime
  5. Types, errors, and numeric correctness
  6. Concurrency and build boundaries
  7. Example
  8. Checklist
  9. References
Original SKILL.md headings · 76 lines, 885 words
SKILL.md preview
---
name: programming-cpp
title: "C++ Development Best Practices"
description: "Modern C++ practices for AI coding agents: architecture, RAII, value semantics, dependency and library selection. Use when writing or reviewing C++ code."
license: Apache-2.0
compatibility: "Requires the project's C++ compiler, standard library, and build system."
domains: developer
rules:
  - file(CMakeLists.txt)
  - file(meson.build)
  - file(conanfile.txt)
  - file(conanfile.py)
  - file(vcpkg.json)
  - content(c++)
  - content(cpp)
---

## Overview

Write modern C++ with value semantics, deterministic resource cleanup, and explicit lifetime contracts. As of 2026-10: C++23 is the published ISO standard; C++26 was technically completed by WG21 in March 2026 and awaits ISO publication, with compiler and library support still partial. Read build presets, compiler versions, selected language standard, standard-library implementation, and CI targets first. A compiler accepting `-std=c++23` or `-std=c++26` does not prove every facility of that standard is available. Recheck vendor support tables when adopting features.

## Mental model

Every resource has an owner; every view has a lifetime. Prefer values and standard containers. RAII (resource acquisition is initialization) makes cleanup follow scope exit, including exceptions, but does not automatically prevent dangling references or unsynchronized shared mutation.

## Modern features, gated by support

| Standard | Useful facilities |
|----------|-------------------|
| C++20 | Concepts, ranges, `std::span`, `std::format`, `std::jthread`/stop tokens, `consteval`, `constinit`. |
| C++23 | `std::expected`, `std::print`, `std::ranges::to`, explicit object parameters, `std::mdspan`. |

Use concepts to express template requirements and ranges when the resulting pipeline is clearer than a loop. `std::format` is C++20; `std::print` is C++23. Neither guarantees a performance win for your workload. Modules and newer library features need compiler, library, and build-system support; don't introduce a migration or compatibility wrapper merely to use newer syntax. C++26 adds static reflection, contracts (`pre`, `post`, `contract_assert`), `std::execution` senders/receivers, standard-library hardening, and erroneous behavior for uninitialized reads; use them only where the project selects C++26 and every supported compiler implements the specific paper.

For safety on current toolchains, enable the implementation's library hardening in debug and CI builds where the project allows it (`_GLIBCXX_ASSERTIONS` for libstdc++, `_LIBCPP_HARDENING_MODE` for libc++), and run AddressSanitizer/UndefinedBehaviorSanitizer in tests; hardening converts some undefined behavior into traps, not into recoverable errors.

## Ownership and lifetime

- Let RAII members implement cleanup and copying/moving where they match the type's semantics: the rule of zero. For direct resource owners, define or delete the relevant special members deliberately.
- Use `unique_ptr` for exclusive dynamic ownership; use `shared_ptr` only for shared lifetime, with `weak_ptr` where needed to avoid ownership cycles. Shared ownership does not synchronize the pointed-to object.
- Raw pointers, references, `string_view`, and `span` ordinarily borrow. Don't retain or return them beyond their backing storage's lifetime. Container reallocation and mutation can invalidate views and iterators; lazy ranges may retain references to predicates and source storage.
- Pass cheap values by value and expensive read-only inputs by `const&`. Transfer ownership explicitly. Return values naturally; `return std::move(local)` can prevent named return value optimization.
- Standard-library moved-from objects are generally valid but unspecified unless documented otherwise. Check operation preconditions before reuse; custom types must define their own valid moved-from behavior.
- A base intended for deletion through a base pointer needs a public virtual destructor; a protected nonvirtual destructor can prohibit that operation. Avoid object slicing when passing polymorphic values.

## Types, errors, and numeric correctness

- Use `optional` for expected absence, `variant` for closed alternatives, and `expected<T, E>` for explicit recoverable failure where compatible with the project's error model. Don't replace established exceptions globally.
- An `expected` must be checked before unchecked value access. Preserve meaningful errors; don't convert failed parsing, I/O, or configuration into a successful empty object or zero.
- Mark results `[[nodiscard]]` when discarding them usually violates the contract. Keep destructors nonthrowing; use an explicit operation when callers need to observe finalization errors.
- Initialize values, validate narrowing conversions, and check arithmetic that can overflow. Signed integer overflow is undefined behavior. Parse complete input: successful prefix parsing must not silently accept trailing junk.
- Keep casts and raw-memory manipulation at explicit interoperability boundaries. Document alignment, lifetime, aliasing, and ownership assumptions; don't bypass the type system to silence a diagnostic.

## Concurrency and build boundaries

- Prefer scoped task ownership. A `jthread` requests stop and joins when destroyed; stopping is cooperative, so blocking operations need a way to wake. Captured references must remain alive until work finishes.
- Protect compound shared invariants with a mutex. Atomic variables alone don't make a multi-step algorithm safe, and `std::atomic<T>` is not guaranteed lock-free. Weaker memory ordering needs a correctness argument, not just a benchmark.
- `const&` does not prove thread safety: another alias may mutate the same object. Establish lifetime and synchronization before sharing access.
- In CMake, attach features, include paths, and dependencies to targets. Use `PRIVATE`, `PUBLIC`, and `INTERFACE` to express actual consumer requirements. Preserve the existing dependency manager and reproducible version policy.

## Example

C++23: preserve failure as an explicit alternative instead of substituting a default.

```cpp
#include <expected>

enum class WorkerError { non_positive };

[[nodiscard]] std::expected<unsigned, WorkerError> worker_count(int value) {
    if (value <= 0) {
        return std::unexpected(WorkerError::non_positive);
    }
    return static_cast<unsigned>(value);
}
```

## Checklist

- [ ] Features work across the project's compiler, library, standard, and target matrix.
- [ ] Resources, borrowed lifetimes, moves, error paths, and synchronization have explicit contracts.
- [ ] No silent defaults, unsafe narrowing, or unnecessary ownership/architecture expansion.
- [ ] When authorized, use project formatting, warnings, focused tests, and applicable sanitizers. Check invalid input and lifetime boundaries; report checks skipped.

## References

- [Published standard](https://isocpp.org/std/the-standard), [GCC support](https://gcc.gnu.org/projects/cxx-status.html), and [Clang support](https://clang.llvm.org/cxx_status.html).
- [C++26 completion trip report (March 2026)](https://herbsutter.com/2026/03/).
- [C++ Core Guidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines).
- Working-draft contracts: [expected](https://eel.is/c++draft/expected), [moved-from library objects](https://eel.is/c++draft/lib.types.movedfrom), [format](https://eel.is/c++draft/format), and [jthread](https://eel.is/c++draft/thread.jthread.class). Check the selected standard before using draft additions.
- [CMake target model](https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html).
Files in this package (2)
  • SKILL.md 7 KB
  • validate 1 KB

Versions

The database has no other skills named programming-cpp.

Database snapshot: 13.11.2026 · package version 71665549b4b2 (SHA-256 digest of the entire package)

Downloads in the last 30 days: 0 · ranking

Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 90cpp-patterns
    Modern C++ patterns and best practices for readable, safe, maintainable C++ code: RAII, ownership, error handling, API design, concurrency basics, and build/tooling hygiene. Use when writing or reviewing C++ (C++20+) code.
  • 100cpp-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,…
  • 95lang-cpp
    Use this skill when the user says 'C++', 'cpp', 'modern C++', 'C++20', 'C++23', 'RAII', 'smart pointers', 'move semantics', 'templates', 'concepts', 'CMake', 'undefined behavior C++', 'rule of five', 'std::span', 'coroutines'. Writes and reviews modern C++ (C++17/20/23):…
  • 90cpp-raii
    Manage every resource through RAII with the rule of zero, correct smart-pointer selection, and move semantics. Use when writing modern C++ or reviewing code with manual new/delete or leaky error paths.
  • 100cpp-modern-cpp20-raii
    Use this skill when writing modern C++20 software using concepts, smart pointers, RAII idioms, and std::span for zero leaks.
  • 91cpp-smart-pointers
    Use when managing memory safely in C++ with smart pointers including unique_ptr, shared_ptr, weak_ptr, and RAII patterns.

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.