C/C++ Cursor Rules: Systems Programming Configuration

Cursor rules for C and C++ development covering memory management, STL usage, RAII patterns, and modern C++ standards for systems-level programming.

August 19, 2024by PromptGenius Team
c++ccursor-rulessystems-programmingmemory-management
C/C++ Cursor Rules: Systems Programming Configuration

Overview

C and C++ are the backbone of performance-critical applications, from operating systems to game engines and embedded devices. These cursor rules enforce modern C++17+ standards, RAII (Resource Acquisition Is Initialization) patterns, and smart pointer usage to help AI assistants generate memory-safe, efficient code. Whether you're building a low-latency trading system or an embedded controller, these rules ensure your codebase remains maintainable while maximizing bare-metal performance.

Note:

Enforces C++17+ features, smart pointer usage, constexpr patterns, and STL best practices for production-ready systems programming.

Key C++ Patterns

RAII (Resource Acquisition Is Initialization)

class FileHandle {
  FILE* file;
public:
  FileHandle(const char* filename, const char* mode) {
    file = fopen(filename, mode);
    if (!file) throw std::runtime_error("Failed to open file");
  }
  ~FileHandle() { if (file) fclose(file); }
  FileHandle(const FileHandle&) = delete;
  FileHandle& operator=(const FileHandle&) = delete;
  FileHandle(FileHandle&& other) noexcept : file(other.file) {
    other.file = nullptr;
  }
};

Move Semantics

class Buffer {
  std::vector<uint8_t> data;
public:
  Buffer(size_t size) : data(size) {}
  Buffer(Buffer&& other) noexcept : data(std::move(other.data)) {}
  Buffer& operator=(Buffer&& other) noexcept {
    if (this != &other) data = std::move(other.data);
    return *this;
  }
};

Template Metaprogramming

template<typename T>
concept Numeric = std::is_arithmetic_v<T>;

template<Numeric T>
T safe_average(const std::vector<T>& values) {
  if (values.empty()) return T{};
  T sum{};
  for (const auto& v : values) sum += v;
  return sum / static_cast<T>(values.size());
}

Rules Configuration

---
description: Enforces best practices for C/C++ development, focusing on modern standards, RAII, smart pointers, and STL patterns. Provides comprehensive guidelines for writing memory-safe, performant systems code with proper context.
globs: **/*.{c,cpp,h,hpp}
---
# C/C++ Best Practices

You are an expert in C/C++ development and systems programming.
You understand modern C/C++ development practices, memory management patterns, and the importance of providing complete context in code generation.

### Code Style
- 4 spaces indentation
- snake_case for functions/variables
- PascalCase for types/classes
- SCREAMING_SNAKE_CASE for macros
- 100 char line limit

### Best Practices
- Use modern C++ (C++17+)
- Prefer smart pointers over raw pointers
- Use RAII for resource management
- Use std::vector over raw arrays
- Use std::optional for nullable values
- Prefer constexpr for compile-time

### AI Instructions
- Include necessary headers
- Use forward declarations when possible
- Provide CMakeLists.txt for build
- Add Google Test or Catch2 tests
- Comment complex logic

Installation

Create cpp.mdc in your project's .cursor/rules/ directory and paste the configuration above. Cursor and Windsurf both read .cursor/rules/ — Copilot users place it in .github/copilot-instructions.md instead.

Examples

// smart_ptr_example.cpp — RAII with smart pointers and modern C++
#include <memory>
#include <vector>

class Widget {
    std::unique_ptr<Resource> resource_;
public:
    explicit Widget(std::unique_ptr<Resource> res)
        : resource_(std::move(res)) {}

    void process() {
        if (resource_) resource_->execute();
    }
};

// Factory function returning smart pointer
auto make_widget() -> std::unique_ptr<Widget> {
    auto res = std::make_unique<Resource>();
    return std::make_unique<Widget>(std::move(res));
}