Skip to content

hakula139/cpp-demo-project

Repository files navigation

Modern C++ Features Demo Project

Build & Test C++23 CMake License

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.

πŸš€ Features

Core C++23 Features

  • 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

Advanced Features

  • 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)
  • Python Bindings: Complete pybind11 integration with modern Python features

Code Quality & Development

  • Automatic Formatting: Pre-commit hooks with clang-format and gersemi
  • Static Analysis: Integrated clang-tidy and cppcheck for code quality
  • Documentation: Comprehensive Doxygen-style documentation
  • CI/CD Automation: Automatic build and test workflows with CMake presets

πŸ“‹ Table of Contents

πŸŽ“ What You'll Learn

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

πŸ› οΈ Installation

Prerequisites

  • 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.

Quick Start

Clone the repository

git clone https://github.com/hakula139/cpp-demo-project
cd cpp-demo-project

Build the project

cmake --preset release
cmake --build --preset release

Run examples

./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_example

Run tests

ctest --preset release

CMake Presets

This project uses CMake presets for streamlined build configuration.

Configure Presets

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

Build & Test Presets

Each configure preset has corresponding build and test presets with the same names.

Workflow Presets

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

Usage Examples

# 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

Build Options

See cmake/README.md for additional build options.

Pre-commit Setup (Recommended)

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:

Install pre-commit

pip3 install pre-commit

Install the git hooks

pre-commit install

Run on all files (optional)

pre-commit run --all-files

What the hooks do

  • 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.

🎯 Usage

#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.

πŸ“ Project Structure

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

πŸ”§ Components Overview

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

πŸ’» Development Notes

Code Quality

  • Consistent formatting
    • Uses clang-format for C++ code
    • Uses gersemi for CMake files
    • Uses ruff for Python code
    • Uses markdownlint-cli2 for Markdown files
  • Static analysis
    • Uses clang-tidy and cppcheck for C++ code
    • Uses ruff, mypy and bandit for Python code
  • 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

Pre-commit Configuration

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

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

About

Modern C++ Features Demo Project

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published