C++ Patterns

repository·master·Indexed 23 days ago

https://github.com/sftrabbit/cpppatterns-patterns

A 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).

Tokens
955
Snippets
1
Records
7
Agent score
31%

What's inside cpppatterns-patterns

  1. Overview of C++ Patterns

    master
    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).
  2. Pattern file naming and directory structure

    master

    Patterns are stored as .cpp files within a specific directory hierarchy:

    <category>/<section>/<pattern>.cpp

    Key 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.cpp becomes common-tasks/pimpl.html in the web front-end.
  3. Code style guidelines for patterns

    master

    While 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.
  4. Writing style for pattern descriptions

    master

    When 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.
  5. How to contribute a new pattern

    master

    To 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.
  6. Required file format for pattern .cpp files

    master

    Every pattern .cpp file 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 code

    Component 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.
  7. Use Markdown extensions for line references and C++ links

    master

    The pattern description supports two specific Markdown extensions:

    1. Line References

    Use the [XX-YY] syntax to reference line numbers from the original .cpp file. The builder automatically offsets these numbers.

    • [10]: Expands to line 8 (example).
    • [10-14]: Expands to lines 8-12.
    • [!15]: Expands to Line 13 (capitalized).

    To simplify linking to cppreference.com, any link starting with c/ or cpp/ will automatically resolve to the appropriate page on cppreference.com.