Skip to content

Mieraidihaimu/RevisitCPP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

RevisitCPP - Comprehensive C++ Knowledge Repository

A comprehensive C++ knowledge repository covering everything from basic syntax to advanced patterns and modern C++ features (C++11/14/17/20).

πŸ“š Table of Contents

Basic C++ Concepts

  • BasicDataTypes.cpp - Fundamental data types, operators, references, and modern type features
  • BasicControlFlows.cpp - Control structures, loops, lambdas, and C++20 concepts
  • BasicAbstractions.cpp - Classes, structs, unions, and basic data structures
  • Threads.cpp - Basic threading with std::thread and mutexes

Advanced C++ Features

1. SmartPointers.cpp

  • unique_ptr - Exclusive ownership semantics
  • shared_ptr - Shared ownership with reference counting
  • weak_ptr - Breaking circular references
  • Custom deleters and polymorphism with smart pointers
  • Factory patterns with smart pointers

2. MoveSemantics.cpp

  • Lvalue vs Rvalue references
  • Move constructors and move assignment operators
  • Perfect forwarding with std::forward
  • Universal references (forwarding references)
  • Return Value Optimization (RVO)
  • Move-only types
  • STL integration with move semantics

3. AdvancedTemplates.cpp

  • Template specialization (full and partial)
  • Variadic templates and fold expressions
  • SFINAE (Substitution Failure Is Not An Error)
  • Type traits and type transformations
  • Tag dispatching
  • Template template parameters
  • Non-type template parameters
  • Expression templates

4. DesignPatterns.cpp

  • CRTP - Curiously Recurring Template Pattern
  • Singleton - Thread-safe singleton implementation
  • Factory - Traditional and modern registration-based factories
  • Observer - Event notification pattern
  • Strategy - Runtime algorithm selection
  • PIMPL - Pointer to Implementation idiom
  • Builder - Step-by-step object construction
  • Adapter - Interface adaptation pattern

5. AdvancedConcurrency.cpp

  • std::async and std::future
  • std::promise for cross-thread communication
  • Atomic operations and memory ordering
  • Condition variables
  • Producer-consumer pattern
  • Thread pools
  • shared_mutex for reader-writer locks
  • scoped_lock for deadlock prevention
  • packaged_task and shared_future
  • call_once for initialization

6. ModernSTL.cpp

  • STL algorithms (sort, find, transform, accumulate, etc.)
  • Custom iterators implementation
  • Iterator adaptors (back_inserter, reverse_iterator, move_iterator)
  • Modern containers (unordered_map, multimap, set)
  • std::optional for optional values
  • std::variant for type-safe unions
  • std::any for type-erased storage
  • Function objects and std::bind
  • Numeric algorithms
  • Ranges concept (C++20)

7. CompileTime.cpp

  • constexpr functions and variables
  • constexpr classes
  • if constexpr for compile-time branching
  • Template metaprogramming
  • Compile-time computations (factorial, fibonacci, primes)
  • Type computations at compile time
  • constexpr lambdas
  • consteval and constinit concepts (C++20)
  • Practical compile-time examples

Practical Examples

  • LRUCache.cpp - Complete LRU cache implementation with disk I/O

πŸš€ Building and Running

Prerequisites

  • C++17 or later compiler (GCC 7+, Clang 5+, MSVC 2017+)
  • For best experience, use C++20 compatible compiler

Compilation

# Using g++
g++ -std=c++17 -pthread src/main.cpp -o main

# Using clang++
clang++ -std=c++17 -pthread src/main.cpp -o main

# For C++20 features
g++ -std=c++20 -pthread src/main.cpp -o main

Run

./main

πŸ“– Learning Path

Beginner

  1. BasicDataTypes.cpp
  2. BasicControlFlows.cpp
  3. BasicAbstractions.cpp
  4. SmartPointers.cpp

Intermediate

  1. MoveSemantics.cpp
  2. Threads.cpp
  3. ModernSTL.cpp
  4. DesignPatterns.cpp

Advanced

  1. AdvancedTemplates.cpp
  2. AdvancedConcurrency.cpp
  3. CompileTime.cpp
  4. LRUCache.cpp (practical implementation)

🎯 Key C++ Features Covered

C++11

  • Auto and decltype
  • Lambda expressions
  • Smart pointers
  • Move semantics
  • Variadic templates
  • constexpr
  • Thread library
  • nullptr

C++14

  • Generic lambdas
  • Return type deduction
  • make_unique
  • Extended constexpr

C++17

  • Structured bindings
  • if constexpr
  • std::optional, std::variant, std::any
  • Fold expressions
  • constexpr lambda
  • shared_mutex

C++20 (Concepts)

  • Concepts and requires
  • consteval
  • constinit
  • Ranges

πŸ“ Code Organization

RevisitCPP/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ main.cpp                    # Main entry point
β”‚   β”œβ”€β”€ BasicDataTypes.cpp          # Basic types
β”‚   β”œβ”€β”€ BasicControlFlows.cpp       # Control flow
β”‚   β”œβ”€β”€ BasicAbstractions.cpp       # OOP basics
β”‚   β”œβ”€β”€ Threads.cpp                 # Basic threading
β”‚   β”œβ”€β”€ SmartPointers.cpp           # Smart pointer patterns
β”‚   β”œβ”€β”€ MoveSemantics.cpp           # Move & forward
β”‚   β”œβ”€β”€ AdvancedTemplates.cpp       # Template programming
β”‚   β”œβ”€β”€ DesignPatterns.cpp          # Design patterns
β”‚   β”œβ”€β”€ AdvancedConcurrency.cpp     # Advanced threading
β”‚   β”œβ”€β”€ ModernSTL.cpp               # STL features
β”‚   β”œβ”€β”€ CompileTime.cpp             # Compile-time prog
β”‚   └── LRUCache.cpp                # LRU implementation
β”œβ”€β”€ tests/
β”‚   └── LRUCacheTests.cpp           # Unit tests
└── README.md                        # This file

πŸ” Topics by Category

Memory Management

  • Smart pointers (unique_ptr, shared_ptr, weak_ptr)
  • Move semantics
  • RAII patterns
  • Custom allocators

Concurrency

  • Threads and mutexes
  • Futures and promises
  • Atomics
  • Thread pools
  • Lock-free programming basics

Template Programming

  • Generic programming
  • Template metaprogramming
  • Type traits
  • SFINAE
  • Concepts

Modern C++ Idioms

  • CRTP
  • PIMPL
  • Type erasure
  • Expression templates
  • Policy-based design

πŸ› οΈ Best Practices Demonstrated

  • βœ… RAII (Resource Acquisition Is Initialization)
  • βœ… Rule of Five (copy/move constructors and assignment)
  • βœ… const correctness
  • βœ… noexcept specifications
  • βœ… Smart pointer usage over raw pointers
  • βœ… Move semantics for performance
  • βœ… constexpr for compile-time computation
  • βœ… Modern STL algorithms over manual loops
  • βœ… Lambda expressions for concise code
  • βœ… Structured bindings for readability

πŸ“š References and Resources

🀝 Contributing

Feel free to add more examples, patterns, or improve existing code!

πŸ“„ License

MIT License - See LICENSE file for details

πŸŽ“ Educational Purpose

This repository is designed for:

  • Learning modern C++ features
  • Understanding advanced patterns
  • Interview preparation
  • Quick reference for C++ idioms
  • Teaching C++ best practices

About

Revisit some C++ programming

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors