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

surprisetalk 114 points 174 comments November 25, 2026
lemire.me · View on Hacker News

Discussion Highlights (15 comments)

raincole

No language scares me like C++ does. Look at this page: https://www.learncpp.com/cpp-tutorial/variable-assignment-an... This is just a single section of the first chapter of a beginner tutorial. And it's already more things to remember than, say, the whole Lua.

vinyl7

The only sane usage of C++ is to only use a very small subset of its features.

ninkendo

Constructors are such an odd duck to me. On the one hand, you have languages like C++ and Swift which have construction as a first-class concept, with tons of rules you need to memorize (Swift's "convenience" vs "designated" initializers are a real treat to unpack). Then you have languages like Rust, Go, Zig, etc which don't have them at all, constructors are just static functions in the namespace of the type, which return an instance of that type. For the life of me I've never understood why you'd want the former. I'm sure there's a reason, but I never miss it when using languages in the latter category. The explanation you typically hear is about OO and inheritance, but I don't buy it: If Animal had a ::new() that initialized private state, Cow could have a ::new() too that could call Animal::new() if it wanted, then do further initialization, etc... I don't think there's anything about inheritance that says constructors are a must. (Edit: In fact, ObjC does exactly this. I should have remembered that given that I did ObjC for a living for a while.)

spacechild1

The example is pretty contrived because in practice you'd want to zero-initialize the members in the default constructor anyway. struct span { const char *p = nullptr; size_t len = 0; span() = default; // or span() {} span(const char *s, size_t n) : p(s), len(n) {} }; In that case, there is no difference between span s; and span s{}; There are only few use cases where you want a default constructor that does not initialize its members. One example are wrappers around primitive types where you want to closely preserve the original semantics. Whether that's a good idea in the first place really depends on the use case. For example, std::atomic used to default-initialize the underlying value, which led to many bugs in user code. Since C++20 std::atomic uses value-initialization. See https://cplusplus.github.io/LWG/issue2334 . On the one hand, std::atomic is now safer to use, on the other hand, it is not an implicit-lifetime-type anymore, which means you can't put it in shared memory without UB... Another example is glm::vec, which has actually hit me a few times in my own code. At least they recognized the issue and allowed to override the default behavior with the GLM_FORCE_CTOR_INIT define.

jeffbee

Which is of no consequence to LLVM

BiraIgnacio

obligatory meme https://imgur.com/forest-gump-c-3wlxtI0

rcleveng

GCC going from effectively the same performance in 14.x to 4x slower in 15.x AND 16.x sounds like a real regression that should be fixed.

dist-epoch

For people who don't know, the number in C++14, C++26, ... is how many ways there are to initialize a variable.

morning-coffee

This is one of the many reasons I left C++ (after investing over 20 years professionally in it) for Rust. Sometimes it is just better to get on with life than deal with pedantic minutiae all the time.

fukaiall

C++ feels super natural to me but at the same time it is a miserable fuck due to such odd legacies and ununified features. People literally have spent ages to simply learn those unnecessarily lots of syntaxes rather than solving actual problems. So many compilers and language standards. I do detest those motherfucking Rust fanboys but reasonbly speaking you should choose either C or Rust for low new level projects in 2026.

jbaber

https://www.reddit.com/r/ProgrammerHumor/comments/8nn4fw/for... Some of these stumped colleagues.

otikik

Ah, another footgun. How nice.

nlehuen

Not to worry, there is a 278 page book about initialization in C++! https://leanpub.com/cppinitbook (I don't know whether it's good or not, I just find it fascinating that it exists)

Panzerschrek

What prevents C++ compilers to process a constructor with inline body consisting only of {} as =default? As a compiler developer I can say, that such a change is almost trivial to implement.

krylon

Sometimes I think that maybe I should get reacquainted with C++. Then I see a headline like this, and am reminded I really shouldn't. No hate, but that just is not for me. I deeply respect people who can wrap their brains around all these arcane rules, but my brain just refuses to do that.

Semantic search powered by Rivestack pgvector
4,151 stories · 37,739 chunks indexed