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
Result
type) - 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,
Result
type,std::expected
)
- Static Analysis: Integrated
clang-tidy
andcppcheck
for code quality - Automatic Formatting: Pre-commit hooks with
clang-format
andgersemi
- Documentation: Comprehensive Doxygen-style documentation
- CI/CD Ready: Modern CMake configuration for easy integration
- π 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)
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-project
cmake --preset release
cmake --build --preset release
./build/release/examples/algorithms_example
./build/release/examples/containers_example
./build/release/examples/exceptions_example
./build/release/examples/memory_example
./build/release/examples/random_example
./build/release/examples/shapes_example
ctest --preset release
This 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 |
Each configure preset has corresponding build and test presets with the same names.
Preset | Description |
---|---|
debug-workflow |
Complete debug workflow: configure + build + test |
release-workflow |
Complete release workflow: configure + build + test |
# 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 cycle
See cmake/README.md
for available 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-commit
pre-commit install
pre-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
- 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);
SortContainer(numbers);
std::println("Sorted: {}", numbers);
// Type-safe factory functions with validation
auto circle = CreateCircle(5.0);
std::println("Area: {:.2f}, Perimeter: {:.2f}", circle->GetArea(), circle->GetPerimeter());
return 0;
}
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)
β βββ debug/ # Debug build output
β βββ release/ # Release build output
β βββ [other presets]
βββ 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 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/ # Source implementation files
β βββ CMakeLists.txt # Components configuration
β βββ [mirrors include structure]
βββ examples/ # Usage examples and demonstrations
βββ tests/ # Test suite using Catch2 v3
βββ .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 |
This project follows the Google C++ Style Guide with some modifications:
- Automatic formatting: Uses
.clang-format
for C++ code andgersemi
for CMake files - Static analysis: Enabled with
.clang-tidy
for code quality checks - Modern C++ practices: Follows Core Guidelines and C++23 best practices
- Documentation: Comprehensive Doxygen-style documentation
The project includes a comprehensive pre-commit setup (.pre-commit-config.yaml
):
repos:
# Standard pre-commit hooks
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: check-added-large-files
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
# C++ formatting with clang-format
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v20.1.7
hooks:
- id: clang-format
files: \.(cpp|hpp|h)$
# CMake formatting with gersemi
- repo: https://github.com/BlankSpruce/gersemi
rev: 0.19.3
hooks:
- id: gersemi
files: (\.cmake|CMakeLists\.txt)$
# Markdown linting and formatting
- repo: https://github.com/DavidAnson/markdownlint-cli2
rev: v0.18.1
hooks:
- id: markdownlint-cli2
args: ['--config', '.markdownlint.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.