cpp
Skill này làm gì
Hướng dẫn này giúp bạn viết code C++ hiện đại hơn, an toàn hơn và hiệu quả hơn. Nó chỉ cách dùng các thực hành tốt nhất để quản lý bộ nhớ, kiểu dữ liệu, xử lý lỗi và làm việc với nhiều tiến trình đồng thời.
Khi nào nên dùng
- ✓Khi học C++ hiện đại.
- ✓Khi xem xét code C++ có sẵn.
- ✓Khi viết code C++ cần nhanh và an toàn.
Liên kết skill
https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/super-code/cpp/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Làm sao để quản lý bộ nhớ an toàn trong C++?”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Hướng dẫn so sánh các ví dụ code C++ “xấu” với ví dụ “tốt”. Nó giải thích vì sao cách tiếp cận hiện đại tốt hơn trong việc tránh lỗi thường gặp và cải thiện hiệu năng.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
C++: Idiomatic Efficiency Reference
When to Use
- Use this skill when the task matches this description: Language-specific super-code guidelines for cpp.
Table of Contents
- Memory & Ownership
- Modern Types & Containers
- Move Semantics & References
- Templates & Concepts
- Error Handling
- Concurrency
- Anti-patterns specific to C++
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); }
// ✅ (C++20)
if (map.contains(key)) { use(map[key]); }
// or keep iterator version when you need the value without double lookup
Use 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) { ... }
…
Câu hỏi thường gặp
“RAII” là gì?
RAII là viết tắt của “Resource Acquisition Is Initialization” (lấy tài nguyên là khởi tạo). Đây là kỹ thuật C++ trong đó tài nguyên (như bộ nhớ hoặc tệp) được quản lý tự động khi đối tượng được tạo và hủy. Ví dụ, một tệp được tự động đóng khi đối tượng đại diện cho nó ra khỏi phạm vi.
Vì sao dùng `std::vector` thay vì `new int[n]`?
`std::vector` tự quản lý bộ nhớ, giúp bạn không phải lo cấp phát và giải phóng bộ nhớ thủ công, vốn dễ gây lỗi. Nó cũng có nhiều tính năng hữu ích hơn.
“Move semantics” là gì?
Đây là cách tối ưu việc chuyển tài nguyên (như khối dữ liệu lớn) giữa các đối tượng. Thay vì sao chép mọi thứ, nó “chuyển” quyền sở hữu tài nguyên, giúp thao tác nhanh hơn nhiều.
Chi tiết
Mô tả gốc của tác giả
Language-specific super-code guidelines for cpp.
Độ phổ biến
- Sao GitHub
- 48k
- Vị trí xếp hạng
- #5888
- Giấy phép
- MIT
- Cập nhật
- 15/10/2026
Bình luận
- …
Thêm skill từ kho mã này
00-andruia-consultant
48kGiúp agent của bạn bắt đầu hoặc cải thiện các dự án AI. Phân tích mã, gợi ý công cụ và lập kế hoạch hành động bằng tiếng Tây Ban Nha.
007
48kCho phép agent của bạn thực hiện kiểm toán bảo mật và mô hình hóa mối đe dọa. Hữu ích để bảo vệ dự án khỏi các cuộc tấn công và lỗ hổng.
10-andruia-skill-smith
48kGiúp agent của bạn tạo hoặc cải thiện các công cụ AI. Xác định cách công cụ hoạt động, viết tài liệu và kiểm tra cấu trúc.
20-andruia-niche-intelligence
48kGiúp agent của bạn tạo bản tóm tắt về một ngành cụ thể. Hữu ích để hiểu bối cảnh, quy tắc, cách người dùng tương tác và rủi ro của dự án.
Skill tương tự
diffs
394kGiúp agent của bạn tạo một diff có thể chia sẻ về các chỉnh sửa, dưới dạng liên kết xem hoặc file ảnh, thay vì một bản tóm tắt viết tay.
coding-agent
394kGiúp agent của bạn giao một việc lập trình lớn cho Codex, Claude Code hoặc OpenCode chạy nền, rồi theo dõi tiến độ của nó.
gh-issues
394kGiúp agent của bạn tìm issue trên GitHub, chọn những issue cần xử lý và mở pull request với bản sửa, kể cả theo dõi nhận xét đánh giá.