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.
constinitremoves static initialization order problems and the guard-variable checks that dynamic initializers need, and it never runs a surprising constructor at startup.constevallets you build compile-time-checked configuration (for example, validating a message ID or parsing a format string while compiling).
Interview takeaway:
constexprmay run at compile time,constevalmust, andconstinitguarantees 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:
spanis to contiguous buffers whatstring_viewis 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::formatallocates, 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::spanand concepts. They are where an interviewer can quickly move from syntax into cache behavior, aliasing rules, and what the compiler actually generates.
Comments
Post a Comment