Type Lists and Recursion at Compile Time

One firm's engineering blog published an exercise on removing adjacent duplicates from a compile-time vector using template metaprogramming, and variadic templates are probed directly elsewhere. The framing that makes all of it tractable is this: a parameter pack is a list, the type system is a purely functional language, and every operation is written the way you would write it in one.

The vocabulary

A variadic template takes a pack of types, and a class template holding one is a list.

template <class... Ts> struct TypeList {};

using Book = TypeList<Order, Level, Trade>;

There are no loops here and no mutation. There is pattern matching, in the form of partial specialisation, and recursion. So every algorithm has the same shape as its equivalent in a functional language: a base case for the empty list, and a step that peels the head off and recurses on the tail.

The rest of this lesson is for subscribers

Unlock every lesson in Systems Programming for Trading, and every other premium course.

Subscribe to continue

Test your knowledge

Questions are only available to subscribers.

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