Skip to content

zhanmyz/modern-cpp-forge

Repository files navigation

🚀 Learning C++ — Advanced Topics for AI/ML Development

C++20 CMake Linux MIT

English | 中文

A hands-on project for systematically learning advanced C++ topics, focused on techniques commonly used in AI/LLM development: concurrency & thread pools, design patterns, modern C++ features, metaprogramming, and performance optimization.

Official C++ reference: https://en.cppreference.com

📁 Project Structure

modern-cpp-forge/
├── CMakeLists.txt              # Root build file
├── .clang-format               # Code formatting config
├── .clang-tidy                 # Static analysis config
├── common/                     # Shared library (logger, timer)
│   ├── include/common/
│   │   ├── logger.h            # Thread-safe colored logging
│   │   └── timer.h             # High-precision timer
│   └── src/
│       ├── logger.cpp
│       └── timer.cpp
├── reference/                  # 📖 Reference implementations
│   ├── concurrency/            # Multithreading & concurrency
│   │   ├── thread_basics.cpp       # Thread creation & management
│   │   ├── mutex_and_lock.cpp      # Mutexes & lock types
│   │   ├── condition_variable.cpp  # Condition variables & producer-consumer
│   │   ├── thread_pool.cpp         # Complete thread pool
│   │   ├── atomic_operations.cpp   # Atomics & lock-free programming
│   │   └── async_and_future.cpp    # std::async & std::future
│   ├── design_patterns/        # Design patterns
│   │   ├── singleton.cpp           # Singleton (3 variants)
│   │   ├── factory.cpp             # Factory (registry-based)
│   │   ├── observer.cpp            # Observer & event system
│   │   └── strategy.cpp            # Strategy pattern
│   ├── memory_management/      # Memory management
│   │   ├── smart_pointers.cpp      # Smart pointer deep dive
│   │   ├── move_semantics.cpp      # Move semantics & perfect forwarding
│   │   └── raii.cpp                # RAII resource management
│   ├── modern_cpp/             # Modern C++ features
│   │   ├── templates_and_concepts.cpp  # Templates & C++20 Concepts
│   │   ├── lambda_and_functional.cpp   # Lambda & functional programming
│   │   └── coroutines.cpp             # C++20 Coroutines
│   ├── metaprogramming/        # Metaprogramming
│   │   └── type_traits_and_sfinae.cpp  # Type traits & SFINAE
│   └── performance/            # Performance optimization
│       └── cache_friendly.cpp      # Cache-friendly programming
├── src/                        # ✏️ Practice code (your implementations)
│   ├── concurrency/
│   ├── design_patterns/
│   ├── memory_management/
│   ├── modern_cpp/
│   ├── metaprogramming/
│   └── performance/
├── tests/                      # Unit tests
├── benchmarks/                 # Performance benchmarks
├── docs/                       # Documentation
│   └── learning_roadmap.md     # Learning roadmap
├── scripts/                    # Utility scripts
│   ├── build.sh                # Build script
│   ├── clean.sh                # Clean script
│   └── run.sh                  # Run script
└── .vscode/                    # VS Code configuration
    ├── launch.json             # F5 debug config
    ├── tasks.json              # Build tasks
    ├── settings.json           # Editor settings
    └── extensions.json         # Recommended extensions

🛠️ Prerequisites

  • Compiler: GCC 10+ or Clang 12+ (C++20 support required)
  • Build system: CMake 3.16+
  • Debugger: GDB
  • OS: Linux (Ubuntu 20.04+)

🚀 Quick Start

1. Clone

git clone https://github.com/zhanmyz/modern-cpp-forge.git
cd modern-cpp-forge

2. Build

# Option 1: Build script (recommended)
chmod +x scripts/*.sh
./scripts/build.sh debug      # Debug mode (sanitizers enabled)
./scripts/build.sh release    # Release mode (optimized)

# Option 2: Manual CMake
mkdir -p build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Debug
cmake --build . --parallel $(nproc)

3. Run Examples

# Run reference implementations
./bin/reference/concurrency/thread_basics
./bin/reference/design_patterns/singleton
./bin/reference/performance/cache_friendly

# Run tests
./bin/tests/test_logger

# Run benchmarks
./bin/benchmarks/bench_containers

# Using the run script
./scripts/run.sh thread_pool

4. Debug in VS Code

  1. Open any .cpp file
  2. Press F5 — auto-builds and debugs the current file
  3. Breakpoints, variable inspection, call stack — all work out of the box

📚 How to Use

Recommended Workflow

  1. Read the reference code under reference/ (detailed comments included)
  2. Understand the core concepts and their AI/ML applications
  3. Implement your own version under src/ in the corresponding directory
  4. Compare your implementation against the reference
  5. Experiment — tweak parameters, test edge cases, observe behavior

Learning Roadmap

See docs/learning_roadmap.md

Suggested order: Memory Management → Concurrency → Design Patterns → Modern C++ → Metaprogramming → Performance

🏗️ Features

Feature Description
🎨 Colored logging Timestamped, thread-ID, file:line colored terminal output
⏱️ Scoped timer RAII-based auto-timing for performance measurement
🔧 Modern CMake Each source file compiles to its own executable
🐛 Debug support AddressSanitizer + UBSanitizer + GDB
📏 Code standards clang-format + clang-tidy configured
📊 Benchmarks Built-in performance comparison framework
🧪 Unit tests Test framework ready
📖 Annotated code Each concept explained with real-world analogies

🔧 Common Commands

# Build
./scripts/build.sh debug

# Clean
./scripts/clean.sh

# Format code
find . -name "*.cpp" -o -name "*.h" | xargs clang-format -i

# Static analysis
clang-tidy src/**/*.cpp -- -std=c++20 -I common/include

# Run all tests
find build/bin/tests -type f -executable -exec {} \;

📝 Adding New Topics

  1. Create a reference implementation under reference/<category>/
  2. Create a practice template under src/<category>/
  3. Rebuild: ./scripts/build.sh debug
  4. New files are auto-discovered by CMake

License

MIT

About

Modern C++ learning framework — structured reference implementations, hands-on practice, and performance benchmarks covering concurrency, design patterns, memory management, metaprogramming, and more.

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages