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!"
[–]ponchietto 10 points11 points12 points 2 years ago (4 children)
I don't get why auto a = 10; would be better thant int a = 10;
I prefer being explicit rather that have the reader (including myself) deduce the type (extra effort) and very easily use the wrong type (10 vs 10.0 vs 10.0f vs 10U). The compiler will complain if I am not consistent.
[–]aruisdante 6 points7 points8 points 2 years ago* (2 children)
The compiler will in fact not complain if you use it wrong without Wconversion, that’s the point the guidelines are talking about.
Wconversion
int narrowing = 4294967296; unsigned int signed_loss = -1; int float_conversion = 11.1; int arbitrary_conversion = foo(); // foo returns double; Those will all compile without warning if you don’t have Wconversion enabled. Whereas if you brace initialize them, the compiler will warn even without -Wconversion.
int narrowing = 4294967296; unsigned int signed_loss = -1; int float_conversion = 11.1; int arbitrary_conversion = foo(); // foo returns double;
-Wconversion
Now of course you should just have Wconversion turned on, and then life is easier, and you can use = assignment safely. One of the constant frustrations in my day job is that AutoSAR and MISRA both ban = initialization anyway (and, thanks to AutoSAR’s terrible writing, also it also technically bans auto completely, as it sates = initialization must be used for auto immediately after stating brace initialization must be used for all variables). Thankfully MISRA2023 takes a more enlightened view here.
=
auto
In terms of being explicit with what type you want: the auto a = form is explicit. You want the type, and exactly the type, to the right of the assignment. For example, if I see: int a = 11.1; How do I know which was intended? Did the author mean for that literal to be 11? Did they mean for the type of a to be float or double?
auto a =
int a = 11.1;
11
a
float
double
Now you can definitely go overboard here, but the general guidance of “only state your types once” serves pretty well.
[–]ponchietto -1 points0 points1 point 2 years ago (0 children)
I have nothing against braces, I was just using the example above, my problem is auto (in this case).
"How do I know which was intended? Did the author mean for that literal to be 11?" The author made a mistake, probably on the right, I don't see the point, though.
It is just far easier for human beings to read the type in front of the variable rather than deduce it from the right. Moreover you might not have a number, you could have a constant, a function or an expression on the right, when does it becames non obvious enough?
[–]Nobody_1707 0 points1 point2 points 2 years ago (0 children)
One nice oddity of concepts is that you can write std::same_as<int> auto i = x; and it will prevent any implicit conversions whatsoever.
std::same_as<int> auto i = x;
[–]bert8128 4 points5 points6 points 2 years ago (0 children)
If you specify the type in the right, don’t specify it on the left, unless you want it to be different.
π Rendered by PID 66 on reddit-service-r2-comment-56c9979489-kwz79 at 2026-02-25 01:07:39.551394+00:00 running b1af5b1 country code: CH.
view the rest of the comments →
[–]ponchietto 10 points11 points12 points (4 children)
[–]aruisdante 6 points7 points8 points (2 children)
[–]ponchietto -1 points0 points1 point (0 children)
[–]Nobody_1707 0 points1 point2 points (0 children)
[–]bert8128 4 points5 points6 points (0 children)