Technology1 min read

In C++, = default and {} are not the same constructor

By · Published by Everything Blog

In short

The divergence stems from how compilers classify trivial versus user-provided constructors, which fundamentally changes downstream function call optimizations. Under GCC 16 with optimization flags enabled, using brackets lets the compiler write fields directly, whereas defaulting routes pointers and lengths through temporary memory. Benchmark testing across 100 million elements confirmed that routing data through intermediate storage creates massive overhead. Ultimately, the bracketed constructor drastically outperformed the defaulted syntax across both GCC 15 and GCC 16 releases.

Key points

  • Choosing `= default` resulted in performance that was more than four times slower than us…: Choosing `= default` resulted in performance that was more than four times slower than using the `{}` implementation.
  • Syntax choices for constructors directly alter how optimizing compilers handle parameter…: Syntax choices for constructors directly alter how optimizing compilers handle parameter passing and memory copying.
  • The severe performance degradation caused by trivial structure initialization persisted a…: The severe performance degradation caused by trivial structure initialization persisted across both GCC 15 and GCC 16 compilers.

Sources checked by our analysis pipeline: 1

2 November 2026 · 3 min

You can write an empty default constructor as span() = default

or as span() {}

. It can have remarkable effects on performance.

struct span {

const char *p;

int len;

const char *end;

span() = default; // or span() {}

span(const char *s, int n) : p(s), len(n), end(s + n) {}

};

The constructor span() = default

on its first declaration makes span

trivial. The constructor span() {}

is user-provided, so span

is not trivial.

An example

This span

is a start pointer, a length, and an end pointer. A box

holds one span. The expression box()

value-initializes that span. The loop assigns a value-initialized box when i

reaches n

, and otherwise a span of length i + 1

:

struct box {

span s;

box() : s() {}

box(span in) : s(in) {}

};

void consume(box &);

void walk(const char *s, int n) {

for (int i = 0;; i++) {

box b;

b = i == n ? box() : box(span(s, i + 1));

if (i == n) {…

Technology