C++ templates and generic programming
You design type-safe, reusable libraries with function and class templates, type traits, concepts and variadic templates.
Level 401 · 17 lessons · 15 practice labs
Lessons
Template fundamentals
- Function templates and type deduction
- Class templates and CTAD
- Full and partial specialisation
- Non-type template parameters
- Short task: a generic bounded buffer template
Metaprogramming
- Type traits
- SFINAE and enable_if: the old way
- Writing your own concepts and requires expressions
- Variadic templates and fold expressions
- Compile-time branches with if constexpr
- Short task: a type-safe formatting function
Generic design
- Static polymorphism with CRTP
- Type erasure: a wrapper like std::function
- Policy-based design
- Reading template errors and cutting compile times
- Short task: strong types that prevent unit mix-ups
Capstone
- Capstone: a small generic library constrained with concepts
Practice labs
- Writing a generic clamp function: Write a function template that clamps a value between a lower and an upper bound and resolve the deduction error on mixed-type calls.
- Adding a deduction guide to a range class: Write a class template holding a lower and upper end and add a deduction guide so it can be constructed without naming the type.
- A ring buffer sized by a template parameter: Write a ring buffer whose capacity is a non-type template parameter with no heap allocation and cover full, empty and move-only element cases with tests.
- Writing a trait that detects containers: Write a type trait that detects types with begin and end and verify it on ten types with static_assert.
- Specialising serialisation by type: Give a generic serialise template full specialisations for bool and string and a partial one for pointers and test each yields the expected text.
- Simplifying an enable_if pile with if constexpr: Rewrite a conversion function written as four SFINAE overloads in one body with if constexpr while behaviour and compile-failure tests stay the same.
- Defining a concept for printable types: Write a concept describing types that can be written to a stream and constrain a print function with it.
- Describing a storage backend with requires: Write a requires expression that checks the read, write and delete signatures and show a type missing a function is rejected with a clear error.
- Fixing overload ranking with concepts: Remove the ambiguous-call error in a distance function with versions for random-access and forward iterators through concept subsumption and prove the right one is chosen.
- Summing with a fold expression: Write two functions that sum a variable number of arguments with a fold expression and check that all are positive.
- Building a log line from variadic arguments: Write a variadic log function that writes its arguments to a stream with a separator between them and handles the zero-argument case.
- Writing a type-safe formatting function: Write a formatting function that compares placeholder and argument counts at compile time so a mismatched call fails to compile and valid calls give the right text.
- Separating units with strong types: Define metres and seconds as separate strong types so code adding the two fails to compile while like units can be added.
- Giving comparison operators with CRTP: Give classes that write only less-than the other comparisons through a CRTP base and show no virtual call is involved.
- Writing a type-erased task wrapper: Write a std::function-like type-erased wrapper that can also hold move-only callables and avoids heap allocation for small objects.
About this course
You design type-safe, reusable libraries with function and class templates, type traits, concepts and variadic templates.
What you will learn
- Template fundamentals
- Metaprogramming
- Generic design
- Capstone
Prerequisites
You should have practised the topics of the same path up to level 301.
Lab environment
Lessons run in your own Kali Linux terminal that opens in the browser. Nothing to install; the lab starts clean in every lesson.
Frequently asked questions
- Is this course right for my level?
- The course is at level 401; it assumes you know the topics of the lower-level courses.
- Is there anything to install?
- No. Every lesson runs in your own lab environment that opens in the browser.
- How do lessons work?
- Each lesson has 8 to 10 steps and a short quiz. Early steps show the command to type, later steps give less help; if two attempts fail, the solution opens.
- Can I try it for free?
- Yes. The first three lessons of every course are free.