Modern C++, step by step
Learn C++ one idea at a time, then build fast tools with it.
For people who have written some code before, in any language. Each lesson explains one idea in plain words, shows it in a small program, and then looks at what happens in memory. Every example on this site is compiled with GCC and Clang and run before it's published.
A taste of modern C++
Count the bases in a DNA string. Select Run to try it in Compiler Explorer.
#include <map>
#include <print>
#include <string_view>
int main() {
std::string_view dna = "GATTACA";
std::map<char, int> counts;
for (char base : dna) {
++counts[base];
}
for (auto [base, n] : counts) {
std::println("{}: {}", base, n);
}
}Output
A: 3 C: 1 G: 1 T: 2
How each lesson works
- Key terms
The new words and symbols, each in one line, before you start.
- In 30 seconds
The whole idea in a few sentences.
- Examples
Small, complete programs with their real output. Run any of them in your browser.
- Which one to use
What each option is, when to use it, why, and how.
- Under the hood
Memory diagrams, sizes and real compiler and sanitizer messages.
- Try it
A short exercise with a hint and a checked solution.
Curriculum
Module 0 Getting started
What happens between your source file and a running program, and the setup you'll use for every lesson: a compiler, warnings, sanitizers and CMake.
Module 1 Basics
Variables, the number types and what their sizes mean, operators including bit operations, control flow, functions, and printing and reading text.
Module 2 Memory
Where values live and for how long: the stack and the heap, references and pointers, arrays and spans, and how RAII and smart pointers free memory for you.
- 2.1Stack, heap and lifetimeDone
- 2.2ReferencesDone
- 2.3PointersDone
- 2.4Arrays, std::array and std::spanDone
- 2.5Dynamic memory and RAIIDone
- 2.6Smart pointers: unique_ptr and shared_ptrDone
Module 3 Strings and containers
Text with std::string and std::string_view, the containers you'll use most, and types that hold a value that may be missing or one of several kinds.
- 3.1string and string_viewDone
- 3.2vectorDone
- 3.3Maps and setsDone
- 3.4optional, variant and tupleDone
Module 4 Your own types
Structs and classes, constructors and the rules for copying and moving, operators, enums, and inheritance with virtual functions.
- 4.1Structs and classesDone
- 4.2Constructors, destructors and the rule of zeroDone
- 4.3Copy and moveDone
- 4.4Operators and comparisons with <=>Done
- 4.5enum classDone
- 4.6Inheritance and virtual functionsDone
Module 5 Errors and undefined behavior
Three ways to report a failure (exceptions, std::optional and std::expected), and how to find undefined behavior before it finds you.
Module 6 Generic code
Function and class templates, concepts that say what a template needs, and work done at compile time with constexpr and consteval.
- 6.1Function templatesDone
- 6.2Class templatesDone
- 6.3ConceptsDone
- 6.4constexpr and constevalDone
Module 7 Lambdas and algorithms
Lambdas, iterators and the standard algorithms, and ranges and views for building pipelines over data.
- 7.1LambdasDone
- 7.2Iterators and algorithmsDone
- 7.3Ranges and viewsDone
Module 8 Organizing a project
Splitting code into headers and source files, building it with CMake, C++ modules, and writing tests.
- 8.1Headers, source files and the one-definition ruleDone
- 8.2CMake and librariesDone
- 8.3ModulesDone
- 8.4TestingDone
Module 9 Performance
How data is laid out in memory, why caches decide speed, reading what the compiler produced, SIMD, and working with raw bits.
- 9.1Layout, alignment and paddingDone
- 9.2Caches and data-oriented designDone
- 9.3Optimization levels and reading assemblyDone
- 9.4SIMDDone
- 9.5Bits, bit_cast and endiannessDone
Module 10 Concurrency
Running work on several threads, sharing data safely with mutexes and atomics, and splitting a job across cores.
- 10.1Threads and jthreadDone
- 10.2Mutexes and atomicsDone
- 10.3Parallel workDone
Module 11 C++ for AI
The building blocks of AI inference in plain C++: tensors as multidimensional views, fast matrix multiplication, small number formats, and a tiny inference engine you can call from Python.
- 11.1Tensors with mdspanComing later
- 11.2Matrix multiplication, from naive to fastComing later
- 11.3Half precision and quantizationComing later
- 11.4Capstone: a tiny inference engineComing later
- 11.5Calling your C++ from PythonComing later
Module 12 C++ for bioinformatics
Reading sequence files, packing DNA into bits, counting k-mers, aligning sequences with dynamic programming, and processing reads in parallel.
- 12.1Reading FASTA and FASTQDone
- 12.22-bit DNA and k-mersDone
- 12.3Sequence alignmentDone
- 12.4Processing reads in parallelDone
Module 13 Interop and WebAssembly
Calling C++ from other languages through the C ABI and Python bindings, and running it in the browser as WebAssembly with Emscripten.
- 13.1The C ABIComing later
- 13.2C++ in the browser with WebAssemblyComing later