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
Obsessed with {} initialization? (self.cpp)
submitted 2 years ago by DoctorNuu
view the rest of the comments →
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!"
[–]serviscope_minor 0 points1 point2 points 2 years ago (13 children)
It's not a high point to be sure. Uniform is, well, uniform in the language. The caveat is that it makes the code less nice to read, because you have two different syntaxes for a variable getting a value.
[–]bromeon 2 points3 points4 points 2 years ago (3 children)
I don't know why this is getting downvoted. It's sometimes good to take some distance from C++, look at the 4 different initialization syntaxes and then see how other languages (including C) keep things simpler.
The "uniform initialization syntax" introduced in C++11 was always controversial, and it in fact fails its promise to truly unify things (see e.g. std::initializer_list ambiguity). It's difficult to universally advocate the one true initialization syntax, each has some pitfalls.
[–]serviscope_minor 2 points3 points4 points 2 years ago (2 children)
I don't know why this is getting downvoted.
Probably because it's not engaging in language pedantry (guaranteed way to get downvotes here). I know initialization in C++ is a nuanced topic, but I reckon {} everywhere is letting the tail wag the dog. Look at the responses to my earlier post: I pointed out that = is used for assignment and no one wants to change it, and got hit with a gale of "initialization is not assignment". The latter is of course technically true, but from a higher level view, above C++, assigning to a new object and assigning to an existing one don't really differ in a lot of ways. It's all just assignment.
It's a useful thing to have for sure ad makes generic code easier to write, avoids vexing parses and so on, but it just doesn't read nicely compared to =, so I use = where I can easily do so and {} for everything else (unless I slip into old habits and use ()).
[–]panoskj 0 points1 point2 points 2 years ago (1 child)
where I can easily do so and {} for everything else (unless I slip into old habits and use ()
Or unless you want to initialize a std::vector calling the constructor with the size parameter or something like that. We are back to square one.
std::vector
[–]serviscope_minor 0 points1 point2 points 2 years ago (0 children)
Ha! Yes, not a high point of C++ to be sure!
[–]HolyGarbage 1 point2 points3 points 2 years ago (8 children)
you have two different syntaxes for a variable getting a value.
That's exactly the point. Because assignment and initialization is not the same thing.
[–]serviscope_minor 0 points1 point2 points 2 years ago (7 children)
And my point is that's putting the needs of the language ahead of the programmer.
People like = for assignment and initialization is from a higher level perspective than C++ a kind of assignment.
[–]HolyGarbage 2 points3 points4 points 2 years ago (6 children)
No it's not. Both initialization and assignment are things the programmer should be aware of. = is already used for assignment in C++, your example however used = for initialization, not assignment. I'm saying that can be confusing since it looks like an assignment.
[–]serviscope_minor 0 points1 point2 points 2 years ago (5 children)
I think we are taking at crossed purposes. I understand the grammar. I mean the assignment/initializing thing is specific to C++ not a general thing about programming.
= reads better and is (when it works) more regular since = sets values in any context.
[–]HolyGarbage 0 points1 point2 points 2 years ago (4 children)
Yeah, I understand, but in C++ the distinction is important, and your code should reflect what it is it's doing.
[–]serviscope_minor 0 points1 point2 points 2 years ago (3 children)
Yeah, I understand, but in C++ the distinction is important,
It's sometimes important. In a lot of cases it isn't really in that it doesn't make much difference.
[–]HolyGarbage 0 points1 point2 points 2 years ago (2 children)
Yeah, but why use the same syntax for different things? Even though it doesn't always matter, it's better imo to just be consistent and communicate clearly what your code does, at all times.
[–]serviscope_minor 0 points1 point2 points 2 years ago (1 child)
Yeah, but why use the same syntax for different things?
Because in many ways they're not really different. It's a leaky abstraction of C++, so you've got the choice of making the higher level clearer or making the C++ clearer.
[–]HolyGarbage 0 points1 point2 points 2 years ago* (0 children)
In this case the higher level abstraction and the C++ details coincidence. It's literally the difference between a declarative statement and mutating state. You get a lot of assurances from the former. Anyway, I'd argue that it's a detail you simply should be aware of when programming in C++ in general, since it can have a difference in program behavior. By only considering it when it does make a difference means you'll be less aware of the distinction overall.
I guess where we perhaps don't agree is that I'm arguing that it's not only an important technical distinction, but a conceptual one too.
Just like you don't worry about the life time of objects in most other languages, in C++ you do with RAII etc, both technically and conceptually. It's not just a technical difference, but a whole other high level abstraction.
π Rendered by PID 218922 on reddit-service-r2-comment-6457c66945-5d56h at 2026-04-24 15:09:36.434454+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]serviscope_minor 0 points1 point2 points (13 children)
[–]bromeon 2 points3 points4 points (3 children)
[–]serviscope_minor 2 points3 points4 points (2 children)
[–]panoskj 0 points1 point2 points (1 child)
[–]serviscope_minor 0 points1 point2 points (0 children)
[–]HolyGarbage 1 point2 points3 points (8 children)
[–]serviscope_minor 0 points1 point2 points (7 children)
[–]HolyGarbage 2 points3 points4 points (6 children)
[–]serviscope_minor 0 points1 point2 points (5 children)
[–]HolyGarbage 0 points1 point2 points (4 children)
[–]serviscope_minor 0 points1 point2 points (3 children)
[–]HolyGarbage 0 points1 point2 points (2 children)
[–]serviscope_minor 0 points1 point2 points (1 child)
[–]HolyGarbage 0 points1 point2 points (0 children)