A comprehensive demonstration project showcasing modern C++23 features, best practices, and advanced programming techniques. This project serves as both a learning resource and a reference implementation for modern C++ development.
- std::print: Modern formatted output with type safety
- std::format: Powerful string formatting with custom formatters
- std::expected: Railway-oriented error handling (via
Resulttype) - std::ranges: Algorithms and views for functional programming
- std::numbers: Mathematical constants (
Ο,e) with precision - std::source_location: Enhanced exception context and debugging
- Concepts: Compile-time type constraints and validation
- Three-way comparison (
<=>): Spaceship operator for robust comparisons
- Modular Architecture: Clean, scalable project structure with 8 separate components
- Comprehensive Testing: Full test coverage using Catch2 v3 with benchmarking
- Advanced CMake: Modern CMake build system with modular configuration and packaging
- Memory Safety: RAII patterns, smart pointers, and resource management utilities
- Type Safety: Concept-based constraints preventing common programming errors
- Performance Tools: Built-in timing utilities and benchmark framework
- Error Handling: Multiple error handling strategies (exceptions,
Resulttype,std::expected) - Python Bindings: Complete pybind11 integration with modern Python features
- Automatic Formatting: Pre-commit hooks with
clang-formatandgersemi - Static Analysis: Integrated
clang-tidyandcppcheckfor code quality - Documentation: Comprehensive Doxygen-style documentation
- CI/CD Automation: Automatic build and test workflows with CMake presets
- π Features
- π Table of Contents
- π What You'll Learn
- π οΈ Installation
- π― Usage
- π Project Structure
- π§ Components Overview
- π» Development Notes
- π License
This project demonstrates practical applications of:
- Modern C++ Concepts: Type-safe template constraints and compile-time validation
- Ranges & Views: Functional programming patterns with lazy evaluation
- Error Handling: Multiple strategies from exceptions to functional error types
- Memory Management: RAII, smart pointers, and resource lifecycle management
- Performance: Timing utilities, benchmarking, and optimization techniques
- Testing: Comprehensive test suites with Catch2 v3 and template testing
- Build Systems: Modern CMake with modular design and package management
- Code Quality: Static analysis, formatting, and development workflows
- C++23 compatible compiler (Clang 20+ / GCC 14+)
- CMake 3.23+
- Ninja build system (required for CMake - faster builds than Make)
- Python 3.12+ (optional, for Python bindings)
It's recommended to use a development container for the best development experience.
See .devcontainer/README.md for more details.
git clone https://github.com/hakula139/cpp-demo-project
cd cpp-demo-projectcmake --preset release
cmake --build --preset release./build/examples/algorithms_example
./build/examples/containers_example
./build/examples/exceptions_example
./build/examples/memory_example
./build/examples/random_example
./build/examples/shapes_example
./build/examples/timing_examplectest --preset releaseThis project uses CMake presets for streamlined build configuration.
| Preset | Description |
|---|---|
debug |
Debug build with symbols and no optimization |
release |
Release build with full optimization |
debug-no-tests |
Debug build without tests and examples (faster config) |
release-no-tests |
Release build without tests and examples (faster config) |
debug-strict |
Debug build with static analysis and warnings treated as errors |
release-strict |
Release build with static analysis and warnings treated as errors |
debug-python |
Debug build with Python bindings |
release-python |
Release build with Python bindings |
debug-python-no-tests |
Debug build with Python bindings, without tests and examples |
release-python-no-tests |
Release build with Python bindings, without tests and examples |
debug-python-strict |
Debug build with Python bindings, static analysis and warnings treated as errors |
release-python-strict |
Release build with Python bindings, static analysis and warnings treated as errors |
Each configure preset has corresponding build and test presets with the same names.
Each workflow preset is a combination of the configure, build, and test presets.
| Preset | Description |
|---|---|
debug-workflow |
Complete workflow for debug preset |
release-workflow |
Complete workflow for release preset |
debug-strict-workflow |
Complete workflow for debug-strict preset |
release-strict-workflow |
Complete workflow for release-strict preset |
debug-python-workflow |
Complete workflow for debug-python preset |
release-python-workflow |
Complete workflow for release-python preset |
debug-python-strict-workflow |
Complete workflow for debug-python-strict preset |
release-python-strict-workflow |
Complete workflow for release-python-strict preset |
# List available presets
cmake --list-presets=configure
cmake --list-presets=build
cmake --list-presets=test
# Quick development cycle
cmake --preset debug # Configure debug build
cmake --build --preset debug # Build debug targets
ctest --preset debug # Run debug tests
# Fast iteration (no tests and examples)
cmake --preset debug-no-tests # Configure without tests
cmake --build --preset debug-no-tests # Build main targets only
# Production build
cmake --preset release # Configure release build
cmake --build --preset release # Build release targets
ctest --preset release # Run release tests
# Automated workflows (configure + build + test)
cmake --workflow --preset debug-workflow # Complete debug cycle
cmake --workflow --preset release-workflow # Complete release cycleSee cmake/README.md for additional build options.
This project uses pre-commit to ensure code quality and consistent formatting. Set up pre-commit hooks to automatically format your code before each commit:
pip3 install pre-commitpre-commit installpre-commit run --all-files- Standard checks:
- Removes trailing whitespace
- Ensures proper file endings
- Validates YAML syntax
- Checks for added large files
- clang-format: Formats C++ code according to the project style
- gersemi: Formats CMake files with consistent indentation
- ruff: Lint and format Python code with consistent style
- mypy: Lints Python code with type hints
- bandit: Lints Python code with security best practices
- markdownlint-cli2: Lints Markdown files with consistent formatting
The hooks will run automatically on git commit and prevent commits with formatting issues.
#include <print>
#include "algorithms/stl.hpp"
#include "containers/container.hpp"
#include "shapes/circle.hpp"
using cpp_features::algorithms::SortContainer;
using cpp_features::containers::Container;
using cpp_features::shapes::CreateCircle;
auto main() -> int {
// Modern container with concept constraints
Container<int> numbers{42, 17, 89, 3, 56};
std::println("Original: {}", numbers);
// Sort the container in place
SortContainer(numbers);
std::println("Sorted: {}", numbers);
// Type-safe factory functions with validation
auto circle = CreateCircle(5.0);
auto area = circle->GetArea();
auto perimeter = circle->GetPerimeter();
std::println("Area: {:.2f}, Perimeter: {:.2f}", area, perimeter);
return 0;
}More examples can be found in the examples directory.
cpp-demo-project/
βββ .github/ # GitHub Actions configuration
β βββ workflows/ # GitHub Actions workflows
βββ .vscode/ # VS Code configuration
β βββ launch.json # VS Code launch configuration
β βββ settings.json # VS Code settings
β βββ tasks.json # VS Code tasks
βββ build/ # Build output (generated by CMake)
βββ cmake/ # CMake modules and utilities
β βββ CompilerWarnings.cmake # Compiler warning configuration
β βββ Dependencies.cmake # External dependencies configuration
β βββ ModuleHelpers.cmake # Module helper functions
β βββ StaticAnalysis.cmake # Static analysis tools
β βββ config.cmake.in # Package configuration
β βββ README.md # CMake modules documentation
βββ include/ # Public C++ header files
β βββ algorithms/ # STL algorithm wrappers with concepts
β βββ concepts/ # Custom concepts and type traits
β βββ containers/ # Modern container wrapper with ranges support
β βββ exceptions/ # Custom exception hierarchy and Result type
β βββ memory/ # Resource management and RAII utilities
β βββ random/ # Type-safe random number generation
β βββ shapes/ # Polymorphic shapes with factory functions
β βββ timing/ # Performance measurement and benchmarking
βββ src/ # C++ source implementation files
β βββ CMakeLists.txt # Components configuration
β βββ [module]/ # C++ source implementation for the component
βββ examples/ # C++ usage examples and demonstrations
β βββ [module]_example.cpp # C++ usage examples for the component
βββ tests/ # C++ test suite using Catch2 v3
β βββ test_[module].cpp # C++ unit tests for the component
βββ binding/ # pybind11 C++ binding files
β βββ CMakeLists.txt # Python bindings configuration
β βββ cpp_features.cpp # Main pybind11 module
β βββ [module]_binding.cpp # Individual module bindings
βββ python/ # Python wrapper modules (see python/README.md for more details)
βββ .clang-format # clang-format configuration (for C++ code formatting)
βββ .clang-tidy # clang-tidy configuration (for static analysis)
βββ .clangd # clangd configuration (for code completion)
βββ .gersemirc # gersemi configuration (for CMake code formatting)
βββ .markdownlint.yaml # markdownlint configuration (for Markdown formatting)
βββ .pre-commit-config.yaml # pre-commit hooks configuration
βββ CMakeLists.txt # Main project configuration
βββ CMakePresets.json # CMake presets configuration
βββ LICENSE # MIT License
βββ README.md # This file
| Component | Purpose | Key Features |
|---|---|---|
| Algorithms | STL algorithm wrappers | Concepts, ranges, type safety |
| Concepts | Type constraints | Arithmetic, container, utility concepts |
| Containers | Enhanced containers | Modern wrapper, ranges, std::expected |
| Exceptions | Error handling | Custom hierarchy, Result type, source location |
| Memory | Resource management | RAII, smart pointers, automatic cleanup |
| Random | Random generation | Type-safe, multiple distributions, ranges |
| Shapes | OOP demonstration | Polymorphism, factory functions, comparisons |
| Timing | Performance tools | Benchmarking, scoped timing, statistics |
- Consistent formatting
- Uses
clang-formatfor C++ code - Uses
gersemifor CMake files - Uses
rufffor Python code - Uses
markdownlint-cli2for Markdown files
- Uses
- Static analysis
- Uses
clang-tidyandcppcheckfor C++ code - Uses
ruff,mypyandbanditfor Python code
- Uses
- Modern practices
- Follows Core Guidelines and modern C++23 best practices
- Follows PEP 8 and modern Python conventions
- Comprehensive documentation
- Doxygen-style documentation for C++ code
- Numpy-style docstrings for Python code
The project includes a comprehensive pre-commit setup .pre-commit-config.yaml.
Benefits:
- Consistent code formatting across the entire project
- Automatic detection of common issues before commit
- Enforced coding standards for all contributors
- Integration with modern formatting tools
This project is licensed under the MIT License - see the LICENSE file for details.