A collection of modern C++ coding examples and small exercises focused on correctness, clarity, and real-world patterns.
This repository is intended as:
- An interview-preparation playground
- A reference for concurrency, synchronization, low-level, and modern C++ techniques
- A growing set of self-contained, buildable examples
- A collection of practical examples and CLI tools rather than puzzle-only solutions
Current examples include:
- Multithreading
- Smart pointers
- Views
- Folding
- Parallelism
- STL and ranges:
- Algorithms
- Data Structures
- OOP
cpp-coding-exercise/
├── Makefile # top-level dispatcher
├── common.mk # shared compiler flags
├── <example>/
│ ├── Makefile
│ ├── main.cpp
│ └── tests.sh # optional behavior tests
└── .github/workflows/
├── ci.yml # builds, sanitizers, tests, and coverage
└── coverage-comment.yml
- Each example is self-contained
- Each folder has its own
Makefile - The top-level
Makefileautomatically discovers and builds all examples - No central list to maintain when adding new folders
sudo apt update
sudo apt install g++-14 make clang-format gcovr jq libtbb-devThe examples target C++23 and are tested on Ubuntu with GCC 14.
makeThis will recursively build every directory that contains a Makefile.
cd thread-safe-queue
makeRunnable examples expose a run target:
make runTop-level make run runs all built targets and each available tests.sh script.
make build # Build every example with the selected sanitizer
make test # Build as needed, run examples, and execute test scripts
make format # Format all C and C++ source files
make lint # Check formatting without modifying files
make coverage # Generate HTML, XML, JSON, and text coverage reports
make check # Run formatting and supported sanitizer builds/tests
make clean # Remove binaries, objects, and coverage artifactsmake coverage enforces at least 80% line coverage, 80% function coverage, and 70% branch coverage by default.
Override local thresholds with COVERAGE_MIN, COVERAGE_FUNCTION_MIN, and COVERAGE_BRANCH_MIN.
Clean everything:
make cleanOr clean a single example:
cd thread-safe-queue
make cleanCommon compiler settings live in common.mk:
CXX := g++-14
CXXFLAGS := -std=c++23 -Wall -Wextra -WerrorIndividual examples may extend this, e.g.:
CXXFLAGS += -pthreadAddressSanitizer is enabled by default to detect memory safety problems and leaks.
# Builds with AddressSanitizer automatically
make
# ThreadSanitizer
make SANITIZE=thread
# UndefinedBehaviorSanitizer
make SANITIZE=undefined
# Select the sanitizer matrix used by make check
make check SANITIZERS="address thread undefined"
# No sanitizers
make SANITIZE=GCC ThreadSanitizer cannot reliably reserve its shadow-memory layout under WSL2 and may terminate before an example
starts with FATAL: ThreadSanitizer: unexpected memory mapping. On WSL2, make check therefore runs ASan and UBSan by
default. Native Linux runs ASan, TSan, and UBSan. SANITIZERS can override either default.
The parallel-transform example uses libstdc++'s parallel STL backend, which delegates to oneTBB. TSan requires oneTBB
itself to be sanitizer-aware so that its internal synchronization is visible. The dedicated CI TSan job therefore builds
the pinned oneTBB release with TBB_SANITIZE=thread instead of using Ubuntu's ordinary runtime library.
clang-format enforces the repository's C++ style.
make format # Apply formatting
make lint # Check all example directories
./clang-check.sh # Equivalent recursive standalone checkEach example also exposes make check-format from its own directory.
GitHub Actions builds all examples on:
- Pushes to
main - Pull requests targeting
main
The CI setup requires no updates when new example folders are added.
CI runs ASan and UBSan in one job and TSan with a sanitizer-aware oneTBB build in another. It also enforces the documented coverage floors, uploads an HTML coverage report, and posts a coverage summary on pull requests. Each sanitizer configuration starts from a clean build so compiler flags cannot be silently reused from a previous configuration.
- C++23
- GNU Make
- GCC 14
- clang-format
- gcovr
- Linux (Ubuntu)
-
✔ Correctness over cleverness
-
✔ Explicit concurrency semantics
-
✔ Minimal dependencies
-
✘ No frameworks
-
✘ No large abstractions
-
✘ No header‑only meta‑programming for its own sake
See CONTRIBUTING.md before submitting a change. For help, use the channels in SUPPORT.md. Report security concerns privately as described in SECURITY.md.
Project changes are recorded in CHANGELOG.md, and the repository's data practices are documented in PRIVACY.md.
MIT (unless otherwise stated in a specific example).
Many examples intentionally focus on edge cases and failure modes such as data races, lifetime issues, and ordering bugs. They are meant to be read, built, and experimented with.
Contributions and discussions are welcome.