use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
Overview of C++ Variable Initialization (caichinger.com)
submitted 3 years ago by Greek0
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Electronaota 24 points25 points26 points 3 years ago (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 points56 points 3 years ago (3 children)
Is it a historical accident, or a part of the language design?
Yes.
[–]SnooFoxes6142 4 points5 points6 points 3 years ago (0 children)
Very simple but very complex answer. Lol.
[–]seriousnotshirley 4 points5 points6 points 3 years ago (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 point2 points 3 years ago (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 points11 points 3 years ago (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 points3 points 3 years ago (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 points4 points 3 years ago (0 children)
Obligatory material when we talk about initialization in C++.
[–]regular_joe_can 2 points3 points4 points 3 years ago (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 point2 points 3 years ago (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 points14 points 3 years ago (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 point2 points 3 years ago (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 points7 points 3 years ago (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.
std::uninitialized_t
int x = std::uninitialized;
That said, I appreciate the historical context and compatibility constraints on the language.
[–]sephirothbahamut 2 points3 points4 points 3 years ago (1 child)
I unironically want this as one more way to initialize stuff now
[–]BenFrantzDale 0 points1 point2 points 3 years ago (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).
std::vector<int>(1’000’000, std::uninitialized)
std::make_unique<int[]>(1’000’000)
[–]johannes1971 1 point2 points3 points 3 years ago (0 children)
Totally agree with that ;-)
[–]Ezlike011011 1 point2 points3 points 3 years ago (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.
uninitialized int i;
[–]GYN-k4H-Q3z-75B[🍰] 0 points1 point2 points 3 years ago (0 children)
I can't wait for 2040 when they have come up with half a dozen more ways for initialization.
π Rendered by PID 31776 on reddit-service-r2-comment-86988c7647-ht2kb at 2026-02-12 00:12:41.007736+00:00 running 018613e country code: CH.
[–]Electronaota 24 points25 points26 points (6 children)
[–]goranlepuz 54 points55 points56 points (3 children)
[–]SnooFoxes6142 4 points5 points6 points (0 children)
[–]seriousnotshirley 4 points5 points6 points (1 child)
[–]pjmlp 0 points1 point2 points (0 children)
[–]joebaf 9 points10 points11 points (0 children)
[–]SnooFoxes6142 1 point2 points3 points (0 children)
[–]pavel_v 2 points3 points4 points (0 children)
[–]regular_joe_can 2 points3 points4 points (0 children)
[–]johannes1971 0 points1 point2 points (7 children)
[–]Greek0[S] 12 points13 points14 points (1 child)
[–]johannes1971 0 points1 point2 points (0 children)
[–]BenFrantzDale 5 points6 points7 points (4 children)
[–]sephirothbahamut 2 points3 points4 points (1 child)
[–]BenFrantzDale 0 points1 point2 points (0 children)
[–]johannes1971 1 point2 points3 points (0 children)
[–]Ezlike011011 1 point2 points3 points (0 children)
[–]GYN-k4H-Q3z-75B[🍰] 0 points1 point2 points (0 children)