cpp – skill Lập trình cho agent AI
Hướng dẫn super-code riêng cho ngôn ngữ cpp.
Cài skill cpp bằng một lệnh
Chọn công cụ bạn đang dùng, copy lệnh và dán vào terminal. Skill sẽ nằm trong thư mục skills của công cụ đó và sẵn sàng dùng ở phiên làm việc tiếp theo.
Claude Code
mkdir -p ~/.claude/skills && curl -fsSL https://skill.dn1nguoi.com/z/cpp-442b5.tgz | tar xz -C ~/.claude/skills
Gọi trong Claude Code: /cpp – rồi mô tả công việc bạn muốn làm.
Codex
mkdir -p ~/.codex/skills && curl -fsSL https://skill.dn1nguoi.com/z/cpp-442b5.tgz | tar xz -C ~/.codex/skills
Gọi trong Codex: $cpp – rồi mô tả công việc bạn muốn làm.
Hermes Agent
mkdir -p ~/.hermes/skills && curl -fsSL https://skill.dn1nguoi.com/z/cpp-442b5.tgz | tar xz -C ~/.hermes/skills
Gọi trong Hermes Agent: /cpp – rồi mô tả công việc bạn muốn làm.
DeepSeek
mkdir -p ~/.deepcode/skills && curl -fsSL https://skill.dn1nguoi.com/z/cpp-442b5.tgz | tar xz -C ~/.deepcode/skills
Gọi trong DeepSeek: /cpp – rồi mô tả công việc bạn muốn làm.
Nội dung SKILL.md
Nội dung SKILL.md gốc bằng tiếng Anh.
C++: Idiomatic Efficiency Reference
Apply only within the requested scope and current project conventions. Preserve behavior, ownership, error handling, accessibility and security; examples are independent fragments, not equivalent rewrites in every context. Verify language/library versions and run relevant checks. Required tests and user-facing progress/results take precedence over brevity rules. Do not install dependencies or delete files merely to follow these examples.
When to Use
- Use this skill when the task matches this description: Language-specific super-code guidelines for cpp.
Table of Contents
1. [Memory & Ownership](#memory)
2. [Modern Types & Containers](#types)
3. [Move Semantics & References](#move)
4. [Templates & Concepts](#templates)
5. [Error Handling](#errors)
6. [Concurrency](#concurrency)
7. [Anti-patterns specific to C++](#antipatterns)
---
1. Memory & Ownership {#memory}
// ❌ Raw new/delete
Widget* w = new Widget();
// ... 15 lines later ...
delete w;
// ✅
auto w = std::make_unique<Widget>();// ❌ Shared ownership when unique suffices
auto w = std::make_shared<Widget>();
transfer(w); // only one owner
// ✅ — unique_ptr; move when transferring
auto w = std::make_unique<Widget>();
transfer(std::move(w));// ❌ new[] for dynamic arrays
int* arr = new int[n];
// ... use ...
delete[] arr;
// ✅
std::vector<int> arr(n);// ❌ Manual RAII wrapper for file/mutex
FILE* f = fopen(path, "r");
// ... must remember fclose ...
// ✅
std::ifstream f(path);
// closes automatically at scope exit
// For non-standard resources: use unique_ptr with custom deleter
auto f = std::unique_ptr<FILE, decltype(&fclose)>(fopen(path, "r"), fclose);Rule: if you type new, you almost certainly want make_unique or make_shared.
---
2. Modern Types & Containers {#types}
// ❌ C-style string manipulation
char buf[256];
sprintf(buf, "%s:%d", host, port);
// ✅
auto addr = std::format("{}:{}", host, port); // C++20
// or: auto addr = host + ":" + std::to_string(port);// ❌ out-parameter for multiple returns
void compute(int input, int& result, std::string& error);
// ✅
struct ComputeResult { int value; std::string error; };
ComputeResult compute(int input);
// or: std::pair / std::tuple with structured bindings
auto [value, error] = compute(input);// ❌ Manual loop to find element
int idx = -1;
for (int i = 0; i < vec.size(); i++) {
if (vec[i] == target) { idx = i; break; }
}
// ✅
auto it = std::ranges::find(vec, target); // C++20
// or: std::find(vec.begin(), vec.end(), target);// ❌ Checking .find() != .end() then accessing
auto it = map.find(key);
if (it != map.end()) { use(it->second); }
// ✅ — one lookup when the value is needed
if (auto it = map.find(key); it != map.end()) { use(it->second); }
// or keep iterator version when you need the value without double lookupUse std::string_view for function parameters that don't need ownership.
---
3. Move Semantics & References {#move}
// ❌ Copying a large container into a function
void process(std::vector<Data> items) { ... } // copies on call
// ✅ — const ref for read, move for sink
void process(const std::vector<Data>& items) { ... } // read-only
void consume(std::vector<Data> items) { ... } // sink: caller moves in// ❌ std::move on const object (silently copies)
const std::string s = "hello";
take(std::move(s)); // still copies
// ✅ — don't const things you intend to move
std::string s = "hello";
take(std::move(s));// ❌ Returning std::move from local (prevents NRVO)
std::vector<int> build() {
std::vector<int> v;
// ... fill ...
return std::move(v); // pessimization
// ✅ — just return the local; compiler applies NRVO or implicit move
return v;
}---
4. Templates & Concepts {#templates}
// ❌ SFINAE soup
template<typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
T square(T x) { return x * x; }
// ✅ (C++20 concepts)
template<std::integral T>
T square(T x) { return x * x; }// ❌ Template for one type
template<typename T>
void log(T msg) { std::cout << msg; }
// Only ever called with std::string
// ✅ — don't templatize unless you need multiple types
void log(std::string_view msg) { std::cout << msg; }Concepts make template errors readable — prefer them over SFINAE and static_assert.
---
5. Error Handling {#errors}
// ❌ Error codes via int returns (C-style in C++)
int parse(const std::string& input, Data& out);
// ✅ — std::expected (C++23) or exceptions
std::expected<Data, ParseError> parse(const std::string& input);
// or throw for exceptional conditions
Data parse(const std::string& input); // throws ParseError// ❌ Catching by value (slices derived exceptions)
try { ... }
catch (std::exception e) { ... }
// ✅
catch (const std::exception& e) { ... }// ❌ Exception in destructor
~MyClass() {
if (cleanup() < 0) throw CleanupError(); // terminates
// ✅ — destructors must be noexcept; log/swallow errors
~MyClass() noexcept {
if (cleanup() < 0) log_error("cleanup failed");
}---
6. Concurrency {#concurrency}
// ❌ Manual thread + join tracking
std::thread t(work);
// ... must remember t.join() ...
// ✅ (C++20)
std::jthread t(work); // auto-joins on destruction// ❌ Lock/unlock manually
mtx.lock();
data.push_back(item);
mtx.unlock(); // missed on exception
// ✅
{
std::scoped_lock lock(mtx);
data.push_back(item);
}// ❌ Polling a shared bool for completion
while (!done.load()) { std::this_thread::sleep_for(10ms); }
// ✅ — use std::future or condition_variable
auto future = std::async(std::launch::async, compute);
auto result = future.get();Use std::scoped_lock over lock_guard — it handles multiple mutexes and avoids deadlock.
---
7. Anti-patterns specific to C++ {#antipatterns}
| Anti-pattern | Preferred |
|---|---|
| Raw new/delete | make_unique / make_shared |
| (Type)expr C-style cast | static_cast<Type>(expr) |
| #define constants | constexpr variables |
| NULL | nullptr |
| using namespace std; in headers | explicit std:: prefix |
| Manual loop for transform/filter | std::ranges or <algorithm> |
| std::endl | '\n' (endl flushes — slow) |
| char* for string parameters | std::string_view |
| Exception specification throw() | noexcept |
| Inheriting from std:: containers | composition, not inheritance |
| volatile for thread synchronization | std::atomic |
| Header-only mega-templates | separate declaration/definition where compile time matters |
Limitations
- These are language-specific guidelines and do not cover overall architectural decisions.
- Over-compression might reduce readability; apply judgement.
Skill liên quan
- create-branch – Tạo nhánh git theo quy ước đặt tên của Sentry. Dùng khi được yêu cầu "create a branch"…
- requesting-code-review – Review trước khi commit: quét bảo mật, cổng chất lượng và tự động sửa lỗi.
- rowan – Rowan là nền tảng cloud-native cho quy trình mô hình hóa phân tử và hóa dược, có Python…
- crossframe-debate – Dùng khi CrossFrame Suite định tuyến công việc kiểm tra mệnh đề, phân tích tranh luận…
- rest-graphql-debug – Gỡ lỗi API REST/GraphQL: mã trạng thái, xác thực, schema, tái hiện lỗi.
- crossframe-review – Dùng khi đầu ra CrossFrame rõ ràng cần rà soát về độ trung thực lập luận, ranh giới bằng…
- csharp – Hướng dẫn super-code theo ngôn ngữ dành riêng cho csharp.
- csharp-pro – Viết code C# hiện đại với các tính năng nâng cao như record, pattern matching và…