English
Summary
Conclusions
A template describes a shared algorithm once, and the compiler generates the required specializations for arguments known at compile time. Type deduction does not look for a convenient common type, so the template signature is part of the contract. A class template can have a non-type parameter, and then FixedStack<int, 2> and FixedStack<int, 3> are different types, while CTAD deduces the arguments only from a constructor. A class specialization replaces the behavior for specified arguments, and for functions you usually use overloading or constraints. For implicit instantiation, the template definition must be visible, so it is placed in a header. Parameter packs and fold expressions are expanded at compile time and are not a run-time container. Concepts and requires express exact requirements for types and move diagnostics closer to the interface, but they do not prove that the operations are semantically correct. Static polymorphism selects an implementation at instantiation time and does not replace a virtual interface for heterogeneous collections.
Self-check questions
- Why does
maxOf(3, 7.5)not deduce a single T? - Which types and operations does the training FixedStack actually support?
- Why can CTAD not deduce the capacity from an empty constructor?
- How does a class specialization differ from function overloading?
- What information is needed for implicit instantiation?
- How does a requires clause differ from a requires expression?
- Does a concept prove that a comparison operator is semantically correct?
- Why does an empty fold over the comma work, while one over + needs a decision?
- Where should you use
static_assertinstead of a concept? - When do you need dynamic selection of an implementation instead of a template?
Review questions for the lab
- What is a type argument, and what is a non-type argument?
- Which constraints of your type are checked by the compiler?
- Which properties remain run-time checks?
- How does the CTAD guarantee differ from a constructor check?
- Why can a failure test not be replaced with a comment?