Modern-CPP-Programming/README.md

240 lines
17 KiB
Markdown
Raw Normal View History

2019-03-04 11:34:32 +01:00
# Modern C++ Programming #
2021-11-28 16:06:01 +01:00
<img src="other/cpp_logo.png" />
2021-03-04 22:54:21 +01:00
## C++11 / C++14 / C++17 / (C++20) ##
2018-03-08 12:04:09 +01:00
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*
- Include the *last language standard* concepts and features
- *Practical teaching*: small structured descriptions associated to 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)** | 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)** | Integral data types, floating-point, operators, and conversion |
| **3** | **[Basic Concepts II - Entities and Control Flow](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/03.Basic_Concepts_II.pdf)** | Enumerators, structures, control flow statements |
| **4** | **[Basic Concepts III - Memory Management](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/04.Basic_Concepts_III.pdf)** | Heap, Stack, pointers, references, const property, conversion operators |
| **5** | **[Basic Concepts IV - Functions and Preprocessing](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/05.Basic_Concepts_IV.pdf)** | Functions, lambda expressions, preprocessing directives |
| **6** | **[C++ Object Oriented Programming I](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/06.Object_Oriented_I.pdf)** | Class hierarchy, constructor, destructor, class keywords |
| **7** | **[C++ Object Oriented Programming II](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/07.Object_Oriented_II.pdf)** | Polymorphism, operators overloading |
| **8** | **[C++ Templates and Meta-programming I](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/08.Templates_I.pdf)** | Function template, type traits |
2022-03-08 21:39:27 +01:00
| **9** | **[C++ Templates and Meta-programming II](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/09.Templates_II.pdf)** | Class template, SFINAE |
| **10** | **[Translation Units](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/10.Translation_Units.pdf)** | Dealing with multiple translation units and files, namespace |
| **11** | **[Code Conventions](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/11.Code_Convention.pdf)** | Project organization and main code conventions |
| **12** | **[Ecosystem](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/12.Ecosystem.pdf)** | Debugging, testing, building, documenting tools |
| **13** | **[Utilities](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/13.Utilities.pdf)** | Main `std` libraries |
| **14** | **[Containers, Iterators, and Algorithms](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/14.Iterators_Containers_Alg.pdf)** | Containers, iterators, algorithms |
| **15** | **[Advanced Topics](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/15.Advanced_Topics.pdf)** | Move semantics, error handling, C++ idioms |
| **16** | **[Optimization I](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/16.Optimization_I.pdf)** | Code optimizations, e.g. arithmetic, memory, etc. |
| **17** | **[Optimization II](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/17.Optimization_II.pdf)** | Compiler optimizations, profiling and benchmarking tools |
## TOPICS
2018-03-08 12:04:09 +01:00
2020-03-01 13:09:56 +01:00
**[1. Introduction](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/01.Introduction.pdf)**
2018-03-08 12:04:09 +01:00
2021-03-04 22:54:21 +01:00
* **A Little History of C/C++ Programming Languages**
* **Areas of Application and Popularity**
* **C++ Philosophy**
* **Books and References**
* **The Course**
**[2. Basic Concepts I - Fundamental Types](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`
2021-03-04 22:54:21 +01:00
* **Conversion Rules**
* **C++ Operators**: Operators precedence, Prefix/Postfix increment/decrement, Assignment, Compound , and comma operators, Spaceship operator `<=>`
* **Integral Data Types**: Fixed width integers, When use signed/unsigned integer? Promotion, Truncation, Undefined behavior
* **Floating-point Types and Arithmetic**: Normal/Denormal values, Not a Number (`NaN`), Infinity, Properties
* **Floating-point Issues**: Floating-point comparison, Catastrophic cancellation
2021-03-04 22:54:21 +01:00
**[3. Basic Concepts II - Entities and Control Flow](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/03.Basic_Concepts_II.pdf)**
* **Enumerators**
* **`struct`, `union`, and Bitfield**
* **`using`, `decltype`, and `auto`**
* **Control Flow**: `if` Statement, `for` Loop, `switch`, `goto`
**[4. Basic Concepts III - Memory Management](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/04.Basic_Concepts_III.pdf)**
* **Heap and Stack**: Stack Memory, `new`, `delete`, Memory Leak
* **Initialization**: Variable initialization, Uniform initialization, Structure initialization, Fixed-size array initialization, Dynamic array initialization
2021-03-04 22:54:21 +01:00
* **Pointers and References**: Pointer, Address-of operator `&`, Reference
* **`const`, `constexpr`, `consteval`, `constinit`**
* **Explicit Type Conversion**: `static_cast`, `const_cast`, `reinterpret_cast`, Type punning
2020-03-01 12:50:30 +01:00
* `sizeof` Operator
2019-03-04 11:29:53 +01:00
2021-03-04 22:54:21 +01:00
**[5. Basic Concepts IV - Functions and Preprocessing](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/05.Basic_Concepts_IV.pdf)**
2019-03-04 11:29:53 +01:00
2021-03-04 22:54:21 +01:00
* **Declaration and Definition**
* **Functions**: Pass by-value, Pass by-pointer, Pass by-reference, Overloading, Default parameters, Attributes
* **Function Objects and Lambda Expressions**: Function pointer, Function object (or Functor), Capture list, Other features, Capture list and classes
* **Preprocessing**: Preprocessors, Common errors, Useful macro, Stringizing operator (`#`), `#pragma` and `#error`, Token-Pasting Operator (`##`), Variadic Macro
2018-03-08 12:04:09 +01:00
2020-04-06 12:47:46 +02:00
**[6. C++ Object Oriented Programming I](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/06.Object_Oriented_I.pdf)**
2019-03-04 11:29:53 +01:00
* **C++ Classes**
* **Class Hierarchy**: Access specifiers, Inheritance access specifiers
* **Class Constructor**: Default constructor, Initializer list, Uniform initialization, Delegate constructor, `explicit` keyword
2021-03-04 22:54:21 +01:00
* **Copy Constructor**
* **Class Destructor**
* **Defaulted Members** (`= default`)
2021-03-04 22:54:21 +01:00
* **Class Keywords**: `this`, `static`, `const`, `mutable`, `using`, `friend`, `delete`
2019-03-04 11:29:53 +01:00
2020-04-06 12:47:46 +02:00
**[7. C++ Object Oriented Programming II](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/07.Object_Oriented_II.pdf)**
2018-03-08 12:04:09 +01:00
* **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, Subscript operator `[]`, Comparison operator `<`, Spaceship operator `<=>` Function call operator `()`, Conversion operator `T()`, Increment and decrement operators `++`/`--`, Assignment operator `=`, Stream operator `<<`, Operator Notes
* **C++ Special Objects**: Aggregate, Trivial class, Standard-layout class, Plain old data (POD), Hierarchy
2018-03-08 12:04:09 +01:00
2020-04-06 12:47:46 +02:00
**[8. C++ Templates and Meta-programming I](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/08.Templates_I.pdf)**
2018-03-08 12:04:09 +01:00
* **Function Template**: Overview, Template parameters, Template parameter - default value, Specialization, Overloading, `auto` deduction
2021-03-04 22:54:21 +01:00
* **Compile-Time Utilities**: `static_assert`, `decltype` Keyword, `using` Keyword
* **Type Traits**: Overview, Type traits library, Type manipulation, Type relation and transformation
* **Template Parameters**: Overview, Special cases
2018-03-08 12:04:09 +01:00
2020-04-06 12:47:46 +02:00
**[9. C++ Templates and Meta-programming II](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/9.Templates_II.pdf)**
2018-03-08 12:04:09 +01:00
* **Class Template**: Class specialization, Class + Function - specialization, `friend` keyword, Dependent names, Template variable
2021-03-04 22:54:21 +01:00
* **Template Meta-Programming**
* **SFINAE: Substitution Failure Is Not An Error**: Function SFINAE, Class SFINAE, Class + Function SFINAE
* **Variadic Template**: Folding Expression, Variadic Class Template
2018-03-08 12:04:09 +01:00
2020-04-06 12:47:46 +02:00
**[10. Translation Units](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/10.Translation_Units.pdf)**
2018-03-08 12:04:09 +01:00
* **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
* **Dealing with Multiple Translation Units**: One Definition Rule (ODR), `inline` functions/variables, Class in multiple translation units, Global variable issues
2021-03-04 22:54:21 +01:00
* **Function Template**
* **Class Template**
* **ORD and Undefined Behavior**
* **`#include` Issues**: Forward declaration, Include guard, Circular dependencies, Common linking errors
* **Namespace**: Namespace functions vs. `static` methods, Namespace alias, Anonymous namespace, `inline` namespace
* **How to Compile**: Compile strategies, Deal with libraries, Build static/dynamic libraries, Find dynamic library dependencies, Analyze object/executable symbols
2018-03-08 12:04:09 +01:00
2020-04-06 12:47:46 +02:00
**[11. Code Conventions](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/11.Code_Convention.pdf)**
2019-03-04 11:29:53 +01:00
* **C++ Project Organization**: Project directories, Project files, `src/include` directories
2021-03-04 22:54:21 +01:00
* **Coding Styles and Conventions**
* **`#include`**
2021-03-04 22:54:21 +01:00
* **Macro and Preprocessing**
* **Namespace**
* **Variables**
* **Functions**
* **Structs and Classes**
* **Control Flow**
2021-03-04 22:54:21 +01:00
* **Modern C++ Features**
* **Maintainability**
* **Naming and Formatting**
* **Code Documentation**
2018-03-08 12:04:09 +01:00
2020-04-06 12:47:46 +02:00
**[12. Ecosystem](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/12.Ecosystem.pdf)**
2019-03-04 11:29:53 +01:00
* **Debugging**: Assertion, Execution debugging (`gdb`)
* **Memory Debugging**: `valgrind`, Stack protection
* **Sanitizers**: Address sanitizer, Leak sanitizer, Memory sanitizers, Undefined behavior sanitizer
2021-03-04 22:54:21 +01:00
* **Debugging Summary**
* **Code Checking and Analysis**: Compiler warnings, Static analyzers
* **Code Testing**: Unit test, Code coverage, Fuzz testing
2021-03-04 22:54:21 +01:00
* **Code Quality**: `clang-tidy`
* **CMake**: `cmake` and `ctest`
2021-03-04 22:54:21 +01:00
* **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 - `TabNine/Kite`, Local code search - `ripgrep`, Code search engine - `searchcode/grep.app`, Code exploration - `SourceTrail`, Code benchmarking - `Quick-Bench`, Font for Coding
2018-03-08 12:04:09 +01:00
2020-04-06 12:47:46 +02:00
**[13. Utilities](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/13.Utilities.pdf)**
2021-03-04 22:54:21 +01:00
* **I/O Stream**: Manipulator, `ofstream/ifstream`
* **Math Libraries**
* **Strings**: `std::string`, Conversion from/to numeric balues, `std::string view`, `std::format`
* **Random Number**: Basic Concepts, C++ `<random>`, Seed, PRNG period and quality, Distribution, Quasi-random
* **Time Measuring**: Wall-Clock time, User time, System time
2021-03-04 22:54:21 +01:00
* **Std Template Classes**: `std::pair`, `std::tuple`, `std::variant`, `std::optional`, `std::any`
* **Filesystem Library**: Query methods, Modify methods
2020-04-06 12:47:46 +02:00
**[14. Containers, Iterators, and Algorithms](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/14.Iterators_Containers_Alg.pdf)**
2021-03-04 22:54:21 +01:00
* **Containers and Iterators**
* **Sequence Containers**: `std::array`, `std::vector`, `std::list`, `std::deque`, `std::forward_list`
* **Associative Containers**: `std::set`, `std::map`, `std::multiset`
2021-03-04 22:54:21 +01:00
* **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`, Range access methods, Iterator traits
2021-03-04 22:54:21 +01:00
* **Algorithms Library**: `std::find_if`, `std::sort`, `std::accumulate`, `std::generate`, `std::remove_if`
2020-04-06 12:47:46 +02:00
2020-03-01 13:09:56 +01:00
**[15. Advanced Topics](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/15.Advanced_Topics.pdf)**
2018-03-08 12:04:09 +01:00
* **Move Semantic**: `lvalues` and `rvalues` references, Move semantic, Compiler implicitly declared, `std::move`
* **Universal Reference and Perfect Forwarding**: Universal reference, Reference collapsing rules, Perfect forwarding
2021-03-04 22:54:21 +01:00
* **Value Categories**
* **`&`, `&&` Overloading - Ref-qualifiers**
2021-03-04 22:54:21 +01:00
* **Copy Elision and RVO**
* **Type Deduction**: Pass by-reference, Pass by-pointer, Pass by-value
2021-03-04 22:54:21 +01:00
* **`const` Correctness**
* **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
2021-03-04 22:54:21 +01:00
* **Smart pointers**: `std::unique_ptr`, `std::shared_ptr`, `std::weak_ptr`
* **Concurrency**: Thread Methods, Mutex, Atomic, Task-based parallelism
2019-03-04 11:29:53 +01:00
2020-03-01 13:09:56 +01:00
**[16. Optimization I](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/16.Optimization_I.pdf)**
2019-03-04 11:29:53 +01:00
* **General Concepts**: Asymptotic complexity, Optimization cycle, Ahmdal's Law, Throughput, Bandwidth, Latency, Performance Bounds, Arithmetic Intensity
* **Basic Architecture Concepts**: Instruction-level parallelism, Littles Law, Data-level parallelism, thread-level parallelism, RISC, CISC Instruction sets
* **I/O Operations**: `printf`, Memory Mapped I/O, Speed up raw data loading
* **Locality and Memory Access Patterns**: Memory hierarchy, Memory locality, Internal structure alignment, External structure alignment
* **Arithmetic**: Data types, Operations, Conversion, Floating-point, Compiler intrinsic functions, Value in a Range, Lookup Table
* **Control Flow**: Loop hoisting, Loop unrolling, Branch hints, Recursion
* **Functions**: Function call cost, Argument passing, Function optimizations, Function inlining, Pointers Aliasing
2021-03-04 22:54:21 +01:00
* **C++ Objects**: C++ Objects Optimizations
2020-03-01 12:50:30 +01:00
2020-03-01 13:09:56 +01:00
**[17. Optimization II](https://github.com/federico-busato/Modern-CPP-Programming/blob/master/17.Optimization_II.pdf)**
2020-03-01 12:50:30 +01:00
* **Compiler Optimizations**: About the compiler, Architecture flags, Optimization flags, Help the Compiler to produce better code, Profile guided optimization (PGO)
2021-03-04 22:54:21 +01:00
* **Compiler Transformation Techniques**
* **Libraries and Data Structures**: External libraries, Std library
2021-03-04 22:54:21 +01:00
* **Profiling**: `gprof`, `uftrace`, `callgrind`, `cachegrind`, `perf` Linux profiler
* **Performance Benchmarking**: What to test?, Workload/Dataset quality, Cache behavior, Stable CPU performance, Program memory layout
* **Parallel Computing**: Concurrency vs. Parallelism, Performance scaling, Gustafsons Law, Parallel programming languages
2021-03-04 22:54:21 +01:00
### Roadmap
1. Complete C++20 topics
2. Software Design Chapter
3. Build Aspects Chapter (e.g. reducing build time)
2021-03-04 22:54:21 +01:00
### Essential Tool ###
Online compiler and execution: [CompilerExplorer](https://godbolt.org/)
* for code execution: [Add new..] -> [execution only]
2018-03-08 12:04:09 +01:00
### Reporting bugs and contributing ###
2020-03-01 13:09:56 +01:00
If you find any typos, conceptual errors, or sections to improve, please report them by writing directly to me or by using the `issue` panel
2018-03-08 12:04:09 +01:00
## Author ##
2021-05-20 13:12:28 +02:00
`Federico Busato`
- Nvidia Corporation [fbusato@nvidia.com](mailto:fbusato@nvidia.com)
- Dept. of Computer Science, University of Verona, Italy [federico.busato@univr.it](mailto:federico.busato@univr.it)
- Twitter: [twitter.com/fedebusato](https://twitter.com/fedebusato)
- Linkedin: [www.linkedin.com/in/federico-busato/](https://www.linkedin.com/in/federico-busato/)