Skip to main content

Low latency C++20 features (Excluding Threading)

 

Important C++20 Features for Low-Latency C++ (Excluding Threading)

The goals in low-latency C++ stay the same as in C++17: no heap allocation on the hot path, no unnecessary copies, predictable branches, and as much work as possible done at compile time. C++20 pushes further on compile-time computation, gives you proper tools for bit manipulation and type punning, and adds safer non-owning views. This guide ranks the C++20 features that matter most for those goals. Threading features (std::jthread, stop_token, latches, barriers, semaphores, atomic wait/notify, atomic_ref) are deliberately left out.


Tier 1: Features You Should Know Cold

1. constexpr upgrades, consteval, and constinit

C++20 greatly expands what can run at compile time, and adds two new keywords that let you require it.

constexpr is far more capable. You can now use std::vector and std::string inside constexpr evaluation (as long as the memory is freed before the evaluation ends), the <algorithm> functions such as std::sort are constexpr, and constexpr functions can contain try blocks and use virtual functions.

constexpr auto makeSortedTable() {
    std::array<int, 8> a{8, 3, 5, 1, 7, 2, 6, 4};
    std::sort(a.begin(), a.end());          // constexpr std::sort in C++20
    return a;
}

constexpr auto kTable = makeSortedTable();  // computed by the compiler

consteval declares an immediate function: every call must be evaluated at compile time, or the program doesn't compile.

consteval int square(int x) { return x * x; }

constexpr int a = square(8);   // OK
int n = 5;
int b = square(n);             // error: n is not a constant expression

constinit guarantees a variable is initialized statically (at compile time), without making it const.

constinit int g_counter = 0;                 // no dynamic initialization, ever
constinit thread_local int t_depth = 0;      // avoids the thread-local init wrapper

Why it matters:

  • Lookup tables, hash seeds and CRC tables cost nothing at startup.
  • constinit removes static initialization order problems and the guard-variable checks that dynamic initializers need, and it never runs a surprising constructor at startup.
  • consteval lets you build compile-time-checked configuration (for example, validating a message ID or parsing a format string while compiling).

Interview takeaway: constexpr may run at compile time, consteval must, and constinit guarantees constant initialization of a (mutable) variable.


2. <bit>: fast bit manipulation as portable standard functions

C++20 standardizes the bit operations that low-latency code uses constantly. They compile down to single instructions (popcnt, lzcnt, tzcnt, bsr, rol) when the target supports them.

#include <bit>

std::popcount(0b1011u);        // 3   (number of set bits)
std::countl_zero(x);           // leading zeros
std::countr_zero(x);           // trailing zeros (find first set bit)
std::has_single_bit(x);        // is x a power of two?
std::bit_ceil(x);              // smallest power of two >= x
std::bit_floor(x);             // largest power of two <= x
std::rotl(x, 5);               // rotate left, useful for hashing

Typical low-latency uses:

  • Order book level bitmaps: find the next non-empty price level with countr_zero.
  • Ring buffer sizing: round capacity up to a power of two with bit_ceil, then replace % with & (size - 1).
  • Hashing and checksums: rotl / rotr.
// Fast index wrapping in a power-of-two ring buffer
static_assert(std::has_single_bit(Capacity));
index = (index + 1) & (Capacity - 1);

// Next set bit in a 64-bit occupancy mask
int level = std::countr_zero(mask);

Note: all of these require unsigned integer types. std::byteswap (for endianness) is C++23, not C++20.


3. std::bit_cast: type punning without undefined behavior

Reading a float as a uint32_t through a union or a reinterpret_cast pointer is undefined behavior in C++ (strict aliasing). std::bit_cast is the sanctioned, constexpr way to reinterpret an object's bytes as another type of the same size.

float f = 1.5f;
auto bits = std::bit_cast<uint32_t>(f);     // well-defined
auto back = std::bit_cast<float>(bits);

It is also the right way to decode a fixed-layout wire header from raw bytes:

struct Header { uint32_t id; uint16_t len; uint16_t flags; };
static_assert(std::is_trivially_copyable_v<Header>);

std::array<std::byte, sizeof(Header)> raw = /* from network */;
Header h = std::bit_cast<Header>(raw);

The compiler typically optimizes this to a single load or register move, with no function call. std::memcpy achieves the same result, but bit_cast is constexpr and states the intent.

Requirements: both types must be the same size and trivially copyable. Byte order is still your responsibility (check std::endian).


4. std::span: non-owning view over contiguous memory

span<T> is a pointer plus a size. Passing one never allocates or copies. It replaces the old (T* ptr, size_t n) parameter pair and works with arrays, std::vector, std::array and raw buffers.

void process(std::span<const std::byte> data);

std::array<std::byte, 256> a;
std::vector<std::byte> v;
std::byte raw[64];

process(a);          // all of these work with no copying
process(v);
process(raw);

Slicing is O(1):

auto header  = data.first(sizeof(Header));
auto payload = data.subspan(sizeof(Header));

A static extent (std::span<int, 4>) encodes the size in the type, so there's no stored size and the compiler can unroll loops.

Pitfalls

  • Like string_view, it doesn't own its data. A span over a destroyed or reallocated container dangles.
  • operator[] is not bounds-checked, which is often what you want on the hot path, but be deliberate about it.

Interview takeaway: span is to contiguous buffers what string_view is to strings, a zero-cost way to pass memory around without ownership.


5. Concepts: constrained templates with zero runtime cost

Concepts express template requirements directly in the signature, so templates stay as fast as hand-written code (all resolved at compile time) and fail with readable errors.

template <typename T>
concept TriviallySerializable =
    std::is_trivially_copyable_v<T> && std::is_standard_layout_v<T>;

template <TriviallySerializable T>
void write(Buffer& buf, const T& value) {
    std::memcpy(buf.next(sizeof(T)), &value, sizeof(T));
}

You can overload on concepts, which replaces tag dispatch and SFINAE:

template <std::integral T>       void encode(T v)  { /* integer path */ }
template <std::floating_point T> void encode(T v)  { /* float path */ }

For latency work, concepts are the preferred way to express static (compile-time) interfaces as an alternative to virtual functions. A template constrained by a concept gets full inlining where a virtual call usually does not.

template <typename H>
concept MessageHandler = requires(H h, const Msg& m) {
    { h.onMessage(m) } -> std::same_as<void>;
};

template <MessageHandler H>
void run(H& handler, std::span<const Msg> msgs) {
    for (const auto& m : msgs) handler.onMessage(m);   // inlinable
}

6. [[likely]] and [[unlikely]]

Standard branch-probability hints, replacing compiler-specific __builtin_expect. The compiler can use them to lay out code so the expected path is the fall-through, which helps the instruction cache and branch predictor.

if (msg.isValid()) [[likely]] {
    process(msg);
} else [[unlikely]] {
    logError(msg);
}

switch (type) {
    case MsgType::Trade:  [[likely]] handleTrade(); break;
    case MsgType::Reject: [[unlikely]] handleReject(); break;
}

Pitfalls

  • They are hints. The effect varies by compiler and optimization level, and wrong hints can hurt.
  • Measure with a profiler or perf (look at branch-miss counts) instead of guessing. For genuinely hot paths, profile-guided optimization often does better than manual hints.

7. [[no_unique_address]]

Lets an empty member (a stateless allocator, comparator or policy class) occupy no space, overlapping with other members. This keeps hot structures small, which means more of them per cache line.

template <typename T, typename Alloc = std::allocator<T>>
struct Buffer {
    T* data;
    size_t size;
    [[no_unique_address]] Alloc alloc;   // adds 0 bytes if Alloc is empty
};

static_assert(sizeof(Buffer<int>) == sizeof(int*) + sizeof(size_t));

Pitfall: MSVC ignores the standard attribute for ABI reasons and requires [[msvc::no_unique_address]] instead. Check sizeof with a static_assert so a silent size regression becomes a compile error.


8. std::format_to / format_to_n: formatting without the heap

std::format returns a std::string and so can allocate, but its siblings write into a buffer you provide.

char buf[128];
auto result = std::format_to_n(buf, sizeof(buf) - 1, "{} {:.2f} {}", symbol, price, qty);
*result.out = '\0';

Advantages over iostream and printf: format strings are checked at compile time, it is type-safe, it avoids the locale machinery by default, and it is generally faster than ostringstream.

Pitfalls: plain std::format allocates, so keep it out of the hot path (log asynchronously, or format into a preallocated buffer). Library support also varies (GCC 13+, MSVC 2019 16.10+, libc++ 17+ partial). For the very hottest logging, many shops still use a custom or binary logger.


9. Defaulted comparisons and the spaceship operator <=>

One declaration generates all six comparison operators, member-wise and in declaration order.

struct Price {
    int64_t ticks;
    auto operator<=>(const Price&) const = default;   // also generates ==
};

Price a{100}, b{105};
bool less = a < b;      // works, with all of <, <=, >, >=, ==, != available

It reduces boilerplate and bugs, and keeps ordering consistent, which matters for keys in sorted containers and for heap comparators. Comparison is memberwise, so field order matters (put the most discriminating field first).

struct OrderKey {
    int64_t price;       // compared first
    int64_t timestamp;   // then this
    auto operator<=>(const OrderKey&) const = default;
};

10. std::make_unique_for_overwrite (and _shared_)

make_unique<T[]>(n) value-initializes, so for arrays of trivial types it zero-fills memory. For a large scratch buffer that you are about to overwrite anyway, that is wasted work. The _for_overwrite versions default-initialize, leaving trivial types uninitialized.

auto buf = std::make_unique_for_overwrite<std::byte[]>(1 << 20);
// no 1 MB memset; contents are indeterminate until you write them

The allocate_shared_for_overwrite and make_shared_for_overwrite variants exist for shared_ptr.

Pitfall: reading before writing is undefined behavior for trivial types. Use this only when you fully overwrite the buffer.


Tier 2: Useful in Specific Situations

11. Ranges and views (use with care)

Ranges provide lazy, composable views with no intermediate containers:

#include <ranges>

auto activeOrders = orders
    | std::views::filter([](const Order& o) { return o.isActive(); })
    | std::views::transform(&Order::price);

for (auto p : activeOrders) { /* no temporary vector was created */ }

Range algorithms also accept projections, which removes many comparator lambdas:

std::ranges::sort(orders, {}, &Order::timestamp);

Why be careful in hot code: views generally optimize well in release builds, but they increase compile times, produce complex types and error messages, and can run noticeably slower in unoptimized or debug builds. Check the generated code or benchmark before using a long pipeline on the critical path.


12. std::source_location

A constexpr, type-safe replacement for __FILE__ / __LINE__ / __func__ macros, with no runtime cost beyond static strings.

void check(bool ok, std::source_location loc = std::source_location::current()) {
    if (!ok) {
        fail(loc.file_name(), loc.line());
    }
}

Useful for assertions and lightweight tracing without macros.


13. std::to_array

Creates a std::array with the size and element type deduced, including from a C array or braced list, so there is no need to count elements or fall back to a raw array.

constexpr auto kPorts = std::to_array({8080, 8081, 8082});   // std::array<int, 3>
constexpr auto kName  = std::to_array("ABC");                // std::array<char, 4>

14. std::assume_aligned

Tells the compiler that a pointer has a given alignment, enabling better vectorization and avoiding alignment checks. Undefined behavior if the claim is false.

void scale(float* data, size_t n) {
    float* p = std::assume_aligned<32>(data);   // promise: 32-byte aligned
    for (size_t i = 0; i < n; ++i) p[i] *= 2.0f;
}

15. std::endian

A compile-time constant for byte order, useful for wire-format code (pair it with bit_cast):

if constexpr (std::endian::native == std::endian::little) {
    // x86/ARM-LE path: byte swap needed for big-endian network data
}

16. Safe integer comparison: std::cmp_less, std::in_range

Comparing signed and unsigned integers with built-in operators performs implicit conversions that can silently give wrong answers. These functions compare by mathematical value.

int  a = -1;
unsigned b = 1;

a < b;                  // false! a is converted to a huge unsigned value
std::cmp_less(a, b);    // true, the correct mathematical result

std::in_range<uint8_t>(300);   // false

17. Template lambdas and better lambda support

Lambdas can have explicit template parameter lists and are default-constructible when captureless. Together with pack expansion in init-captures, they make it easy to build zero-overhead function objects (see the lambda guide for details).

auto sumAll = []<typename... Ts>(Ts... xs) { return (xs + ...); };

18. Smaller library conveniences that help parsing and containers

std::string_view line = "BUY,AAPL,100";
line.starts_with("BUY");                  // string and string_view starts_with / ends_with

std::unordered_map<int, Order> book;
book.contains(42);                        // clearer than find() != end()

std::erase_if(vec, [](const Order& o) { return o.filled(); });   // erase-remove in one call

auto n = std::ssize(vec);                 // signed size, avoids signed/unsigned warnings
std::midpoint(lo, hi);                    // overflow-safe midpoint for binary search

Also useful: using enum (bring enum values into scope in a switch), designated initializers (Order{.price = 100, .qty = 5}, which must follow declaration order), and [[nodiscard("reason")]] to explain why a result mustn't be ignored.


C++20 Features to Treat with Caution in Low-Latency Code

Feature Why to be careful
Coroutines The coroutine frame is heap-allocated by default (the compiler may elide it, but this isn't guaranteed). You can customize allocation in the promise type, but it takes real care. Powerful for async state machines, but check the allocation and indirect-call cost before putting one on the hot path.
Ranges pipelines Fine in release builds in most cases, but compile-time cost, complex types, and slow debug builds. Benchmark long chains.
std::format (the allocating form) Returns std::string, which may allocate. Use format_to / format_to_n with a preallocated buffer, or a dedicated low-latency logger.
Modules A build-time feature. It doesn't make your program faster at runtime, and tooling support is still uneven.
constexpr dynamic allocation The memory exists only at compile time. It is not a way to get runtime allocations for free, and it must be released before constant evaluation ends.
[[likely]] / [[unlikely]] Compiler-dependent effect. Wrong hints can make code slower. Verify with perf or PGO.
<=> on large structs A defaulted comparison compares members in order and can be more work than a hand-written key comparison. Order members and measure.

Summary Table

# Feature Low-latency benefit
1 constexpr / consteval / constinit Compile-time tables, no startup cost, no static-init-order bugs
2 <bit> Single-instruction bit ops, power-of-two sizing, bitmap scanning
3 std::bit_cast UB-free type punning, wire-header decoding
4 std::span Zero-copy views over contiguous buffers
5 Concepts Inlinable compile-time interfaces instead of virtual
6 [[likely]] / [[unlikely]] Standard branch-layout hints
7 [[no_unique_address]] Smaller hot structs via empty-member optimization
8 format_to / format_to_n Type-safe formatting into a preallocated buffer
9 <=> and defaulted comparisons Less boilerplate, consistent ordering for keys
10 make_unique_for_overwrite Skip zero-filling large buffers
11 Ranges and views Lazy pipelines without temporaries (measure first)
12 std::source_location Macro-free, zero-cost diagnostics
13 std::to_array Deduced-size arrays, fewer raw arrays
14 std::assume_aligned Better vectorization via alignment promises
15 std::endian Compile-time byte-order handling
16 cmp_less, in_range Correct signed/unsigned comparisons
17 Template lambdas Zero-overhead generic callables
18 starts_with, contains, erase_if Cheaper, clearer parsing and container code

Final thought: the C++20 story for low latency is "move more to compile time, stop undefined-behavior type punning, and use zero-cost views." The ones I would expect any candidate to discuss are constexpr/consteval/constinit, <bit>, std::bit_cast, std::span and concepts. They are where an interviewer can quickly move from syntax into cache behavior, aliasing rules, and what the compiler actually generates.

Comments

Popular posts from this blog

FIX protocol interview questions : Part 1 - Admin/Session

Well, I just felt a need that key FAQs in FIX be documented, so that can be used for a variety of purposes, primarily during interviews to gauge a candidate. I have tried to be correct to the max, but do not guarantee it. This is what is best to my knowledge, and its good to cross check things with the FIX site and documentation available there. 1. What is buy side and what is sell side ? Sell side is the one who earns brokerage. Buy side is the one who pays brokerage. buy side (institutions) sell side (brokers/dealers) 2. What is the latest FIX version in market now? Its FIX 5.0 SP2. Something called FIXT.1.1 is also coming. 3. What is FIXML? FIXML is a structured, validated Extensible Markup Language (XML) derived grammar that is encapsulated within the standard FIX message. This format leaves the FIX session handling intact and minimizes the impact to existing implementations. As an XML-derived language, FIXML messages can be validated by standard parsers and take advantage of a fle...

what is a core dump

 A core dump (or core file ) is a file that captures the memory contents of a running process at a particular point in time, typically when the process crashes or encounters a serious error, such as a segmentation fault or illegal instruction. What is a core dump? It is essentially a snapshot of a process’s memory, including the call stack, memory allocations, and the state of the program at the time of the crash. It helps developers analyze the cause of a crash by examining the state of the program when the error occurred. Why does a core dump occur? A core dump typically happens when a process encounters a fatal error that causes it to terminate unexpectedly. This might include: Segmentation faults (segfaults): Trying to access memory that the process is not allowed to. Illegal instructions: Executing invalid machine instructions. Memory access violations: Trying to access memory in a way that is not allowed (e.g., reading from or writing to protected memory). Un...

std::unordered_map problems

 The std::unordered_map in C++ is a powerful container that provides fast access to key-value pairs with average constant-time complexity ( O(1) ) for lookups, inserts, and deletions. However, like any data structure, it has its potential issues, trade-offs, and limitations. Here are some common problems and limitations associated with std::unordered_map : 1. Unpredictable Iteration Order Problem: The order of elements in an unordered_map is not guaranteed. It depends on the internal hash table implementation and the distribution of hash values. Impact: This makes std::unordered_map unsuitable for use cases where the order of elements matters, such as when you need to traverse the elements in a specific order. Example: cpp Copy std::unordered_map< int , std::string> map; map[ 1 ] = "one" ; map[ 2 ] = "two" ; map[ 3 ] = "three" ; // The order in which elements are iterated is not guaranteed for ( auto & entry : map) { std::cout ...