cpp-pro

Write idiomatic C++ code with modern features, RAII, smart pointers, and STL algorithms. Handles templates, move semantics, and performance optimization.

31,392 stars
Complexity: easy

About this skill

This skill transforms an AI agent into a proficient C++ programming expert. It enables the agent to generate idiomatic and high-quality C++ code, adhering to modern best practices. Key areas of expertise include Resource Acquisition Is Initialization (RAII), effective use of smart pointers, leveraging the Standard Template Library (STL), handling templates, implementing move semantics, and applying performance optimization techniques. The skill guides the agent to clarify requirements, apply relevant C++ best practices, and provide actionable, verifiable code solutions, making it ideal for professional C++ development tasks. It helps an agent act as a C++ programming expert, providing guidance, best practices, and checklists for complex C++ workflows.

Best use case

Developing new C++ applications, refactoring existing C++ codebases, seeking expert guidance on modern C++ best practices, optimizing C++ performance, or implementing advanced C++ features like templates and move semantics.

Write idiomatic C++ code with modern features, RAII, smart pointers, and STL algorithms. Handles templates, move semantics, and performance optimization.

Well-structured, idiomatic, and modern C++ code snippets or complete solutions; clear explanations of C++ concepts; best practice recommendations; performance optimization suggestions; or detailed checklists for C++ development tasks.

Practical example

Example input

Design a C++ class for a thread-safe queue. Ensure it uses modern C++ features, RAII for resource management, and is efficient for producer-consumer scenarios.

Example output

```cpp
#include <queue>
#include <mutex>
#include <condition_variable>
#include <memory>
#include <utility> // For std::move

template <typename T>
class ThreadSafeQueue {
public:
    // Pushes an element into the queue. Uses move semantics for efficiency.
    void push(T value) {
        std::lock_guard<std::mutex> lock(mtx_); // RAII: mutex automatically locked/unlocked
        queue_.push(std::move(value));
        cv_.notify_one(); // Notify one waiting consumer
    }

    // Pops an element from the queue. Blocks if the queue is empty.
    T pop() {
        std::unique_lock<std::mutex> lock(mtx_); // RAII: mutex locked, can be explicitly unlocked
        cv_.wait(lock, [this] { return !queue_.empty(); }); // Wait until queue is not empty
        T value = std::move(queue_.front()); // Use move semantics
        queue_.pop();
        return value;
    }

    // Tries to pop an element without blocking. Returns true on success, false otherwise.
    bool try_pop(T& value) {
        std::lock_guard<std::mutex> lock(mtx_);
        if (queue_.empty()) {
            return false;
        }
        value = std::move(queue_.front());
        queue_.pop();
        return true;
    }

    // Checks if the queue is empty.
    bool empty() const {
        std::lock_guard<std::mutex> lock(mtx_);
        return queue_.empty();
    }

    // Returns the number of elements in the queue.
    size_t size() const {
        std::lock_guard<std::mutex> lock(mtx_);
        return queue_.size();
    }

private:
    std::queue<T> queue_;
    mutable std::mutex mtx_; // Mutex for protecting shared data
    std::condition_variable cv_; // For signaling between producers and consumers
};

// Example Usage (for demonstration, not part of the class):
/*
#include <iostream>
#include <thread>

void producer(ThreadSafeQueue<int>& q) {
    for (int i = 0; i < 10; ++i) {
        q.push(i);
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
}

void consumer(ThreadSafeQueue<int>& q) {
    for (int i = 0; i < 10; ++i) {
        int value = q.pop();
        std::cout << "Consumed: " << value << std::endl;
    }
}

int main() {
    ThreadSafeQueue<int> myQueue;
    std::thread p(producer, std::ref(myQueue));
    std::thread c(consumer, std::ref(myQueue));

    p.join();
    c.join();

    return 0;
}
*/
```

**Explanation of Modern C++ Features and Best Practices Used:**
*   **RAII (Resource Acquisition Is Initialization):** Employed through `std::lock_guard<std::mutex>` and `std::unique_lock<std::mutex>`. These objects automatically acquire (lock) the mutex upon construction and release (unlock) it upon destruction, preventing deadlocks and ensuring mutex integrity even if exceptions occur.
*   **Smart Pointers (Implicit):** While `std::unique_ptr` or `std::shared_ptr` are not directly used in the queue's core logic here (as `T` is a generic type), the concept of managing resources (like mutexes) automatically aligns with smart pointer philosophy. If `T` were a pointer type, smart pointers would be highly recommended.
*   **STL Algorithms and Containers:** `std::queue` is used as the underlying container for its FIFO behavior. `std::mutex` and `std::condition_variable` are standard library components for thread synchronization.
*   **Templates:** The `ThreadSafeQueue` is a class template (`template <typename T>`), allowing it to store any data type, promoting code reusability.
*   **Move Semantics:** `std::move` is explicitly used when `push`ing and `pop`ping elements. This avoids expensive copy operations for complex objects, instead transferring ownership and improving performance significantly in many scenarios.
*   **Performance Optimization:** The use of `std::condition_variable` is crucial for efficiency. Instead of busy-waiting (repeatedly checking if the queue is empty), consumer threads block (`cv_.wait`) until a producer signals (`cv_.notify_one()`) that data is available, conserving CPU cycles.
*   **Concurrency Primitives:** `std::mutex` and `std::condition_variable` provide robust mechanisms for protecting shared data (`queue_`) from race conditions and for efficient thread communication.
This implementation adheres to modern C++ standards, providing a robust and efficient thread-safe queue.

When to use this skill

  • When working on C++ programming tasks or workflows that require professional-grade code.
  • When needing guidance, best practices, or checklists for modern C++ development.
  • When generating idiomatic C++ code that utilizes features like RAII, smart pointers, and STL algorithms.
  • When implementing or understanding advanced C++ concepts such as templates and move semantics.

When not to use this skill

  • When the task is unrelated to C++ programming.
  • When you need a different programming language or domain tool outside the scope of C++.
  • When the task requires expertise in non-C++ specific software development or theoretical computer science topics.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/cpp-pro/SKILL.md --create-dirs "https://raw.githubusercontent.com/sickn33/antigravity-awesome-skills/main/plugins/antigravity-awesome-skills-claude/skills/cpp-pro/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/cpp-pro/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How cpp-pro Compares

Feature / Agentcpp-proStandard Approach
Platform SupportClaudeLimited / Varies
Context Awareness High Baseline
Installation ComplexityeasyN/A

Frequently Asked Questions

What does this skill do?

Write idiomatic C++ code with modern features, RAII, smart pointers, and STL algorithms. Handles templates, move semantics, and performance optimization.

Which AI agents support this skill?

This skill is designed for Claude.

How difficult is it to install?

The installation complexity is rated as easy. You can find the installation instructions above.

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

Related Guides

SKILL.md Source

## Use this skill when

- Working on cpp pro tasks or workflows
- Needing guidance, best practices, or checklists for cpp pro

## Do not use this skill when

- The task is unrelated to cpp pro
- You need a different domain or tool outside this scope

## Instructions

- Clarify goals, constraints, and required inputs.
- Apply relevant best practices and validate outcomes.
- Provide actionable steps and verification.
- If detailed examples are required, open `resources/implementation-playbook.md`.

You are a C++ programming expert specializing in modern C++ and high-performance software.

## Focus Areas

- Modern C++ (C++11/14/17/20/23) features
- RAII and smart pointers (unique_ptr, shared_ptr)
- Template metaprogramming and concepts
- Move semantics and perfect forwarding
- STL algorithms and containers
- Concurrency with std::thread and atomics
- Exception safety guarantees

## Approach

1. Prefer stack allocation and RAII over manual memory management
2. Use smart pointers when heap allocation is necessary
3. Follow the Rule of Zero/Three/Five
4. Use const correctness and constexpr where applicable
5. Leverage STL algorithms over raw loops
6. Profile with tools like perf and VTune

## Output

- Modern C++ code following best practices
- CMakeLists.txt with appropriate C++ standard
- Header files with proper include guards or #pragma once
- Unit tests using Google Test or Catch2
- AddressSanitizer/ThreadSanitizer clean output
- Performance benchmarks using Google Benchmark
- Clear documentation of template interfaces

Follow C++ Core Guidelines. Prefer compile-time errors over runtime errors.

Related Skills

n8n-code-javascript

31392
from sickn33/antigravity-awesome-skills

Write JavaScript code in n8n Code nodes. Use when writing JavaScript in n8n, using $input/$json/$node syntax, making HTTP requests with $helpers, working with dates using DateTime, troubleshooting Code node errors, or choosing between Code node modes.

Programming & DevelopmentClaude

javascript-pro

31392
from sickn33/antigravity-awesome-skills

Master modern JavaScript with ES6+, async patterns, and Node.js APIs. Handles promises, event loops, and browser/Node compatibility.

Programming & DevelopmentClaude

java-pro

31392
from sickn33/antigravity-awesome-skills

Master Java 21+ with modern features like virtual threads, pattern matching, and Spring Boot 3.x. Expert in the latest Java ecosystem including GraalVM, Project Loom, and cloud-native patterns.

Programming & DevelopmentClaude

haskell-pro

31392
from sickn33/antigravity-awesome-skills

Expert Haskell engineer specializing in advanced type systems, pure

Programming & DevelopmentClaude

c-pro

31392
from sickn33/antigravity-awesome-skills

Write efficient C code with proper memory management, pointer

Programming & DevelopmentClaude

nft-standards

31392
from sickn33/antigravity-awesome-skills

Master ERC-721 and ERC-1155 NFT standards, metadata best practices, and advanced NFT features.

Web3 & BlockchainClaude

nextjs-app-router-patterns

31392
from sickn33/antigravity-awesome-skills

Comprehensive patterns for Next.js 14+ App Router architecture, Server Components, and modern full-stack React development.

Web FrameworksClaude

new-rails-project

31392
from sickn33/antigravity-awesome-skills

Create a new Rails project

Code GenerationClaude

networkx

31392
from sickn33/antigravity-awesome-skills

NetworkX is a Python package for creating, manipulating, and analyzing complex networks and graphs.

Network AnalysisClaude

network-engineer

31392
from sickn33/antigravity-awesome-skills

Expert network engineer specializing in modern cloud networking, security architectures, and performance optimization.

Network EngineeringClaude

nestjs-expert

31392
from sickn33/antigravity-awesome-skills

You are an expert in Nest.js with deep knowledge of enterprise-grade Node.js application architecture, dependency injection patterns, decorators, middleware, guards, interceptors, pipes, testing strategies, database integration, and authentication systems.

Frameworks & LibrariesClaude

nerdzao-elite

31392
from sickn33/antigravity-awesome-skills

Senior Elite Software Engineer (15+) and Senior Product Designer. Full workflow with planning, architecture, TDD, clean code, and pixel-perfect UX validation.

Software DevelopmentClaude