Chuyển đến nội dung
Skillpedia
Lập trình chung8.0k · #12684

modern-cpp

Skill này làm gì

Hướng dẫn code C++ theo các chuẩn hiện đại, từ C++20 đến C++26, với trọng tâm là an toàn. Dành cho lập trình viên viết code C++ mới hoặc cập nhật code cũ.

Khi nào nên dùng

  • ✓Khi lập trình viên viết hàm, lớp hoặc thư viện C++ mới.
  • ✓Khi lập trình viên muốn cập nhật code C++ cũ sang các mẫu hiện đại.
  • ✓Khi lập trình viên làm việc với C++ quan trọng về bảo mật và muốn các mẫu an toàn hơn.

Liên kết skill

https://github.com/trailofbits/skills/blob/main/plugins/modern-cpp/skills/modern-cpp/SKILL.md

Sao 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

Hãy dùng skill này để giúp tôi: https://github.com/trailofbits/skills/blob/main/plugins/modern-cpp/skills/modern-cpp/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Cập nhật lớp C++ cũ này để dùng smart pointer và xử lý lỗi hiện đại.”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Skill thay các mẫu rủi ro, như new và delete thô, mảng kiểu C, printf và ép kiểu kiểu C, bằng lựa chọn hiện đại an toàn hơn, và giải thích vì sao mỗi thay đổi giúp ích. Nó tôn trọng chuẩn cũ hơn nếu dự án yêu cầu.

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.

Modern C++

Guide for writing modern C++ using C++20, C++23, and C++26 idioms. Focuses on patterns that eliminate vulnerability classes and reduce boilerplate, with a security emphasis from Trail of Bits.

When to Use This Skill

  • Writing new C++ functions, classes, or libraries
  • Modernizing existing C++ code (pre-C++20 patterns)
  • Choosing between legacy and modern approaches
  • Working on security-critical or safety-sensitive C++
  • Reviewing C++ code for modern idiom adoption

When NOT to Use This Skill

  • User explicitly requires older standard: Respect constraints (embedded, legacy ABI)
  • Pure C code: This skill is C++-specific
  • Build system questions: CMake, Meson, Bazel configuration is out of scope
  • Non-C++ projects: Mixed codebases where C++ isn't primary

Anti-Patterns to Avoid

Avoid Use Instead Why
new/delete std::make_unique, std::make_shared Eliminates leaks, double-free
Raw owning pointers std::unique_ptr, std::shared_ptr RAII ownership semantics
C arrays (int arr[N]) std::array<int, N> Bounds-aware, value semantics
Pointer + length params std::span<T> Non-owning, bounds-checkable
printf / sprintf std::format, std::print Type-safe, no buffer overflow
C-style casts (int)x static_cast<int>(x) Explicit intent, auditable
#define constants constexpr variables Scoped, typed, debuggable
SFINAE / enable_if Concepts + requires Readable constraints and errors
Error codes + out params std::expected<T, E> Composable, type-safe errors
union std::variant Type-safe, no silent UB
Raw mutex.lock()/unlock() std::scoped_lock Exception-safe, no deadlocks
std::thread std::jthread Auto-join, stop token support
assert() macro contract_assert (C++26) Visible to tooling, configurable
Manual CRTP Deducing this (C++23) Simpler, no template boilerplate
Macro code generation Reflection (C++26) Zero-overhead, composable

See anti-patterns.md for the full table (30+ patterns).

Decision Tree

What are you doing?
|
+-- Writing new C++ code?
|   +-- Use modern idioms by default (C++20/23)
|   +-- Choose the newest standard your compiler supports
|   +-- See Feature Tiers below
|
+-- Modernizing existing code?
|   +-- Start with Tier 1 (C++20/23) replacements
|   +-- Prioritize by security impact (memory > types > style)
|   +-- See anti-patterns.md for the migration table
|
+-- Security-critical code?
|   +-- Enable compiler hardening flags (see below)
|   +-- Enable hardened libc++ mode
|   +-- Run sanitizers in CI
|   +-- See safe-idioms.md and compiler-hardening.md
|
+-- Using C++26 features?
    +-- Reflection: YES, plan for it (GCC 16+)
    +-- Contracts: cautiously, for new API boundaries
    +-- std::execution: wait for ecosystem maturity
    +-- See cpp26-features.md

Feature Tiers

Features are ranked by practical usability today, not by standard version.

Tier 1: Use Today (C++20/23, solid compiler support)

Feature Replaces Standard
Concepts + requires SFINAE, enable_if C++20
Ranges + views Raw iterator loops C++20
std::span<T> Pointer + length C++20
std::format sprintf, iostream chains C++20
Three-way comparison <=> Manual comparison operators C++20
std::jthread std::thread + manual join C++20
Designated initializers Positional struct init C++20
std::expected<T,E> Error codes, exceptions at boundaries C++23
std::print / std::println printf, std::cout << C++23
Deducing this CRTP, const/non-const duplication C++23
std::flat_map std::map for read-heavy use C++23
Monadic std::optional Nested if-checks on optionals C++23

See cpp20-features.md and cpp23-features.md.

Tier 2: Deploy Now (no standard bump needed)

These improve safety without changing your C++ standard version:

  • Compiler hardening flags — -D_FORTIFY_SOURCE=3, -fstack-protector-strong, -ftrivial-auto-var-init=zero
  • Hardened libc++ — -D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_FAST for ~0.3% overhead bounds-checking
  • Sanitizers in CI — ASan + UBSan as minimum; TSan for concurrent code
  • Warning flags
Câu hỏi thường gặp

Nó thay thế những gì?

Nó thay các mẫu như new và delete thô, mảng C, printf và ép kiểu kiểu C bằng các lựa chọn hiện đại an toàn hơn.

Nó có bao phủ CMake không?

Không. Việc thiết lập hệ thống build nằm ngoài phạm vi.

Nếu dự án của tôi phải dùng chuẩn cũ hơn thì sao?

Skill tôn trọng điều đó và giữ đúng chuẩn mà dự án yêu cầu.

Chi tiết

Mô tả gốc của tác giả

Guides C++ code toward modern idioms (C++20/23/26). Use when writing new C++ code, modernizing legacy patterns, or working on security-critical C++. Replaces raw pointers with smart pointers, SFINAE with concepts, printf with std::print, error codes with std::expected.

repo: trailofbits/skills

Độ phổ biến

Sao GitHub
8.0k
Vị trí xếp hạng
#12684
Giấy phép
CC-BY-SA-4.0
Cập nhật
2/11/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự