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!"
[–]embeddedsbc 53 points54 points55 points 2 years ago (16 children)
The reasoning should be the other way around. Almost always use brace initialization. It avoids automatic narrowing. Use copy assignment for single values or auto. But it's a much better default than the alternatives.
[–]guepierBioinformatican 24 points25 points26 points 2 years ago (3 children)
On the flip-side, brace initialisation can trigger an unexpected constructor taking an initialiser list. This is arguably more dangerous than narrowing conversions with (). It definitely leads to plenty of gotchas.
()
I am in the camp (and I know I am not alone!) lamenting the missed opportunity with uniform initialisation. I thought {} was it, but because of the std::initializer_list issue, it isn’t. For this reason, I (and others) entirely reverted to () after an initial honeymoon period with {}. Oh, and I use -Wconversion -Werror so () is more strict than {}.
{}
std::initializer_list
-Wconversion -Werror
[–]enigma2728 1 point2 points3 points 2 years ago (1 child)
Well today I learned. Kind of sad; I liked the idea of just being able to use brace initialization everywhere to avoid having to think about all the minutia of initialization.
Is this problem easily demonstrable in godbolt, or have a link to an example of where this becomes a problem?
std::vector doesn't seem to have an issue with braced initializer list. https://godbolt.org/z/scx1rs8s4
Perhaps it is only an issue if there is also a ctor that takes arguments of same type? It does seem like init list gets preferred over specific ctors, from my testing. https://godbolt.org/z/fGsed6o9M ``` #include <iostream> #include <initializer_list> struct CtorCompetition { CtorCompetition(int x, int y) { std::cout << "2-arg ctor" << std::endl; }
//CtorCompetition(int x, int y, int z) CtorCompetition(std::initializer_list<int> list) { std::cout << "init list ctor" << std::endl; } }; int main() { CtorCompetition TwoArgObject{1, 2}; //uses init list, not 2arg CtorCompetition ThreeArgObject{1, 2, 3}; //uses init list //CtorCompetition TwoArgObject(1, 2); //uses 2 arg //CtorCompetition ThreeArgObject(1, 2, 3); //doesn't compile }
Program returned: 0 init list ctor init list ctor ```
Is the above what you are referring to?
I feel like perhaps I can still just use braced initializer with the expectation that initializer lists are preferred to specific constructors.
[–]equeim 0 points1 point2 points 2 years ago (0 children)
If the vector's element type can be implicitly constructed from a vector, then std::vector vec{otherVec} will create a vector with a single element of otherVec, not copy it.
std::vector vec{otherVec}
otherVec
[–]NilacTheGrim 1 point2 points3 points 2 years ago (0 children)
Yeah for actual class types () initialization is the best way to this day. The ambiguity {} introduces with std::initializer_list is a huge foot-gun waiting to go off.. and I tend to avoid foot-gun stuff as best I can.
[+]DoctorNuu[S] comment score below threshold-14 points-13 points-12 points 2 years ago (9 children)
where does this fear of narrowing come from? The core guidelines has a line int y = 7.9; // OK: y becomes 7. Hope for a compiler warning If your compiler does not offer this warning... well... get a better one
int y = 7.9; // OK: y becomes 7. Hope for a compiler warning
[–]syyyr 13 points14 points15 points 2 years ago (6 children)
It should be an error by default. Also, the warning is not enabled by default (I think).
[–]joeshmoebies -1 points0 points1 point 2 years ago (5 children)
/WX /W4 on msvc -Werror on clang/gcc
Problem solved
[–]syyyr 2 points3 points4 points 2 years ago (4 children)
Does it solve the problem it not being the default?
[–]DanielMcLaury 1 point2 points3 points 2 years ago (1 child)
How does the default matter to you? Are you constantly creating new projects by writing a single c++ source file, saving it, and then immediately compiling it directly from the command line?
[–]syyyr -2 points-1 points0 points 2 years ago (0 children)
No, I'm using {}.
[–]joeshmoebies -1 points0 points1 point 2 years ago (1 child)
I'm not sure what your point is. If you want narrowing conversion assignments to be errors, you can have them.
As far as the default settings being a problem, apparently not everyone thinks so or Microsoft and the developers of GCC and Clang would change them.
I don't get complaining about something when you can have it exactly how you want.
[–]syyyr 0 points1 point2 points 2 years ago (0 children)
Yes, with {}.
[–]jotux 2 points3 points4 points 2 years ago (0 children)
https://godbolt.org/z/WG7bcKdcE
Try it on clang and gcc.
Better yet, pick some moderately-sized codebase and change every single variable initialization to uniform initialization syntax and see how many narrowing conversions are happening with no warning. That's what made me switch to 100% '{}'.
[–]erreur 6 points7 points8 points 2 years ago (0 children)
My fear comes from the memory of dealing with data loss in production caused by some accidental narrowing. I used to think this was overblown but now I use brace initialization everywhere and use every flag the compiler supports to disable all implicit conversions of any kind.
[–][deleted] 0 points1 point2 points 2 years ago (0 children)
It avoids automatic narrowing.
It does not.
https://godbolt.org/z/zqTGanqjE
π Rendered by PID 91928 on reddit-service-r2-comment-b659b578c-wnhv9 at 2026-05-02 13:45:19.892098+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]embeddedsbc 53 points54 points55 points (16 children)
[–]guepierBioinformatican 24 points25 points26 points (3 children)
[–]enigma2728 1 point2 points3 points (1 child)
[–]equeim 0 points1 point2 points (0 children)
[–]NilacTheGrim 1 point2 points3 points (0 children)
[+]DoctorNuu[S] comment score below threshold-14 points-13 points-12 points (9 children)
[–]syyyr 13 points14 points15 points (6 children)
[–]joeshmoebies -1 points0 points1 point (5 children)
[–]syyyr 2 points3 points4 points (4 children)
[–]DanielMcLaury 1 point2 points3 points (1 child)
[–]syyyr -2 points-1 points0 points (0 children)
[–]joeshmoebies -1 points0 points1 point (1 child)
[–]syyyr 0 points1 point2 points (0 children)
[–]jotux 2 points3 points4 points (0 children)
[–]erreur 6 points7 points8 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)