Compile-Time Limit Order Book in C++ — A Template Metaprogramming Deep Dive (Part 1)
A multi-part tutorial for mastering template metaprogramming with a real HFT-inspired problem.
Compile-Time Limit Order Book in C++ — A Template Metaprogramming Deep Dive (Part 1)
A multi-part tutorial for mastering template metaprogramming with a real HFT-inspired problem.
1. Introduction
In this series, we are going to build a price–time priority limit order book entirely at compile time using C++ template metaprogramming.
Yes — compile time.
This is not something you would ever deploy in production. Real order books evolve at runtime, driven by live market data. The goal here is different and very intentional:
To use a familiar HFT domain problem to build absolute fluency in advanced C++ template metaprogramming.
If you can reason about an order book implemented with recursive templates, partial specializations, and type-level conditionals, you’ll be very comfortable with the kind of low-level C++ thinking expected in low-latency / HFT interviews.
This is Part 1 of a five part series where we are going to build the orderbook step by step. Part 1 — Compile-Time Limit Order Book in C++ — A Template Metaprogramming Deep Dive (This article) Part 2 — Order Representation at Compile Time Part 3 — Price Levels and Compile-Time Queues Part 4 — Merge Sort and List of Price Levels Part 5 — Bringing It All Together The full source code is published on github — RishabhGarg108/compile_time_orderbook
2. What Are We Building?
2.1 Limit Order Book (LOB)
A limit order book is a data structure that stores open buy and sell orders.
Each order has:
+----------+-------------------------+
| Field | Description |
+----------+-------------------------+
| Order ID | Unique identifier |
| Side | Buy (Bid) or Sell (Ask) |
| Price | Limit price |
| Quantity | Remaining quantity |
+----------+-------------------------+
Supported operations:
- Insert order
- Cancel order
- Modify order (loses time priority)
2.2 Price–Time Priority
Orders are ranked using two dimensions:
- Price priority
Bids: higher price = higher priority
Asks: lower price = higher priority
2. Time priority
Earlier order wins if prices are equal
3. Why Compile-Time?
This project is intentionally unrealistic.
At compile time:
- All orders are known upfront
- No dynamic memory
- No runtime mutation
So why do this?
3.1 Interview Value
HFT interviews often probe:
- Your ability to model complex invariants
- Comfort with templates beyond std::vector<T>
- Understanding of instantiation, specialization, recursion
This project touches all of them.
3.2 Skill Transfer
What you learn here directly applies to:
- Static dispatch
- Policy-based design
- Compile-time configuration
- Zero-overhead abstractions
4. Template Metaprogramming Refresher
Before touching the order book, let’s align on the mental model.
4.1 Types as Data
In TMP, types are values.
template<int N>
struct Int {
static constexpr int value = N;
};
Here, Int<5> represents the number 5 at compile time.
4.2 Recursive Templates
All non-trivial TMP boils down to recursion.
template<int N>
struct Factorial {
static constexpr int value = N * Factorial<N-1>::value;
};
template<>
struct Factorial<0> {
static constexpr int value = 1;
};
This exact pattern will reappear when we:
- Traverse orders
- Traverse price levels
- Merge sorted lists
4.3 If / Else at Compile Time
There are no if statements — only type selection.
template<bool Cond, typename T, typename F>
using If = typename std::conditional<Cond, T, F>::type;
We will rely heavily on this idea when:
- Choosing bid vs ask behavior
- Comparing prices
- Selecting insertion paths
5. High-Level Design
Before code, let’s understand the architecture.
5.1 Core Compile-Time Entities
+-------------+----------------------------+
| Concept | Representation |
+-------------+----------------------------+
| Order | Type with constexpr fields |
| Price Level | Type containing orders |
| Order List | Variadic type list |
| Book Side | Sorted list of levels |
| Order Book | Bid side + Ask side |
+-------------+----------------------------+
5.2 Structural Diagram
OrderBook
├── Bids (sorted ↓ price)
│ ├── Level 101
│ │ ├── Order A
│ │ └── Order B
│ └── Level 100
│ └── Order C
└── Asks (sorted ↑ price)
├── Level 102
│ └── Order D
└── Level 103
└── Order E
Each box above is a type, not an object.
6. Skills You Will Gain
By the end of this series, you will be comfortable with:
- Recursive template algorithms
- Partial vs full specialization
- Variadic list manipulation
- Type-level sorting
- Expressing business rules at compile time
- Debugging horrifying template errors 😄
These are exactly the skills interviewers look for when they say:
“Strong C++ fundamentals required.”
7. What’s Next (Part 2 Preview)
In Part 2, we will:
- Build a compile-timestructure to store order information
From there, we’ll gradually layer:
- Price level
- Sorted insertion
- Full order book
메타데이터
- post_id
- 4fd65a8a5b37
- slug
- compile-time-limit-order-book-in-c-a-template-metaprogramming-deep-dive-part-1-4fd65a8a5b37
- url
- https://medium.com/@rishabhgarg108/compile-time-limit-order-book-in-c-a-template-metaprogramming-deep-dive-part-1-4fd65a8a5b37
- canonical_url
- https://medium.com/@rishabhgarg108/compile-time-limit-order-book-in-c-a-template-metaprogramming-deep-dive-part-1-4fd65a8a5b37
- author_url
- https://medium.com/@rishabhgarg108
- status
- ok
- fetched_at
- 2026-08-22 20:24:00