cpp-coding-standards
できること
このガイドは、より安全で、モダンで、理解しやすいC++コードを書くのに役立ちます。コードの構成方法、変数や関数の名前の付け方、リソースの管理方法を提案し、よくあるミスを防ぎます。
使う場面
- ✓新しくC++のコードを書くとき。
- ✓既存のC++コードを確認または改善するとき。
- ✓C++プロジェクトの構造を決めるとき。
スキルのリンク
https://github.com/affaan-m/ECC/blob/main/pi/core/skills/cpp-coding-standards/SKILL.mdコピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。
使い方
いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。
貼り付け用メッセージ
Claude Code、Codex、Cursorの場合:プロジェクトのフォルダで実行するとスキルがインストールされます。
SKILL.mdをダウンロードして、エージェントのスキル用フォルダに入れてください。
AIへの頼み方の例
“C++のコードを安全で読みやすくするには、どうすればよいですか?”
毎週5つの新しいスキル。1分で読めます。
仕組み
このガイドには、ベストプラクティスに沿ってC++コードを書くための実用的なルールと例が載っています。わかりやすさ、型の安全性、正しいメモリ管理に重点を置いています。
技術的な内容(元のSKILL.md)
作者による元のテキスト(英語)をGitHubからそのまま表示しています。
C++ Coding Standards (C++ Core Guidelines)
Comprehensive coding standards for modern C++ (C++17/20/23) derived from the C++ Core Guidelines. Enforces type safety, resource safety, immutability, and clarity.
When to Use
- Writing new C++ code (classes, functions, templates)
- Reviewing or refactoring existing C++ code
- Making architectural decisions in C++ projects
- Enforcing consistent style across a C++ codebase
- Choosing between language features (e.g.,
enumvsenum class, raw pointer vs smart pointer)
When NOT to Use
- Non-C++ projects
- Legacy C codebases that cannot adopt modern C++ features
- Embedded/bare-metal contexts where specific guidelines conflict with hardware constraints (adapt selectively)
Cross-Cutting Principles
These themes recur across the entire guidelines and form the foundation:
- RAII everywhere (P.8, R.1, E.6, CP.20): Bind resource lifetime to object lifetime
- Immutability by default (P.10, Con.1-5, ES.25): Start with
const/constexpr; mutability is the exception - Type safety (P.4, I.4, ES.46-49, Enum.3): Use the type system to prevent errors at compile time
- Express intent (P.3, F.1, NL.1-2, T.10): Names, types, and concepts should communicate purpose
- Minimize complexity (F.2-3, ES.5, Per.4-5): Simple code is correct code
- Value semantics over pointer semantics (C.10, R.3-5, F.20, CP.31): Prefer returning by value and scoped objects
Philosophy & Interfaces (P., I.)
Key Rules
| Rule | Summary |
|---|---|
| P.1 | Express ideas directly in code |
| P.3 | Express intent |
| P.4 | Ideally, a program should be statically type safe |
| P.5 | Prefer compile-time checking to run-time checking |
| P.8 | Don't leak any resources |
| P.10 | Prefer immutable data to mutable data |
| I.1 | Make interfaces explicit |
| I.2 | Avoid non-const global variables |
| I.4 | Make interfaces precisely and strongly typed |
| I.11 | Never transfer ownership by a raw pointer or reference |
| I.23 | Keep the number of function arguments low |
DO
// P.10 + I.4: Immutable, strongly typed interface
struct Temperature {
double kelvin;
};
Temperature boil(const Temperature& water);
DON'T
// Weak interface: unclear ownership, unclear units
double boil(double* temp);
// Non-const global variable
int g_counter = 0; // I.2 violation
Functions (F.*)
Key Rules
| Rule | Summary |
|---|---|
| F.1 | Package meaningful operations as carefully named functions |
| F.2 | A function should perform a single logical operation |
| F.3 | Keep functions short and simple |
| F.4 | If a function might be evaluated at compile time, declare it constexpr |
| F.6 | If your function must not throw, declare it noexcept |
| F.8 | Prefer pure functions |
| F.16 | For "in" parameters, pass cheaply-copied types by value and others by const& |
| F.20 | For "out" values, prefer return values to output parameters |
| F.21 | To return multiple "out" values, prefer returning a struct |
| F.43 | Never return a pointer or reference to a local object |
Parameter Passing
// F.16: Cheap types by value, others by const&
void print(int x); // cheap: by value
void analyze(const std::string& data); // expensive: by const&
void transform(std::string s); // sink: by value (will move)
// F.20 + F.21: Return values, not output parameters
struct ParseResult {
std::string token;
int position;
};
ParseResult parse(std::string_view input); // GOOD: return struct
// BAD: output parameters
void parse(std::string_view input,
std::string& token, int& pos); // avoid this
Pure Functions and constexpr
// F.4 + F.8: Pure, constexpr where possible
constexpr int factorial(int n) noexcept {
return (n <= 1) ? 1 : n * factorial(n - 1);
}
static_assert(factorial(5) == 120);
Anti-Patterns
- Returning
T&&from functions (F.45) - Using
va_arg/ C-style variadics (F.55) - Capturing by reference in lambdas passed to other threads (F.53)
- Returning
const Twhich inhibits move semantics (F.49)
Classes & Class Hierarchies (C.*)
Key Rules
| Rule | Summary |
|---|---|
| C.2 | Use class if invariant exists; struct if data members vary independently |
…
よくある質問
「RAII」とは何ですか?
「Resource Acquisition Is Initialization(リソースの取得は初期化)」の略です。メモリやファイルなどのリソースの取得を、オブジェクトの寿命に結びつける技法です。オブジェクトが有効範囲を出ると、リソースは自動的に解放されます。
「デフォルトで不変(immutability by default)」とはどういう意味ですか?
明確な理由がない限り、データは初期状態では変更できない定数として宣言すべきという考え方です。これにより、意図しない変更を防げます。
なぜ「ポインタ」より「値」を使うほうがよいのですか?
データそのものを持つオブジェクト(値のセマンティクス)を使うと、データへの参照(ポインタのセマンティクス)よりも一般的に安全なコードになります。メモリ管理が単純になり、nullポインタのエラーも避けられるためです。
詳細
作者による元の説明
C++ coding standards based on the C++ Core Guidelines (isocpp.github.io). Use when writing, reviewing, or refactoring C++ code to enforce modern, safe, and idiomatic practices.
repo: affaan-m/ECC
人気
- GitHubのスター
- 280k
- ランキング
- #158
- ライセンス
- MIT
- 更新日
- 2026/10/15
コメント
- …
このリポジトリの他のスキル
accessibility
280kエージェントが誰にとっても使いやすいインターフェースを作れるように手助けします。目が見えない方や身体の動きに制限のある方など、障害のある人にとってもWebサイトやアプリが正しく動くようにします。
agent-architecture-audit
280kエージェントがすべて正しく動いているかを確認できるようにします。AIシステムの問題を見つけて修正し、期待どおりに動くようにします。
agent-eval
280kエージェントが異なるAIコーディングツールを比較できるようにします。特定のタスクにおいて、どれがより速く、安く、信頼性が高いかを測定します。
agent-harness-construction
280kエージェントがタスクの進め方をよりよく理解できるようにします。エージェントが使うツールや指示を最適化し、成功する可能性を高めます。
似ているスキル
diffs
394k編集内容の実際の共有可能な差分を、手書きの要約ではなく、ビューアーのリンクや画像ファイルとして作成するようエージェントを支援します。
coding-agent
394kCodex、Claude Code、OpenCodeに大きめのコーディング作業をバックグラウンドのワーカーとして渡し、その進捗を確認するようエージェントを支援します。
gh-issues
394kGitHubのイシューを探し、取り組むものを選び、修正を含むプルリクエストを開くようエージェントを支援します。レビューコメントへの対応も含みます。