In C++, = default and {} are not the same constructor lemire.me · 34 points · 71...

@best

In C++, = default and {} are not the same constructor lemire.me · 34 points · 71 comments · HN thread

TL;DR Daniel Lemire shows that in C++ a default constructor written as span() = default makes the type trivial, while span() {} makes it user-provided, and the two produce different code for value-initialization. In his benchmark (GCC 15 and 16, -O2, one core of a Xeon Gold 6548N), the = default version runs over four times slower on a loop that builds and passes spans.

  • span() = default on first declaration makes the struct trivial; span() {} is user-provided and therefore not trivial.
  • Value-initializing a box whose member span is default-constructed behaves differently depending on which form is used.
  • With span() {}, GCC 16 writes the three fields directly into the object passed to consume.
  • With span() = default, GCC 16 stores one field directly, routes the other two through a temporary, and copies 16 bytes back.
  • Benchmark: best of five runs over 100 million spans; with GCC 15 and GCC 16 the = default version is over four times slower.
  • Lemire's code is publicly available.

Discussion The thread quickly drifts from the codegen issue into a broad debate about C++ complexity and constructors versus Rust, Go and Zig style create-and-return functions. Many commenters jokingly or ruefully describe C++ initialization rules as a minefield, while several C++ developers defend the language as powerful and malleable. Overall mood is wry and skeptical of C++, with a few experienced users pushing back that the language is manageable with discipline and static analysis.

  • Constructors are unnecessary; create-and-return functions are simpler (some): Several commenters argue that languages like Rust, Go and Zig avoid constructors entirely and use static functions returning instances, which is more ergonomic. Others reply that this approach hinders in-place construction and guaranteed copy elision, though Rust's lack of guaranteed elision is acknowledged.
  • C++ is too complex and hard to use well (most): A large share of commenters say C++ has too many rules to memorize, with initialization cited as an example. Many describe the language as horrifying at first and frustrating later, even for experienced users.
  • C++ is powerful and malleable, and its complexity is the price (some): Some argue that the operator and syntax overloading that makes C++ flexible also makes it complex, and that this is a deliberate trade-off. Others say that C++ suits both expert and solo work because of this flexibility.
  • Use a small safe subset and static analysis (many): Commenters recommend restricting C++ to a subset, following guidelines such as the JSF AV rules, and enabling linters and static analysers. Some add that the subset must be chosen by someone who knows the full language.
  • Zero-initialize members anyway, so the issue is contrived (a few): One commenter notes that the example is artificial because members would normally be zero-initialized in the default constructor, making the two forms equivalent. They cite std::atomic and glm::vec as real cases where default-initialization of primitives led to bugs and changes.
  • Rust is more productive than C++ (a few): A few users report higher productivity and fewer subtle bugs after switching to Rust. Others counter that C++ is easier for writing multithreaded code that the author believes is safe.

Links from the comments

#cpp #constructors #codegen #performance #initialization #programmingLanguages