cpp-pro
Write idiomatic C++ code with modern features, RAII, smart pointers, and STL algorithms. Handles templates, move semantics, and performance optimization.
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
Manual Installation
- Download SKILL.md from GitHub
- Place it in
.claude/skills/cpp-pro/SKILL.mdinside your project - Restart your AI agent — it will auto-discover the skill
How cpp-pro Compares
| Feature / Agent | cpp-pro | Standard Approach |
|---|---|---|
| Platform Support | Claude | Limited / Varies |
| Context Awareness | High | Baseline |
| Installation Complexity | easy | N/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
AI Agents for Coding
Browse AI agent skills for coding, debugging, testing, refactoring, code review, and developer workflows across Claude, Cursor, and Codex.
Best AI Skills for Claude
Explore the best AI skills for Claude and Claude Code across coding, research, workflow automation, documentation, and agent operations.
ChatGPT vs Claude for Agent Skills
Compare ChatGPT and Claude for AI agent skills across coding, writing, research, and reusable workflow execution.
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
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.
javascript-pro
Master modern JavaScript with ES6+, async patterns, and Node.js APIs. Handles promises, event loops, and browser/Node compatibility.
java-pro
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.
haskell-pro
Expert Haskell engineer specializing in advanced type systems, pure
c-pro
Write efficient C code with proper memory management, pointer
nft-standards
Master ERC-721 and ERC-1155 NFT standards, metadata best practices, and advanced NFT features.
nextjs-app-router-patterns
Comprehensive patterns for Next.js 14+ App Router architecture, Server Components, and modern full-stack React development.
new-rails-project
Create a new Rails project
networkx
NetworkX is a Python package for creating, manipulating, and analyzing complex networks and graphs.
network-engineer
Expert network engineer specializing in modern cloud networking, security architectures, and performance optimization.
nestjs-expert
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.
nerdzao-elite
Senior Elite Software Engineer (15+) and Senior Product Designer. Full workflow with planning, architecture, TDD, clean code, and pixel-perfect UX validation.