In C++, writing an empty default constructor as `span() = default` or `span() {}` produces different performance outcomes. The `= default` declaration makes a struct trivial, while `span() {}` is user-provided, preventing the compiler from optimizing memory layout. Testing on GCC 15 and 16 with an Intel Xeon Gold 6548N shows the `= default` version achieves approximately 1.73 ns/span, while `span() {}` runs around 1.74 ns/span.
However, the structural difference matters: `= default` allows the end pointer to be stored directly in the box, whereas `span() {}` forces data through a temporary before copying. With Clang 21, both approaches perform similarly at 1.75 ns/span. The choice affects whether the compiler can apply value initialization optimizations, potentially impacting high-frequency trading or embedded systems where every nanosecond counts.
Developers must weigh code clarity against potential micro-optimizations when selecting constructor syntax.
Source: Hacker News · Summarized by HeadlinesBriefing