Skip to content
binshelf/ cpp

Search lessons

Type a C++ term, like vector, reference, unique_ptr or use-after-free.

    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

    1. Key terms

      The new words and symbols, each in one line, before you start.

    2. In 30 seconds

      The whole idea in a few sentences.

    3. Examples

      Small, complete programs with their real output. Run any of them in your browser.

    4. Which one to use

      What each option is, when to use it, why, and how.

    5. Under the hood

      Memory diagrams, sizes and real compiler and sanitizer messages.

    6. Try it

      A short exercise with a hint and a checked solution.

    Curriculum

    1. 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.

      1. 0.1How a C++ program is built
      2. 0.2Setting up: compilers, warnings and sanitizers

    2. Module 1 Basics

      Variables, the number types and what their sizes mean, operators including bit operations, control flow, functions, and printing and reading text.

      1. 1.1Variables and initialization
      2. 1.2Integers and floating-point numbers
      3. 1.3Operators and bit operations
      4. 1.4Control flow
      5. 1.5Functions
      6. 1.6Printing and reading: std::print, std::format and streams

    3. 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.

      1. 2.1Stack, heap and lifetime
      2. 2.2References
      3. 2.3Pointers
      4. 2.4Arrays, std::array and std::span
      5. 2.5Dynamic memory and RAII
      6. 2.6Smart pointers: unique_ptr and shared_ptr

    4. 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.

      1. 3.1string and string_view
      2. 3.2vector
      3. 3.3Maps and sets
      4. 3.4optional, variant and tuple

    5. Module 4 Your own types

      Structs and classes, constructors and the rules for copying and moving, operators, enums, and inheritance with virtual functions.

      1. 4.1Structs and classes
      2. 4.2Constructors, destructors and the rule of zero
      3. 4.3Copy and move
      4. 4.4Operators and comparisons with <=>
      5. 4.5enum class
      6. 4.6Inheritance and virtual functions

    6. 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.

      1. 5.1Exceptions
      2. 5.2optional and expected for errors
      3. 5.3Undefined behavior, assertions and sanitizers

    7. 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.

      1. 6.1Function templates
      2. 6.2Class templates
      3. 6.3Concepts
      4. 6.4constexpr and consteval

    8. Module 7 Lambdas and algorithms

      Lambdas, iterators and the standard algorithms, and ranges and views for building pipelines over data.

      1. 7.1Lambdas
      2. 7.2Iterators and algorithms
      3. 7.3Ranges and views

    9. Module 8 Organizing a project

      Splitting code into headers and source files, building it with CMake, C++ modules, and writing tests.

      1. 8.1Headers, source files and the one-definition rule
      2. 8.2CMake and libraries
      3. 8.3Modules
      4. 8.4Testing

    10. 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.

      1. 9.1Layout, alignment and padding
      2. 9.2Caches and data-oriented design
      3. 9.3Optimization levels and reading assembly
      4. 9.4SIMD
      5. 9.5Bits, bit_cast and endianness

    11. Module 10 Concurrency

      Running work on several threads, sharing data safely with mutexes and atomics, and splitting a job across cores.

      1. 10.1Threads and jthread
      2. 10.2Mutexes and atomics
      3. 10.3Parallel work

    12. 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.

      1. 11.1Tensors with mdspanComing later
      2. 11.2Matrix multiplication, from naive to fastComing later
      3. 11.3Half precision and quantizationComing later
      4. 11.4Capstone: a tiny inference engineComing later
      5. 11.5Calling your C++ from PythonComing later
    13. 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.

      1. 12.1Reading FASTA and FASTQ
      2. 12.22-bit DNA and k-mers
      3. 12.3Sequence alignment
      4. 12.4Processing reads in parallel

    14. 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.

      1. 13.1The C ABIComing later
      2. 13.2C++ in the browser with WebAssemblyComing later