# Modern C++ Programming

## C++11 / C++14 / C++17 / C++20 / (C++23) This *open-access* course is directed at those who are already familiar with C and object-oriented programming towards a proficiency level of C++ programming. The course covers the basics of C++ programming and moves on to advanced C++ semantics and concepts. **Key features**: - *Free and frequently updated* - *22 lectures, ~1500 slides* - Include the *last language standard* concepts and features - *Practical teaching*: non-verbose, short structured descriptions associated with code - *Minimal code examples* for showing just a specific feature or issue without digressing - *Complementary language aspects*: tools, coding conventions, project organization, and code optimization - *Experience-based*: many aspects, examples, and problems come from real-world cases faced during my work as software engineer If you enjoy the course or you find it useful, please add a **Star** [![stars - Modern-CPP-Programming](https://img.shields.io/github/stars/federico-busato/Modern-CPP-Programming?style=social)](https://github.com/federico-busato/Modern-CPP-Programming) ## CHAPTERS | # | TITLE | MAIN FOCUS | | ------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------- | | **1** | [**Introduction**](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/01.Introduction.pdf) ([html](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/htmls/01.Introduction.html)) | History of C/C++, Areas of Applications, Course introduction | | **2** | [**Basic Concepts I - Fundamental Types**](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/02.Basic_Concepts_I.pdf) ([html](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/htmls/02.Basic_Concepts_I.html)) | Types overview, operators, and conversion | | **3** | [**Basic Concepts II - Integral and Floating-point Types**](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/03.Basic_Concepts_II.pdf) ([html](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/htmls/03.Basic_Concepts_II.html)) | Integral and floating-point types and their arithmetic | | **4** | [**Basic Concepts III - Entities and Control Flow**](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/04.Basic_Concepts_III.pdf) ([html](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/htmls/04.Basic_Concepts_III.html)) | Enumerators, structures, control flow statements | | **5** | [**Basic Concepts IV - Memory Management**](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/05.Basic_Concepts_IV.pdf) ([html](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/htmls/05.Basic_Concepts_IV.html)) | Heap, Stack, pointers, references, const properties, conversion operators | | **6** | [**Basic Concepts V - Functions and Preprocessing**](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/06.Basic_Concepts_V.pdf) ([html](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/htmls/06.Basic_Concepts_V.html)) | Functions, lambda expressions, preprocessing directives | | **7** | [**Object Oriented Programming I - Class Concepts**](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/07.Object_Oriented_I.pdf) ([html](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/htmls/07.Object_Oriented_I.html)) | Class hierarchy, constructor, destructor, class keywords | | **8** | [**Object Oriented Programming II - Polymorphism and Operator Overloading**](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/08.Object_Oriented_II.pdf) ([html](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/htmls/08.Object_Oriented_II.html)) | Polymorphism, operators overloading | | **9** | [**Templates and Meta-programming I - Function Templates and Compile-Time Utilities**](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/09.Templates_I.pdf) ([html](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/htmls/09.Templates_I.html)) | Function template, type traits | | **10** | [**Templates and Meta-programming II - Class Templates and SFINAE**](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/10.Templates_II.pdf) ([html](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/htmls/10.Templates_II.html)) | Class template, SFINAE | | **11** | [**Translation Units I**](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/11.Translation_Units_I.pdf) ([html](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/htmls/1.Translation_Units_I.html)) | Linkage and One Definition Rule | | **12** | [**Translation Units II**](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/12.Translation_Units_II.pdf) ([html](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/htmls/12.Translation_Units_II.html)) | Dealing with multiple translation units and files,  `#include`, and modules | | **13** | [**Code Conventions**](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/13.Code_Convention.pdf) ([html](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/htmls/13.Code_Convention.html)) | Project organization and main code conventions | | **14** | [**Ecosystem I**](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/14.Ecosystem_I.pdf) ([html](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/htmls/14.Ecosystem_I.html)) | Debugging, and testing | | **15** | [**Ecosystem II**](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/15.Ecosystem_II.pdf) ([html](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/htmls/15.Ecosystem_II.html)) | Cmake, documenting, and other Tools | | **16** | [**Utilities**](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/16.Utilities.pdf) ([html](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/htmls/16.Utilities.html)) | Main `std` libraries | | **17** | [**Containers, Iterators, and Algorithms**](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/17.Iterators_Containers_Alg.pdf) ([html](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/htmls/17.Iterators_Containers_Alg.html)) | Containers, iterators, algorithms, ranges | | **18** | [**Advanced Topics I**](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/18.Advanced_Topics_I.pdf) ([html](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/htmls/18.Advanced_Topics_I.html)) | Move semantics, universal reference, type deduction | | **19** | [**Advanced Topics II**](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/19.Advanced_Topics_II.pdf) ([html](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/htmls/19.Advanced_Topics_II.html)) | Error handling, C++ idioms, smart pointers | | **20** | [**Optimization I - Basic Concepts**](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/20.Optimization_I.pdf) ([html](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/htmls/20.Optimization_I.html)) | Ahmdal Law, performance bounds, architecture concepts (ILP, SIMD, etc.), memory hierarchy | | **21** | [**Optimization II - Code Optimization**](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/21.Optimization_II.pdf) ([html](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/htmls/21.Optimization_II.html)) | Arithmetic optimizations, memory optimizations, etc. | | **22** | [**Optimization III - Non-Coding Optimizations and Benchmarking**](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/22.Optimization_III.pdf) ([html](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/htmls/22.Optimization_III.html)) | Compiler optimizations, profiling and benchmarking tools | ***ALL-IN-ONE BOOK***: [**modern-cpp.pdf**](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/modern-cpp.pdf) (could be a few commits behind), [html](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/htmls/modern-cpp.html) ## TOPICS IN DETAILS **[1. Introduction](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/01.Introduction.pdf)** * **A Little History of C/C++ Programming Languages** * **Areas of Application and Popularity** * **C++ Philosophy** * **C++ Weakness** * **Books and References** * **The Course** **[2. Basic Concepts I - Fundamental Types and Operations](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/02.Basic_Concepts_I.pdf)** * **Preparation**: What compiler should I use?, What editor/IDE compiler should I use?, How to compile? * **Hello World**: I/O Stream * **C++ Fundamental Types Overview**: Arithmetic types, Non-standard arithmetic types, `void` type, Pointer type and `nullptr` * **Conversion Rules** * **`auto` declaration** * **C++ Operators**: Operators precedence, Prefix/Postfix increment/decrement, Assignment, Compound , and Comma operators, Spaceship operator `<=>` , Safe Comparison Operators **[3. Basic Concepts II - Integral and Floating-point Types](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/03.Basic_Concepts_II.pdf)** * **Integral Data Types**: Fixed width integers, `size_t` and`ptrdiff_t`, Signed/Unsigned integer characteristics, Promotion, Truncation, Undefined behavior * **Floating-point Types and Arithmetic**: IEEE Floating-point Standard and Other Representations, Normal/Denormal values, Infinity, Not a Number (`NaN`), Machine Epsilon, Units at the Last Place (ULP), Cheatsheet, Summary, Arithmetic Properties, Detect Floating-point Errors * **Floating-point Issues**: Catastrophic cancellation, Floating-point comparison **[4. Basic Concepts III - Entities and Control Flow](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/04.Basic_Concepts_III.pdf)** * **Entities** * **Declaration and Definition** * **Enumerators** * **`struct`, Bitfield, `union`** * **Control Flow**: `if` Statement, `for` Loop, Range-base `for` loop, `switch`, `goto`, Avoid unused variable warning `[[maybe unused]]` **[5. Basic Concepts IV - Memory Concepts](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/05.Basic_Concepts_IV.pdf)** * **Heap and Stack**: Stack Memory, `new`, `delete`, Non-allocating placement allocation, Non-throwing allocation, Memory leak * **Initialization**: Variable initialization, Uniform initialization, Fixed-size array initialization, Structure initialization, Dynamic memory initialization * **Pointers and References**: Pointer Operations, Address-of operator `&`, Reference * **Constant and Literals, `const`, `constexpr`, `consteval`, `constinit`**, `if constexpr`, `std::is constant evaluated()`, `if consteval` * **`volatile` keyword** * **Explicit Type Conversion**: `static_cast`, `const_cast`, `reinterpret_cast`, Type punning * `sizeof` Operator **[6. Basic Concepts V - Functions and Preprocessing](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/06.Basic_Concepts_V.pdf)** * **Functions**: Pass by-value, Pass by-pointer, Pass by-reference, Function signature and Overloading, Overloading and `=delete`, Default parameters, Attributes `[[attributes]]` * **Function Pointer and Function Objects** * **Lambda Expressions**: Capture list, Parameters, Composability, `constexpr/consteval`, `template`, `mutable`, `[[nodiscard]]`, Capture list and classes * **Preprocessing**: Preprocessors, Common errors, Source location macros, Conditional compiling macros, Stringizing operator (`#`), `#error` and `warning`, `#pragma`, Token-Pasting Operator (`##`), Variadic Macro **[7. Object-Oriented Programming I - Class Concepts](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/07.Object_Oriented_I.pdf)** * **C++ Classes**: RAII Idiom * **Class Hierarchy** * **Access specifiers**: Inheritance access specifiers, When use `public/protected/private` for data members? * **Class Constructor**: Default constructor, Class initialization, Uniform initialization for objects, Delegate constructor, `explicit` keyword, `[[nodiscard]]` and classes * **Copy Constructor** * **Class Destructor** * **Defaulted Constructors, Destructor, and Operators** (`= default`) * **Class Keywords**: `this`, `static`, `const`, `mutable`, `using`, `friend`, `delete` **[8. Object-Oriented Programming II - Polymorphism and Operator Overloading](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/08.Object_Oriented_II.pdf)** * **Polymorphism**: `virtual` methods, Virtual table, `override` keyword, `final` keyword, Common errors, Pure virtual method, Abstract class and interface * **Inheritance Casting and Run-time Type Identification** * **Operator Overloading**: Overview, Comparison operator `<`, Spaceship operator `<=>`, Subscript operator `[]`, Multidimensional Subscript operator `[]`, Function call operator `()`, static operator `[]` and operator `()`, Conversion operator `T()`, Return type overloading resolution, Increment and decrement operators `++`/`--`, Assignment operator `=`, Stream operator `<<`, Operator Notes * **C++ Special Objects**: Aggregate, Trivial class, Standard-layout class, Plain old data (POD), Hierarchy **[9. Templates and Meta-programming I - Function Templates and Compile-Time Utilities](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/09.Templates_I.pdf)** * **Function Template**: Overview, Template parameters, Template parameter - default value, Overloading, Specialization * **Template Variable** * **Template Parameter Types**: Generic Type Notes, `auto` Placeholder, Class template parameter type, Array and pointer types, Function type * **Compile-Time Utilities**: `static_assert`, `decltype` Keyword, `using` Keyword * **Type Traits**: Overview, Type traits library, Type manipulation **[10. Templates and Meta-programming II -  Class Templates and SFINAE](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/10.Templates_II.pdf)** * **Class Template**: Class specialization, Class template constructor, Constructor template automatic deduction (CTAD) * **Class Template - Advanced Concepts**: Class + Function - Specialization, Dependent Names - `typename` and `template` Keywords, Class template hierarchy and `using`, `friend` Keyword, Template Template Arguments * **Template Meta-Programming** * **SFINAE: Substitution Failure Is Not An Error**: Function SFINAE, Class SFINAE, Class + Function SFINAE * **Variadic Template**: Folding Expression, Variadic class template * **C++20 Concepts**: Overview, `concept` Keyword, `requires` Clause, `requires` Expression, `requires` Expression + Clause, `requires` Clause + Expression, `requires` and `constexpr`, Nested `requires` **[11. Translation Units I - Linkage and One Definition Rule](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/11.Translation_Units_I.pdf)** * **Basic Concepts**: Translation unit, Local and global scope, Linkage * **Storage Class and Duration**: Storage duration, Storage class, `static` and `extern` keywords, Internal/External linkage examples * **Linkage of `const` and `constexpr`**: Static Initialization Order Fiasco * **Linkage Summary** * **Dealing with Multiple Translation Units**: Class in multiple translation units * **One Definition Rule (ODR)**: Global variable issues, ODR - Point 3, `inline` functions/variables, `constexpr` and `inline` * **ODR - Function Template**: Cases, `extern` keyword * **ODR - Class Template**: Cases, `extern` keyword * **ODR Undefined Behavior and Summary** **[12. Translation Units II - Include, Module, and Namespace](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/12.Translation_Units_II.pdf)** - **`#include` Issues**: Forward declaration, Include guard, Circular dependencies, Common linking errors - **C++20 Modules**: Overview, Terminology, Visibility and Reachability, Module unit types, Keywords, Global module fragment, Private module fragment, Header module unit, Module partitions - **Namespace**: Namespace functions vs. `static` methods, Namespace alias, Anonymous namespace, `inline` namespace, Attributes and namespace - **Compiling Multiple Translation Units**: Fundamental compiler flags, Compile Methods, Deal with libraries, Build static/dynamic libraries, Find dynamic library dependencies, Analyze object/executable symbols **[13. Code Conventions](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/13.Code_Convention.pdf)** * **C++ Project Organization**: Project directories, Project files, “Common” Project Organization Notes, Alternative - “Canonical” project organization * **Coding Styles and Conventions** * **`#include`** * **Macro and Preprocessing** * **Namespace** * **Variables and Arithmetic Types** * **Functions** * **Structs and Classes** * **Control Flow** * **Modern C++ Features** * **Maintainability** * **Naming** * **Readability and Formatting** * **Code Documentation** **[14. Ecosystem I - Debugging](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/14.Ecosystem_I.pdf)** * **Debugging** * **Assertion** * **Execution debugging**: Breakpoints, Watchpoints / Catchpoints, Control Flow, Stack and Info, Print, Disassemble * **Memory Debugging**: `valgrind`, Stack protection * **Sanitizers**: Address sanitizer, Leak sanitizer, Memory sanitizers, Undefined behavior sanitizer * **Debugging Summary** * **Compiler Warnings** * **Static Analysis** * **Code Testing**: Unit test, Test-Driven Development (TDD), Code coverage, Fuzz testing * **Code Quality**: `clang-tidy` **[15. Ecosystem II - Cmake and Other Tools](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/15.Ecosystem_II.pdf)** - **CMake**: `cmake` and `ctest` - **Code Documentation**: `doxygen` - **Code Statistics**: Count lines of code, Cyclomatic complexity analyzer - **Other Tools**: Code formatting - `clang-format`, `Compiler Explorer`, Code transformation - `CppInsights`, Code autocompletion - `GitHub Co-Pilot/TabNine/Kite`, Local code search - `ripgrep`, Code search engine - `searchcode/grep.app`, Code benchmarking - `Quick-Bench`, Font for Coding **[16. Utilities](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/16.Utilities.pdf)** * **I/O Stream**: Manipulator, `ofstream/ifstream` * **Strings**: `std::string`, Conversion from/to numeric values, `std::string_view`, `std::format`, `std::print` * **Math Libraries** * **Random Number**: Basic Concepts, C++ ``, Seed, PRNG period and quality, Distribution, Quasi-random * **Time Measuring**: Wall-Clock time, User time, System time * **Std Class Templates**: `std::pair`, `std::tuple`, `std::variant`, `std::optional`, `std::any`, `std::stacktrace` * **Filesystem Library**: Query methods, Modify methods **[17. Containers, Iterators, and Algorithms](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/17.Iterators_Containers_Alg.pdf)** * **Containers and Iterators** * **Sequence Containers**: `std::array`, `std::vector`, `std::list`, `std::deque`, `std::forward_list` * **Associative Containers**: `std::set`, `std::map`, `std::multiset` * **Container Adaptors**: `std::stack`, `std::queue`, `std::priority_queue` * **View**: `std::span` * **Implement a Custom Iterator**: Semantic, Implement a simple Iterator * **Iterator Utility Methods**: `std::advance`, `std::next`, `std::prev`, `std::distance`, Container access methods, Iterator traits * **Algorithms Library**: `std::find_if`, `std::sort`, `std::accumulate`, `std::generate`, `std::remove_if` * **C++20 Ranges**: Key concepts, Range view, Range adaptor, Range factory, Range algorithms, Range actions **[18. Advanced Topics I](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/18.Advanced_Topics_I.pdf)** * **Move Semantic**: `lvalues` and `rvalues` references, Move semantic, `std::move`, Class Declaration Semantic * **Universal Reference and Perfect Forwarding**: Universal reference, Reference collapsing rules, Perfect forwarding * **Value Categories** * **`&`, `&&` Ref-qualifiers and `volatile` Overloading** * **Copy Elision and RVO** * **Type Deduction**: Pass by-reference, Pass by-pointer, Pass by-value, `auto` deduction * **`const` Correctness** **[19. Advanced Topics II](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/19.Advanced_Topics_II.pdf)** - **Undefined Behavior:** Common cases, Detecting undefined behavior - **Error Handling**: C++ Exceptions, Defining custom exceptions, `noexcept` keyword, Memory allocation issues, Alternative error handling approaches - **C++ Idioms**: Rule of zero/three/five, Singleton, PIMPL, CRTP, Template Virtual Functions - **Smart pointers**: `std::unique_ptr`, `std::shared_ptr`, `std::weak_ptr` - **Concurrency**: Thread Methods, Mutex, Atomic, Task-based parallelism **[20. Optimization I - Basic Concepts](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/20.Optimization_I.pdf)** * **Introduction**: Moore’s Law, Moore’s Law limitations, Reasons for Optimizing * **Basic Concepts**: Asymptotic complexity, Time-Memory Trade-off, Developing Cycle, Ahmdal's law, Throughput, Bandwidth, Latency, Performance bounds, Arithmetic intensity * **Basic Architecture Concepts**: Instruction-level parallelism (ILP), Little’s law, Data-level parallelism (SIMD), Thread-level parallelism (TLP), Single Instruction Multiple Threads (SIMT), RISC, CISC Instruction sets * **Memory Hierarchy**: Memory hierarchy concepts, Memory locality **[21. Optimization II - Code Optimization](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/21.Optimization_II.pdf)** * **I/O Operations**: `printf`, Memory mapped I/O, Speed up raw data loading * **Memory Optimizations**: Heap memory, Stack memory, Cache utilization, data alignment, Memory Prefetch * **Arithmetic**: Data types, Operations, Conversion, Floating-point, Compiler intrinsic functions, Value in a range, Lookup table * **Control Flow**: Loop hoisting, Loop unrolling, Branch hints `[[likely/unlikely]]`, Compiler hints `[[assume]]`, Recursion * **Functions**: Function call cost, Argument passing, Function optimizations, Function inlining, Pointers aliasing * **Object-Oriented Programming** * **Std Library and Other Language Aspects** **[22. Optimization III - Non-Coding Optimizations and Benchmarking](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/22.Optimization_III.pdf)** * **Compiler Optimizations**: About the compiler, Compiler optimization flags, Linker optimization flags, Architecture flags, Help the Compiler to produce better code, Profile guided optimization (PGO), Post-Processing Binary Optimizer * **Compiler Transformation Techniques**: Basic Transformations, Loop unswitching, Loop fusion, Loop fission, Loop interchange, Loop tiling * **Libraries and Data Structures**: External libraries * **Performance Benchmarking**: What to test?, Workload/Dataset quality, Cache behavior, Stable CPU performance, Program, Multi-threads considerations, Program memory layout, Measurement overhead, Compiler optimizations, Metric evaluation * **Profiling**: `gprof`, `uftrace`, `callgrind`, `cachegrind`, `perf` Linux profiler * **Parallel Computing**: Concurrency vs. Parallelism, Performance scaling, Gustafson’s Law, Parallel programming languages ### Roadmap 1. Software Design Chapter 2. Build Aspects Chapter (e.g. reducing build time) ### Essential Tool Online compiler and execution: [CompilerExplorer](https://godbolt.org/) * for code execution: [Add new..] -> [execution only] ### Reporting bugs 🐛 and contributing If you find any typo, conceptual error, or section to improve, please report them by using the `issue` panel. ## Author `Federico Busato` - Twitter: [twitter.com/fedebusato](https://twitter.com/fedebusato) - LinkedIn: [www.linkedin.com/in/federico-busato/](https://www.linkedin.com/in/federico-busato/)