C++ Patterns
repository·master·Indexed 23 days ago
https://github.com/sftrabbit/cpppatterns-patternsA repository of code patterns illustrating modern, idiomatic C++ development for beginner to intermediate developers. It serves as a reference for solving common problems using state-of-the-art C++ features (post-C++11).
What's inside cpppatterns-patterns
- C++ Patterns is a repository of code patterns designed to illustrate modern and idiomatic approaches to writing C++. It serves as a reference for beginner to intermediate developers to solve common problems using state-of-the-art C++ features (post-C++11).
Pattern file naming and directory structure
masterPatterns are stored as
.cppfiles within a specific directory hierarchy:<category>/<section>/<pattern>.cppKey Rules:
- Uniqueness: All pattern file names within a single category must be unique because the section directory is stripped during web front-end builds.
- Stability: Avoid changing file names even if the pattern title changes. If a pattern changes significantly, it should be treated as a new pattern.
- Example Path:
1-common-tasks/classes/pimpl.cppbecomescommon-tasks/pimpl.htmlin the web front-end.
Code style guidelines for patterns
masterWhile exact formatting is flexible, patterns must be idiomatic and modern C++. Follow these specific guidelines:
- Avoid
auto: Use explicit types to help beginners understand the code. - Uniform Initialization: Use uniform initialization syntax where possible.
- Generic Naming: Use generic names for entities (e.g.,
foo,bar,func,x). - Simplicity: Keep example code to the bare minimum required to understand the pattern.
- No Inline Comments for Explanation: Do not use inline comments to explain the pattern; use the description section instead. Use inline comments only as placeholders for omitted code.
- Avoid
Writing style for pattern descriptions
masterWhen writing the intent and description sections:
- Use "we": Use the personal pronoun "we" (e.g., "We use this to...") to make the description feel like a collaborative effort between the author and the reader.
- Use Line References: Incorporate line references (
[XX]) to help readers cross-reference the description with the code. - Keep Intent Concise: The intent should be a simple sentence or two describing the purpose of the pattern.
How to contribute a new pattern
masterTo contribute, fork the repository and submit a pull request. New patterns must follow these quality criteria:
- Use only modern C++ language and standard library features.
- Be generic and widely applicable.
- Be understandable for C++ beginners.
- Act as a starting point for learning C++ features.
Required file format for pattern .cpp files
masterEvery pattern
.cppfile must follow this specific structure to be processed correctly by the web front-end builder:// Title // Tag1, Tag2 Example code // Intent paragraph // // Description paragraph #1 // // Description paragraph #2 Hidden codeComponent Details:
- Title & Tags: The first two lines must be single-line comments. The tags line is optional and should be a comma-separated list.
- Example Code: This section is displayed on the pattern page and must contain everything necessary to understand the pattern.
- Intent & Description: These are processed as extended Markdown. The intent should be a simple sentence or two describing the purpose.
- Hidden Code: Any additional code required to make the file compile (but not essential for understanding the pattern) should be placed here.
- Formatting: Keep a line width of approximately 70 characters.
Use Markdown extensions for line references and C++ links
masterThe pattern description supports two specific Markdown extensions:
1. Line References
Use the
[XX-YY]syntax to reference line numbers from the original.cppfile. The builder automatically offsets these numbers.[10]: Expands toline 8(example).[10-14]: Expands tolines 8-12.[!15]: Expands toLine 13(capitalized).
2. C++ Reference Links
To simplify linking to cppreference.com, any link starting with
c/orcpp/will automatically resolve to the appropriate page on cppreference.com.