Templates and generic code

This part of the C++ course writes code once and compiles it for many types. A function template comes first, then a class template and its specialisations, a variadic pack and the fold expression that sums it. Type traits turn a branch that would not compile into one that does, and concepts say the requirement out loud. Alias templates, perfect forwarding, the curiously recurring template pattern and a small metaprogram close the part.

037 Function templates a template parameter, deduction from arguments, instantiation and the code the compiler writes Open 038 Class templates a container template, member functions, default template arguments and explicit arguments Open 039 Template specialisation a full specialisation for one type, a partial specialisation for a family, and tag dispatch Open 040 Variadic templates a parameter pack, recursive unpacking, sizeof... and a print that takes anything Open 041 Fold expressions fold over +, && and the comma operator, and a sum that takes any number of arguments Open 042 Type traits and if constexpr is_integral, remove_reference, enable_if, if constexpr and branches that never compile Open 043 Concepts a named requirement, the requires clause, abbreviated templates and the error a concept prevents Open 044 Constraints and requires expressions requires expressions, compound requirements, subsumption and constrained overloads Open 045 Type aliases and alias templates using for a name, alias templates, dependent names and typename Open 046 Forwarding references and std::forward T&& deduction, reference collapsing, std::forward and a factory that loses nothing Open 047 The curiously recurring template pattern static polymorphism, a base that knows its derived type, and CRTP against virtual calls Open 048 Template metaprogramming compile-time recursion, type lists, std::integer_sequence and when to stop Open

Lessons