Templates as a Language
Template metaprogramming comes up at a handful of firms, one of which has published a compile-time exercise on its own engineering blog. It is the most differentiated topic in this course, because it is essentially unavailable on generic interview platforms: their grading model runs a program against test inputs, and compile-time work grades by whether the build succeeds, which is a harness almost none of them have. (Ours does: the drills below this section grade through static_assert.)
What a template is
A template is not a function or a class. It is a pattern from which the compiler generates one, per distinct set of arguments, at the point of use. Nothing exists until it is instantiated, which has two consequences that explain most template behaviour.
Errors in type-dependent code appear at instantiation, not at definition. A template body may contain an operation the type does not support, and it compiles fine until someone uses it with such a type. (Mistakes that do not depend on the template parameter are still diagnosed at definition.) That is why template error messages name a call site pages away from the mistake.
The rest of this lesson is for subscribers
Unlock every lesson in Systems Programming for Trading, and every other premium course.
Subscribe to continueTest your knowledge
Keep reading Systems Programming for Trading
19 lessons in this course, and every other premium course, on one subscription.
- Every lesson in every course, with the worked examples and interactive simulators
- Graded questions on every lesson, with explanations for the wrong answers as well as the right one
- The trainers, timed assessments and brainteaser library that go with them