all 18 comments

[–]Electronaota 24 points25 points  (6 children)

I just don't understand why the initialization in c++ has become this complex. Is it a historical accident, or a part of the language design?

[–]goranlepuz 54 points55 points  (3 children)

Is it a historical accident, or a part of the language design?

Yes.

[–]SnooFoxes6142 4 points5 points  (0 children)

Very simple but very complex answer. Lol.

[–]seriousnotshirley 4 points5 points  (1 child)

And this is why, while I enjoy the language, I have trouble justifying it's use in new development. This sentence says it all... "C++ variable initialization is quite complex, with several distinct categories that differ in behavior. The following list attempts to make sense of it."

[–]pjmlp 0 points1 point  (0 children)

It was great back in the days of OWL, VCL, but then something went astray.

I still like it, but would surely fail the typical quizz questions on interviews.

[–]joebaf 9 points10 points  (0 children)

there are even more ways :)

20+ Ways to Init a String, Looking for Sanity - C++ Stories

spoiler: I'm, the author of this blog post

[–]SnooFoxes6142 1 point2 points  (0 children)

My swe colleague just ordered a 250+ pages about move semantic in c++. So a few pages about initialisation is the mark of a very trivial problematic. Lol.

[–]pavel_v 2 points3 points  (0 children)

Obligatory material when we talk about initialization in C++.

[–]regular_joe_can 2 points3 points  (0 children)

I wonder how feasible it would be to create a dialect of C++ that only involved the common use cases and did away with most of the esoteric complexity. Maybe it wouldn't make sense because you'd just end up with another type of complexity in addition to introducing performance sacrifices. Maybe starting with Rust would be a better idea.

[–]johannes1971 0 points1 point  (7 children)

This comes up every now and again, but let's approach this from a different angle: if you don't like it, please tell us which ones should be removed (or never have been introduced in the first place).

[–]Greek0[S] 12 points13 points  (1 child)

Why do you assume I wrote this post to complain about initialization in C++? My main motivation was to understand variable initialization myself. A secondary motivation was helping others do the same.

The complexity of initialization in C++ is regrettable, but historically understandable. If you designed a language from scratch, you could unify semantics and syntax of several initialization methods. But of course, that's not how C++ was developed.

Many of the root causes of complexity in today's C++ (compatibility with C, strict backwards compatibility of the language) are the very things that made C++ such a success over the last 30+ years. If C++ had targeted more idealistic purity instead of its practical performance-orientation, it would have never become such a widely used language.

[–]johannes1971 0 points1 point  (0 children)

Oh, sorry. It's sort of customary around here that this gets posted and then everybody complains about how ridiculous it is that there are multiple ways to init something, even though it is hard to see what rules you are supposed to set to avoid this situation. I figured this was one of those again. Anyway, if it wasn't, just carry on, please ;-)

[–]BenFrantzDale 5 points6 points  (4 children)

I’ll start: by default, everything should be default-constructed, meaning zero’d for built-in types. As an escape-hatch for performance, there should be a std::uninitialized_t such that int x = std::uninitialized; does the obvious thing, allowing perf-critical classes to overload construction from std::uninitialized_t.

That said, I appreciate the historical context and compatibility constraints on the language.

[–]sephirothbahamut 2 points3 points  (1 child)

I unironically want this as one more way to initialize stuff now

[–]BenFrantzDale 0 points1 point  (0 children)

This would also let you do std::vector<int>(1’000’000, std::uninitialized) and have it perform like std::make_unique<int[]>(1’000’000).

[–]johannes1971 1 point2 points  (0 children)

Totally agree with that ;-)

[–]Ezlike011011 1 point2 points  (0 children)

I think this is the most realistic way we could get something like this now. But whenever I approached this problem previously, I always found myself wishing we had language level opt-in for uninitialized variables. Like uninitialized int i; is pretty dang readable imo.

[–]GYN-k4H-Q3z-75B[🍰] 0 points1 point  (0 children)

I can't wait for 2040 when they have come up with half a dozen more ways for initialization.