getcracked
Blog / Engineering

Modern C++ Features for HFT Interviews

Engineering

"So, are you comfortable with C++17?" The interviewer asks. Do not say "I used auto once."


1. Move Semantics (C++11)

Why HFT cares: Avoid deep copies of expensive objects (like huge vectors of market data).
std::move casts to an rvalue reference, allowing resources to be "stolen" rather than copied.

1
// Before C++11 (Copy)
2
std::vector<int> big_data(1000000);
3
std::vector<int> copy = big_data; // Expensive copy constructor! All elements duplicated.
4

5
// After C++11 (Move)
6
std::vector<int> stolen = std::move(big_data); // Cheap pointer swap!
7
// 'big_data' is now empty/valid state. 'stolen' owns the memory.
8

2. Smart Pointers (C++11)

Why HFT cares: Memory leaks kill lengthy trading days.
std::unique_ptr has ZERO overhead compared to raw pointers (it's just a wrapper that calls delete in destructor).
std::shared_ptr has overhead (atomic reference counting). Use sparingly in hot paths.

3. Constexpr (C++11/14)

Why HFT cares: Calculate everything at compile time so the runtime is instantaneous.
You can parse FIX message templates or build lookup tables entirely during compilation.

1
constexpr int factorial(int n) {
2
    return (n <= 1) ? 1 : n * factorial(n - 1);
3
}
4

5
// Computed at compile time. 
6
// The binary just has the number 120 hardcoded. No function call.
7
constexpr int val = factorial(5); 
8

4. Structured Binding (C++17)

Clean syntax for returning multiple values (like Go or Python).

1
std::tuple<int, double, std::string> get_trade() {
2
    return {1, 100.50, "AAPL"};
3
}
4

5
auto [id, price, symbol] = get_trade();
6

5. Concepts (C++20)

Why HFT cares: Better template error messages.
Instead of a 500-line template error, you get "T does not satisfy Addable".