Quant Memo
Core

Templates and Compile-Time Dispatch

A way of writing code once and having the compiler generate a specialised, fast version for each type it's used with — instead of paying a runtime cost to figure out which version to run.

A pricing engine that supports both European and American options could pick which pricing routine to call at runtime, checking a flag on every single call — or it could let the compiler bake that decision in once, at compile time, producing two separate pieces of machine code with no branch to check at all. Templates are C++'s tool for the second approach: you write the pricer generically, parameterised over the option type, and the compiler stamps out a specialised version for each type actually used, resolving the dispatch before the program ever runs.

This matters in latency-sensitive code because runtime dispatch — a virtual function call, a switch statement, a lookup table — costs a branch and often a cache miss on every invocation, and in a loop pricing millions of options that adds up. Compile-time dispatch via templates removes the branch entirely: price<European>(...) and price<American>(...) are different functions by the time the binary exists, so calling one involves no decision, just a direct jump, and the compiler can inline and optimise each specialisation independently since it knows exactly what it's working with.

The tradeoff is compile time and binary size: every type combination a template is instantiated with generates its own copy of the code, so a heavily templated codebase can take much longer to build and produce a larger executable than one using runtime polymorphism. It also pushes error messages to compile time, where template errors are notoriously verbose, and it requires the set of types to be known when the code is written (or at least when it's compiled) — you can't add a new option type at runtime the way you could add a new subclass and dispatch to it dynamically.

Templates let the compiler generate specialised, branch-free code for each type used, trading longer compile times and larger binaries for the removal of runtime dispatch overhead — the right tool when a hot loop calls the same operation over a small, fixed set of types millions of times.

Related concepts

Practice in interviews

Further reading

  • Stroustrup, The C++ Programming Language, ch. 23-27
ShareTwitterLinkedIn