Home › C++ › Modern C++ with CMake rules

Modern C++ with CMake rules

Cursor rules for modern C++ (C++20/23) projects built with CMake presets: RAII, ownership, warnings, and tests.

CursorC++

How to use

Save this as .cursor/rules/cpp.mdc in your repository root, commit it, and your agent will pick it up automatically. Edit the commands to match your project.

.cursor/rules/cpp.mdcDownload  
---
description: Modern C++ with CMake conventions
globs: ["**/*.{cpp,hpp,h,cc,cxx}","**/CMakeLists.txt","CMakePresets.json"]
alwaysApply: false
---

# Modern C++ (C++20/23) + CMake

- Configure/build with presets: `cmake --preset dev && cmake --build --preset dev`; test with `ctest --preset dev`. Sanitizer preset (`-fsanitize=address,undefined`) for debugging.
- Targets, not globals: `target_link_libraries`, `target_include_directories`, `target_compile_features(x PUBLIC cxx_std_20)`. No `include_directories` or `link_libraries` at directory scope.
- Dependencies via the project's manager (vcpkg manifest, Conan, or `FetchContent` with pinned tags).
- Warnings on and treated as errors in CI (`-Wall -Wextra -Wpedantic -Werror` / `/W4 /WX`).

## Code
- RAII for every resource; no naked `new`/`delete`. Ownership via `std::unique_ptr` (default) or `std::shared_ptr` (only when sharing is real); non-owning access via references, `std::span`, `std::string_view`.
- Rule of zero: let members manage resources; if you write one special member, think about all five.
- `const` and `constexpr` by default; `[[nodiscard]]` on functions whose results matter.
- Use `std::optional`, `std::variant`, `std::expected` (C++23) for absent values and errors when the codebase doesn't use exceptions.
- Prefer algorithms and ranges (`std::ranges::sort`) over hand-written loops when clearer.
- No C-style casts, no `using namespace std;` in headers, `#pragma once` or include guards in every header.
- Format with `clang-format`, lint with `clang-tidy` using the repo's config.

## Done when
Build with zero warnings, `ctest` passes, and the sanitizer build is clean for the changed code.

More: all C++ rules · all Cursor rules

Related rules

Arduino / PlatformIO rules

Cursor rules for microcontroller projects with PlatformIO and the Arduino framework: non-blocking loops, memory, pins.

CursorEmbedded / firmware

Embedded C (bare metal / RTOS) AGENTS.md

AGENTS.md for embedded C firmware (bare metal, Zephyr, FreeRTOS): safety-minded C rules, memory, and hardware access.

AGENTS.mdEmbedded / firmware

ESP32 ESP-IDF firmware CLAUDE.md

CLAUDE.md for ESP32 firmware on ESP-IDF 5.x in C: components, FreeRTOS tasks, error handling, and logging.

Claude Code (CLAUDE.md)Embedded / firmware

Solidity + Foundry Copilot path instructions

Path-specific Copilot instructions for Solidity 0.8.x contracts with Foundry: security patterns, fuzz tests, and gas.

GitHub CopilotSolidity / smart contracts

Solidity smart contract security CLAUDE.md

CLAUDE.md for reviewing and writing Solidity contracts safely: threat checklist, Foundry tests, and no-deploy rules.

Claude Code (CLAUDE.md)Solidity / smart contracts

Zig AGENTS.md

AGENTS.md for Zig 0.15 projects: build.zig, explicit allocators, error unions, defer cleanup, and zig test.

AGENTS.mdZig

Get new rules weekly

One email a week: fresh rule files, agent tips, and what's changing in Cursor, Claude Code and Copilot. Free, unsubscribe anytime.

Opens the Rulestack Newsletter on beehiiv in a new tab. Or subscribe directly on beehiiv.