A collection of self-contained C++ code snippets, examples, and mini-projects. Each snippet is independently buildable and includes its own CMake configuration.
These are the pieces worth reading first — they demonstrate depth beyond tutorial-level C++:
| Snippet | Lines | What It Demonstrates |
|---|---|---|
concurrency/happens-before/ |
~1,845 | C++ memory model deep-dive with stress tests across all 6 memory orderings |
concurrency/proactor/ |
~3,705 | POSA2 async I/O pattern — complete proactor implementation |
cpp-features/cpp20/coroutines/ |
~1,354 | Complete generator with promise_type, co_await, co_yield |
cpp-features/cpp23/turing-machines/ |
~5,613 | 5 Turing machines culminating in a Universal TM |
algorithms/bloom-filter/ |
~1,149 | Production-quality Bloom filter with VMware VADP context |
algorithms/hyperloglog/ |
~1,186 | Cardinality estimation based on Flajolet et al. |
concurrency/atomic/ |
~1,000+ | All memory orderings with lock-free producer-consumer |
cpp-idioms/rule-of-five/ |
~800+ | Railway-oriented programming with C++23 std::expected |
cpp-features/cpp23/expected/ |
~600+ | Modern error handling patterns |
design-patterns/ |
23+ patterns | Complete GoF coverage plus C++-specific patterns (CRTP, etc.) |
Each snippet is contained in its own directory with a CMakeLists.txt file. To build a specific snippet:
cd <snippet-directory>
mkdir build && cd build
cmake ..
makeOr you can elect to build everything at once.
-
build_all.shthat:- Automatically finds and builds all CMake projects in your directory structure
- Creates build directories as needed
- Provides colored output and detailed logging
- Shows a comprehensive summary of successful and failed builds with executable paths
--dry-run|-dargument to perform a dry run--run|-rargument to perform run executable after build
-
clean_all.shthat:- Finds and removes all build directories
- Provides similar colored output and logging
- Shows a summary of cleaned projects
--dry-run|-dargument to perform a dry run
- CMake 3.30 or higher
- C++20 compliant compiler (see gotcha's below for linux)
- Git
- binary-search/
- bloom-filter/
- compression/
- hash-table/
- hyperloglog/
- longest_common_subsequence-aka-diff/
- merkle-tree/
- quick-sort/
- active-object-pattern/
- atomic/
- barrier-example/
- cpp20-latch/
- dining-philosophers/
- happens-before/
- jthread-test/
- jthreads/
- latch/
- pipelining/
- proactor/
- producer-consumer/
- promise-future/
- reader-writer/
- shared_mutex/
- stop_token-source-callback/
- thread-affinity/
- thread-local-storage/
- thread-pool/
- thread-pool-load-balance/
- thread-safe-logging/
- timed_mutex/
- chain-of-responsibility/
- command/
- interpreter/
- iterator/
- mediator/
- memento/
- null-object/
- observer/
- state/
- strategy/
- template-method/
- visitor/
- crtp/ — Static polymorphism, mixin classes, object counting, compile-time interface enforcement
- concepts/
- coroutines/
- counting-semaphore/
- designated-initializers/
- modules/
- ranges/
- spaceship/
- test-macros/
- alias-labels/
- byteswap/
- expected/
- if-consteval/
- mdspan/
- multi-subscript/
- new-preprocessor/
- print-println/
- this/
- turing-machines/
- const-constexpr/
- decltype/
- slicing/
- std-function/
- std-move/
- std-optional/
- std-regex/
- template-progression/
Feel free to suggest improvements or report issues. Each snippet should:
- Be self-contained in its own directory
- Include a CMakeLists.txt file
- Have a README.md explaining its purpose and usage
- Include comments explaining complex logic
- Follow the repository's coding style
Each snippet directory should follow this structure:
snippet-name/
├── CMakeLists.txt # Build configuration
├── README.md # Usage and explanation
├── include/ # Header files
│ └── snippet.hpp
└── src/ # Source files
└── snippet.cpp
I am currently evaluating JetBrain's CLion which creates a directory in each project file called cmake-build-debug which if removed becomes a PITA.
If you accidentally removed the cmake-build-debug directory, you can easily recreate it by following these steps:
Navigate to the root directory of your CMake project in your terminal or command prompt, then run:
cmake -DCMAKE_BUILD_TYPE=Debug ..Note: Replace .. with the path to your CMakeLists.txt file if it's not in the same directory.
This command will reconfigure your project and generate the cmake-build-debug directory along with the necessary build files.
You can either:
- Rebuild your project from within your IDE (Visual Studio or CLion)
OR
- Use the following command from your terminal:
cmake --build . --target allThis will rebuild all the targets in your project.
As of this writing 2/17/2025 most of the projects are building on linux, see below for my setup instructions. However there still are six (6) project not building, I am working through issues on them currently.
Build failures occurred when attempting to use std::format in C++20 code. While the code built successfully in CLion, it failed during command line cmake/make builds with the error:
fatal error: format: No such file or directory
The system was initially running GCC 12.3.0 which had incomplete C++20 library support.
GCC 12 implementations shipped with partial C++20 support. Specifically, the std::format header was not included in the standard library headers, which explained why the <format> header could not be found during compilation.
The issue was resolved by upgrading to GCC 13, which provides complete C++20 support including the std::format implementation.
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt updatesudo apt install gcc-13 g++-13 libstdc++-13-devsudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 130
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-13 130- Successfully upgraded to GCC 13.1.0 (Ubuntu 13.1.0-8ubuntu1~22.04)
- Full C++20 support including
std::format - Code now builds correctly both in CLion and command line environments
When implementing C++20 features, particularly std::format, GCC 13 or later is required xfor complete standard library support. Earlier versions may have incomplete implementations of the C++20 standard library features.
This project is licensed under the MIT License - see the LICENSE file for details.
Note: This repository is actively maintained and growing. Check back regularly for new snippets and updates!