Learn C++

Fast, zero-overhead systems language for performance-critical software

42 lessons · 10 hours · 4 levels · covers C++ C++20

C++ is a compiled, statically typed language that combines low-level control over memory and hardware with high-level abstractions such as classes, templates and the standard library. It powers game engines, browsers, databases, trading systems, embedded devices and machine-learning runtimes. Learning modern C++ (C++20, with C++23 notes) teaches you how software really runs and lets you write code that is both expressive and as fast as the machine allows.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics12 lessons · 2 hours in total
2. PracticalUse it productively at work, with the usual idioms26 lessons · 5 hours in total
3. AdvancedThe less common features, performance and trade-offs35 lessons · 8 hours in total
4. ExpertInternals and the edge cases experts know42 lessons · 10 hours in total

The C++ tree

C++Compiling and runningCompiling andrunningmain, input and outputmain, input andoutputVariables and fundamental typesVariables andfundamental typesauto and brace initializationauto and braceinitializationOperators and expressionsOperators andexpressionsBranches and loopsBranches and loopsFunctionsFunctionsArrays and std::vectorArrays andstd::vectorReferencesReferencesstd::string and string_viewstd::string andstring_viewOverloading and default argumentsOverloading anddefault argumentsStructs and classesStructs and classesConstructors, destructors and RAIIConstructors,destructors and RAIIConst correctnessConst correctnessPointersPointersSmart pointersSmart pointersInheritance and virtual functionsInheritance andvirtual functionsOperator overloadingOperator overloadingScoped enums (enum class)Scoped enums (enumclass)Standard containersStandard containersIteratorsIteratorsLambdasLambdasStandard algorithmsStandard algorithmsHeaders, namespaces and the ODRHeaders,namespaces and th…ExceptionsExceptionsBuilding with CMakeBuilding with CMake
Essentials and Practical shown (26 of 42 lessons); the full list is below. Click a lesson to read it.

All 42 lessons, in order

First steps

  1. Compiling and running · Essentials · 10 min

    Turn .cpp source files into an executable with a compiler and linker, and switch on the warnings and checks that catch bugs early.

  2. main, input and output · Essentials · 10 min

    Write a complete program: the main function, printing with std::cout and reading input with std::cin.

  3. Variables and fundamental types · Essentials · 12 min

    Declare variables of the built-in types (int, double, bool, char and friends) and know their sizes, limits and arithmetic rules.

  4. auto and brace initialization · Essentials · 10 min

    Let the compiler deduce types with auto, and use brace initialization to get safe, uniform initialization without narrowing.

  5. Operators and expressions · Essentials · 10 min

    Arithmetic, comparison, logical and bitwise operators, plus the precedence and conversion rules that trip people up.

  6. Branches and loops · Essentials · 12 min

    Control flow with if, switch, while, do-while, classic for and range-based for, including if-with-initializer.

  7. Functions · Essentials · 12 min

    Declare and define functions, pass arguments by value, return results, and understand declarations versus definitions.

  8. References · Essentials · 12 min

    Use references as aliases to pass large objects without copying and to let functions modify their arguments.

  9. Overloading and default arguments · Essentials · 10 min

    Give several functions the same name with different parameter types, and make trailing parameters optional with defaults.

Core data types

  1. Arrays and std::vector · Essentials · 12 min

    Store sequences of values: built-in arrays, fixed-size std::array, and std::vector, the dynamic array you should reach for first.

  2. std::string and string_view · Essentials · 12 min

    Work with text using std::string, and pass read-only text cheaply with std::string_view.

  3. Structs and classes · Essentials · 14 min

    Group data and behaviour into your own types with struct and class, member functions and public/private access.

Classes and object lifetime

  1. Constructors, destructors and RAII · Practical · 15 min

    Initialise objects with constructors, clean up with destructors, and tie every resource to an object's lifetime (RAII).

  2. Const correctness · Practical · 12 min

    Mark what must not change with const: variables, reference parameters and member functions, so the compiler enforces it.

  3. Inheritance and virtual functions · Practical · 15 min

    Build class hierarchies with public inheritance, override virtual functions for runtime polymorphism, and use virtual destructors.

  4. Operator overloading · Practical · 12 min

    Give your types natural syntax: arithmetic, comparisons with C++20's spaceship operator, and stream output.

  5. Scoped enums (enum class) · Practical · 8 min

    Define named sets of constants with enum class, which are scoped, strongly typed and can choose their underlying type.

  6. Move semantics and rvalue references · Advanced · 18 min

    Transfer resources instead of copying them: rvalue references, std::move, move constructors and copy elision.

  7. Rule of zero, three and five · Advanced · 14 min

    Decide which special member functions a class needs, prefer letting members manage resources, and use =default and =delete.

  8. Object lifetime and layout · Expert · 20 min

    How objects occupy memory: storage versus lifetime, construct_at and destroy_at, alignment and padding, trivial types and bit_cast.

Memory and performance

  1. Pointers · Practical · 15 min

    Understand raw pointers: addresses, dereferencing, nullptr, pointer arithmetic, and their modern role as non-owning observers.

  2. Smart pointers · Practical · 15 min

    Own heap objects safely with std::unique_ptr, share them with std::shared_ptr, and break cycles with std::weak_ptr.

  3. Undefined behaviour · Advanced · 15 min

    Know what undefined behaviour is, the common ways to trigger it, why optimisers make it dangerous, and the tools that find it.

  4. Allocators and polymorphic memory resources · Expert · 18 min

    Control where containers get memory: the allocator model, std::pmr memory resources, arenas and pools.

  5. Performance and cache-friendly code · Expert · 20 min

    Write fast C++: measure first, then exploit caches with contiguous data, avoid needless allocations and copies, and prevent false sharing.

Standard library

  1. Standard containers · Practical · 15 min

    Choose between vector, deque, map, set, unordered_map and friends, and use their lookup and insertion APIs correctly.

  2. Iterators · Practical · 12 min

    Traverse any container with iterators, understand half-open ranges and iterator categories, and avoid invalidation bugs.

  3. Lambdas · Practical · 12 min

    Write inline function objects with lambdas: captures by value and by reference, generic lambdas, and storing callables.

  4. Standard algorithms · Practical · 15 min

    Replace hand-written loops with std::sort, find_if, transform, accumulate and the rest of <algorithm> and <numeric>.

  5. Ranges and views · Advanced · 15 min

    Use C++20 ranges: algorithms on whole containers with projections, and lazy, composable views joined with |.

Building and linking

  1. Headers, namespaces and the ODR · Practical · 15 min

    Split code into headers and source files, organise names with namespaces, and follow the one-definition rule.

  2. Building with CMake · Practical · 15 min

    Describe a project with targets in CMakeLists.txt, set the C++ standard and warnings, link libraries and run tests.

  3. Linking, libraries and the ABI · Expert · 18 min

    How the linker resolves symbols, name mangling, static versus shared libraries, ABI compatibility, extern "C" and the pimpl idiom.

Error handling

  1. Exceptions · Practical · 14 min

    Report errors with throw, handle them with try/catch, and rely on RAII and noexcept for exception-safe code.

  2. optional, variant and expected · Advanced · 15 min

    Model maybe-values with std::optional, one-of-several types with std::variant and std::visit, and errors as values with C++23 std::expected.

Generic programming

  1. Templates · Advanced · 18 min

    Write generic code with function and class templates, deduction, non-type parameters, CTAD and variadic templates.

  2. Concepts and constraints · Advanced · 15 min

    Constrain templates with C++20 concepts and requires clauses for clear errors and overloading by capability.

  3. constexpr and compile-time code · Advanced · 15 min

    Run code at compile time with constexpr, consteval and constinit, and check assumptions with static_assert.

  4. Template metaprogramming and SFINAE · Expert · 20 min

    Compute with types: type traits, SFINAE and enable_if, the detection idiom, if constexpr, and how concepts replace them.

Concurrency and coroutines

  1. Threads, mutexes and atomics · Advanced · 18 min

    Run work in parallel with std::jthread, protect shared data with mutexes and scoped locks, and use atomics for simple counters.

  2. Memory model and atomic ordering · Expert · 20 min

    Understand happens-before, and choose between seq_cst, acquire/release and relaxed memory orders for atomics.

  3. Coroutines under the hood · Expert · 25 min

    How C++20 coroutines work: the coroutine frame, promise_type, coroutine_handle, awaiters, and C++23 std::generator.