Skip to main content

Low Latency C++17 Features

 

Important C++17 Features for Low-Latency C++

In low-latency systems (trading, networking, audio, real-time control), the goals are usually the same: no heap allocation on the hot path, no unnecessary copies, no unpredictable branches or locks, and as much work as possible moved to compile time. This guide ranks the C++17 features that help with those goals, with the reason each one matters, an example, and the pitfalls.


Tier 1: Features You Should Know Cold

1. std::string_view: non-owning, zero-allocation string handling

A string_view is a pointer plus a length. Passing, slicing and comparing strings through it never allocates or copies.

// Bad: constructing a std::string from a literal or substring allocates
// once the text exceeds the small-string buffer
void onSymbol(const std::string& symbol);

// Good: no allocation, accepts std::string, literals, char buffers
void onSymbol(std::string_view symbol);

// Parsing a feed message without copying anything
std::string_view msg = "AAPL,150.25,100";
auto comma  = msg.find(',');
auto symbol = msg.substr(0, comma);   // O(1), no copy

Why it matters: parsing market data and protocol messages is mostly slicing a buffer, and string_view lets you do it in place.

Pitfalls

  • It doesn't own the data. If the underlying buffer is destroyed or reused (for example a network receive buffer), the view dangles.
  • It is not guaranteed to be null-terminated, so don't pass sv.data() to C APIs that expect a C string.
  • Never return a string_view to a local std::string or to a temporary.

Interview takeaway: string_view replaces const std::string& parameters, but it shifts lifetime responsibility to the caller.


2. std::optional: "maybe a value" with no heap allocation

optional<T> stores the T inline (plus a flag). There is no allocation, unlike returning T* from new or std::unique_ptr<T>.

std::optional<Order> findOrder(OrderId id) {
    auto it = book.find(id);
    if (it == book.end()) return std::nullopt;
    return it->second;               // stored inline in the optional
}

if (auto o = findOrder(42)) {
    process(*o);
}

It is a cleaner alternative to sentinel values (-1, nullptr) and out-parameters, and it is as cheap as the hand-written version.

Pitfalls

  • sizeof(optional<T>) is sizeof(T) plus padding for the flag, which can bloat a cache-sensitive struct (optional<int> is typically 8 bytes).
  • value() throws std::bad_optional_access. In latency-critical code with exceptions disabled or avoided, use *opt after checking, or value_or.
  • optional<T> copies T when copied. For large T, consider optional<std::reference_wrapper<T>> or a pointer.

3. std::variant and std::visit: polymorphism without virtual functions or heap

variant<A, B, C> holds exactly one of several types in-place, with no allocation and no vtable pointer. It is the standard tool for closed sets of message or event types.

struct NewOrder    { /* ... */ };
struct CancelOrder { /* ... */ };
struct Trade       { /* ... */ };

using Event = std::variant<NewOrder, CancelOrder, Trade>;

template <class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template <class... Ts> overloaded(Ts...) -> overloaded<Ts...>;

void handle(const Event& e) {
    std::visit(overloaded{
        [](const NewOrder&)    { /* ... */ },
        [](const CancelOrder&) { /* ... */ },
        [](const Trade&)       { /* ... */ },
    }, e);
}

Compared with virtual:

virtual hierarchy std::variant
Storage Usually heap + pointer Inline, contiguous (good for cache)
Dispatch Indirect call through the vtable Switch / jump table on the index
Inlining Hard Often possible
Open for extension Yes (new subclasses) No (fixed set of types)

Pitfalls

  • sizeof(variant) is the size of the largest alternative plus the index, so one large alternative bloats every element in an array of variants.
  • std::visit is typically a jump table, so the cost depends on the compiler. Always measure it.
  • std::get<T> throws bad_variant_access on mismatch. In hot code, use std::get_if<T> or visit.

4. if constexpr: compile-time branching with zero runtime cost

The branch not taken is discarded at compile time, so it doesn't need to compile for that type and generates no code. This replaces most SFINAE and tag-dispatch tricks.

template <typename T>
void serialize(Buffer& buf, const T& value) {
    if constexpr (std::is_trivially_copyable_v<T>) {
        std::memcpy(buf.next(sizeof(T)), &value, sizeof(T));   // fast path
    } else {
        value.serialize(buf);                                  // custom path
    }
}

Typical low-latency uses: choosing between fast and safe code paths per type, removing logging or checks in release builds, and specializing for different message types without runtime ifs.

template <bool Debug>
void process(const Msg& m) {
    if constexpr (Debug) {
        validate(m);        // not compiled at all when Debug == false
    }
    handle(m);
}

Interview takeaway: Unlike a plain if with a constant condition, if constexpr genuinely removes the other branch from instantiation, so both branches don't need to be valid for every type.


5. std::pmr (polymorphic memory resources): control over allocation

C++17 adds std::pmr::memory_resource and allocator-aware containers (std::pmr::vector, std::pmr::string, std::pmr::unordered_map...) so you can change where memory comes from without changing the container's type.

#include <memory_resource>

// A stack buffer used as an arena
std::array<std::byte, 4096> buffer;
std::pmr::monotonic_buffer_resource arena{buffer.data(), buffer.size(),
                                          std::pmr::null_memory_resource()};

std::pmr::vector<int> v{&arena};
v.reserve(100);
v.push_back(1);          // allocated from the stack buffer, no malloc

Useful resources:

  • monotonic_buffer_resource: very fast bump allocation, frees everything at once on destruction or release(). Ideal for per-message or per-tick scratch memory.
  • unsynchronized_pool_resource: pooled allocation for single-threaded use.
  • null_memory_resource(): throws if anything tries to allocate. This is an excellent way to prove that a section of code doesn't allocate.

Pitfalls

  • Allocation goes through a virtual call on the resource (small, but not free).
  • A monotonic_buffer_resource never reuses freed memory, so a long-lived container that grows and shrinks will consume the arena.
  • The resource must outlive every container using it.

6. std::from_chars / std::to_chars: fast, non-allocating number conversion

Numeric conversion from <charconv> is locale-independent, non-throwing and non-allocating. It is typically much faster than std::stoi, std::stod, std::stringstream or sscanf.

#include <charconv>

std::string_view field = "12345";
int value;
auto [ptr, ec] = std::from_chars(field.data(), field.data() + field.size(), value);

if (ec == std::errc{}) {
    // parsed successfully; ptr points past the last consumed character
}

char out[32];
auto [end, ec2] = std::to_chars(out, out + sizeof(out), 3.14159);

This is a common choice for parsing FIX messages, CSV or JSON numerics, and for serializing prices without touching the heap or the locale machinery.

Pitfall: integer support is universal, but floating-point from_chars / to_chars arrived later in some standard libraries (GCC 11+, MSVC 2019 16.4+, libc++ more recently). Check the version you ship on.


7. Guaranteed copy elision

Before C++17, returning a prvalue (a temporary) could be elided, but the type still needed an accessible copy/move constructor. In C++17, returning or initializing from a prvalue never copies or moves, so the type doesn't need to be copyable or movable at all.

struct Pinned {
    Pinned() = default;
    Pinned(const Pinned&) = delete;
    Pinned(Pinned&&)      = delete;
};

Pinned make() { return Pinned{}; }   // OK in C++17: no copy, no move

Pinned p = make();                   // constructed directly in p

Why it matters: factory functions can return large objects (order books, arrays, lock-owning types, std::atomic members) by value at no cost, and you can stop writing out-parameters for performance.

Pitfall: only prvalues are guaranteed. Returning a named local (NRVO) is still just permitted, not required:

Pinned make2() {
    Pinned p;
    return p;       // error for non-movable types; NRVO is not guaranteed
}

8. std::hardware_destructive_interference_size: avoiding false sharing

Defined in <new>, it gives the minimum offset between two objects to avoid them sharing a cache line (commonly 64 bytes on x86, 128 on some other platforms).

struct alignas(std::hardware_destructive_interference_size) Counter {
    std::atomic<uint64_t> value{0};
};

Counter perThread[8];   // each counter has its own cache line

This is the standard way to keep per-thread counters, ring buffer head/tail indexes and other hot atomics from invalidating each other's cache lines.

Pitfall: compiler support arrived late (GCC 12, recent Clang and MSVC) and GCC warns about ABI stability when it's used in headers (-Winterference-size). Many codebases still hard-code 64 (or 128) in a named constant. Know both approaches.


9. std::atomic<T>::is_always_lock_free

A compile-time constant telling you whether an atomic type is guaranteed to be implemented without a hidden lock. A "non-lock-free" atomic silently uses a mutex, which is catastrophic for latency.

static_assert(std::atomic<uint64_t>::is_always_lock_free,
              "need a lock-free 64-bit atomic");

struct Big { char data[128]; };
static_assert(!std::atomic<Big>::is_always_lock_free);   // would use a lock internally

Interview takeaway: Put this static_assert next to any atomic you rely on in a lock-free queue or flag. It turns a silent performance cliff into a compile error.


10. inline variables and constexpr improvements

Inline variables let you define a variable in a header without ODR violations and without a separate .cpp definition.

// config.h
inline constexpr int kMaxOrders = 1024;
inline constexpr std::array<int, 4> kLevels{1, 2, 3, 4};

struct Limits {
    static inline int current = 0;      // no out-of-class definition needed
};

constexpr lambdas (implicit in C++17) and a more capable constexpr library let you build lookup tables at compile time instead of at startup:

constexpr auto makeTable = [] {
    std::array<uint32_t, 256> t{};
    for (uint32_t i = 0; i < 256; ++i) {
        t[i] = i * i;
    }
    return t;
};

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

Why it matters: moving work to compile time means no startup cost, no static-initialization-order problems, and the data can live in read-only memory.


Tier 2: Useful in Specific Situations

11. Fold expressions

Collapse a parameter pack with an operator, with no recursion, which is handy for compile-time-unrolled code over a set of handlers or fields.

template <typename... Handlers>
void dispatchAll(const Msg& m, Handlers&... h) {
    (h.on(m), ...);          // calls every handler, unrolled at compile time
}

template <typename... Args>
bool allPositive(Args... args) {
    return ((args > 0) && ...);
}

12. Structured bindings

Zero-cost unpacking of pairs, tuples, structs and arrays, which makes hot-path code easier to read without extra copies if you bind by reference.

for (const auto& [price, qty] : levels) {   // reference bindings: no copies
    total += price * qty;
}

if (auto [it, inserted] = book.try_emplace(id, order); !inserted) {
    // already existed
}

13. std::byte

A type for raw memory that does not behave like a character or an integer, which prevents accidental arithmetic. Useful for buffers, arenas and wire formats.

std::array<std::byte, 1024> buf;
buf[0] = std::byte{0x7F};
auto v = std::to_integer<int>(buf[0]);

14. Over-aligned new / aligned allocation

operator new now respects types with alignment above the default, which matters for SIMD types and cache-line-aligned structures.

struct alignas(64) CacheLineData { char bytes[64]; };

auto* p = new CacheLineData;        // correctly aligned to 64 in C++17

Before C++17 this was silently under-aligned, which could mean undefined behavior with aligned SIMD loads or false sharing.


15. std::launder

Needed when you reuse storage by placement-new and access the new object through a pointer to the old one, especially for types with const or reference members. It appears in custom containers, ring buffers and object pools.

alignas(T) std::byte storage[sizeof(T)];

T* p = new (storage) T{/*...*/};      // use the pointer returned by placement new
// If you later re-construct in the same storage and only have the old pointer:
T* q = std::launder(reinterpret_cast<T*>(storage));

Rule of thumb: keep and use the pointer returned by placement new. Reach for launder when you can't.


16. Attributes: [[nodiscard]], [[maybe_unused]], [[fallthrough]]

[[nodiscard]] is a cheap safety net for error codes and results that must not be ignored.

[[nodiscard]] bool submit(const Order&);
[[nodiscard]] std::optional<Fill> tryMatch(Order&);

submit(o);   // warning: ignoring return value

([[likely]] and [[unlikely]] are C++20, not C++17. In C++17 you use compiler built-ins like __builtin_expect.)


17. std::scoped_lock

A variadic RAII lock that acquires several mutexes with a deadlock-avoidance algorithm. It is still best kept off the hot path, but it's the right tool for the cold path.

std::scoped_lock lock(mutexA, mutexB);

18. Selection statements with initializers

Limits the scope of temporaries, which helps with lock lifetimes and tidy hot-path code.

if (auto it = map.find(key); it != map.end()) {
    use(it->second);
}

switch (auto s = getState(); s) {
    // ...
}

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

Feature Why to be careful
Parallel algorithms (std::execution::par) Thread-pool scheduling, hidden allocations and unpredictable wakeups add latency and jitter. They suit batch and offline work, not a latency-critical path.
std::any Type-erased, so it may heap-allocate for larger types and requires RTTI-based casts. Prefer std::variant.
std::shared_mutex Reader/writer locks are heavier than they look and can still cause cache-line contention and writer starvation. Consider seqlocks or RCU-style designs for hot reads.
std::filesystem Allocates, throws and performs syscalls. Use it for setup and tooling, never on the hot path.
std::string_view stored in long-lived structures Lifetime bugs. Be sure the underlying buffer outlives every view.
std::variant with many or large alternatives One large member inflates every object and hurts cache density. Measure sizeof.
std::optional::value() / std::get They throw. Prefer non-throwing access in latency-critical paths.

Summary Table

# Feature Low-latency benefit
1 std::string_view No allocations or copies when slicing/parsing
2 std::optional Inline "maybe" value, no heap
3 std::variant + visit Closed-set polymorphism without virtual or heap
4 if constexpr Compile-time code selection, zero runtime branching
5 std::pmr Arena/pool allocation, provable no-allocation zones
6 from_chars / to_chars Fast, non-allocating, locale-free number conversion
7 Guaranteed copy elision Return big/non-movable objects by value for free
8 hardware_destructive_interference_size Portable false-sharing avoidance
9 atomic::is_always_lock_free Compile-time proof an atomic doesn't use a hidden lock
10 inline variables, constexpr lambdas Compile-time tables, header-only constants
11 Fold expressions Unrolled variadic dispatch
12 Structured bindings Readable, zero-cost unpacking
13 std::byte Type-safe raw memory
14 Aligned new Correct alignment for SIMD/cache-line types
15 std::launder Correct access to objects re-created in place
16 [[nodiscard]] Don't ignore error results

Final thought: the common thread in C++17 for low latency is "do it inline, do it at compile time, and don't allocate." string_view, optional, variant, if constexpr, pmr and from_chars are the six I would expect any candidate to discuss, and the ones where an interviewer can quickly probe into allocation behavior, cache effects and lifetime.

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 ...