Embedded C++: Classes, RAII and Templates for Microcontrollers
C++ can make firmware safer and clearer without making it bigger or slower - if you know which parts to use. This course starts from the C you already know and adds C++ one idea at a time: classes, cleanup that cannot be forgotten, templates the compiler works out for you. Every example is compiled and run, and every cost is measured, not guessed.
Start here
Start with Volume 00. It explains why C++ is used on microcontrollers, which parts of the language fit on a small chip and which do not, and the two compiler flags every embedded C++ build uses. Then take the volumes in order - each one uses only what came before it.
This course assumes you can read C: variables, functions, pointers and structs. If you cannot yet, take Embedded C from Zero first - it assumes nothing, and this course carries on where it stops.
Every example here was compiled with warnings treated as errors and run before it was published, so the output you see is the output you get. Where a lesson says a feature costs a number of bytes, that number was measured. Each new word is underlined with dots - point at it (or tap it) to see what it means, and every sub-module ends with a quick check.
Full syllabus
Part I - From C to C++
- 00 Start Here: C++ on a Microcontroller Why C++ is used on microcontrollers, the myths and the real costs - measured - the subset that fits on a small chip, and the two compiler flags every embedded C++ build uses. Live
- 01 C++ for C Programmers The small differences that make C++ nicer to write: references, namespaces, overloading and default arguments, extern "C" for interrupt handlers, nullptr, bool and auto, and the C++ casts. Live
Part II - Objects that manage themselves
- 02 Classes and Objects Keeping data and the code that uses it together: access control, constructors and destructors, a GPIO pin class, const member functions and static members. Live
- 03 RAII: Cleanup You Cannot Forget Resources that clean up after themselves: scoped interrupt locks, scoped peripheral enables, and the rule of zero, three and five explained simply. Live
- 04 Inheritance and Polymorphism Inheritance, virtual functions and what a vtable really costs, interfaces for drivers, when to avoid virtual, and CRTP: polymorphism decided at compile time. Live
Part III - Work done by the compiler
- 05 Templates and Compile-Time Code Function and class templates, constexpr and static_assert, type-safe registers built from templates, and zero-cost abstractions - measured in the machine code. Live
- 06 Memory Without the Heap Where objects live, std::array and fixed-size containers, placement new, living without exceptions and RTTI, and the static initialisation order problem. Live
- 07 Modern C++ for Firmware The newer C++ features that help firmware most: enum class, lambdas as callbacks, std::optional and std::span, move semantics simply, and attributes such as [[nodiscard]]. Live
Part IV - Real firmware
- 08 Designing a Driver Layer (HAL) Layered firmware architecture, hardware abstraction interfaces, dependency injection, and a complete UART driver written in C++. Live
- 09 Testing Firmware on Your PC Why firmware should be tested off the board, unit tests, mocking hardware, and running the tests automatically on every change. Live
Part V - Revision and interviews
What you need before starting
| What | Why | Free option |
|---|---|---|
| Some C | Pointers, structs and functions are used from the first page | Embedded C from Zero, Volumes 00 to 08 |
| A web browser | To compile and run C++ without installing anything | Compiler Explorer (it can also show the ARM instructions for a microcontroller) |
| Optional: g++ on your computer | To work offline, with the same flags as the course | GCC is free on Linux and macOS; on Windows, use WSL or MinGW |
| Optional: a microcontroller board | To run the drivers on real hardware | Any board with a C++ toolchain works; the course never needs one |