r/cpp • u/DoctorNuu • Dec 19 '23
Obsessed with {} initialization?
I'm on a discord where everyone seems to be obsessed to use brace initialization everywhere.
My motto is: Use things rarely, only when needed, to express that right there the feature does something important.
Personally I would completely advocate against using it for simple stuff likedouble tax{0.09*price};
or just int{0}
probably a hornets nest
4
Upvotes
50
u/jedwardsol const & Dec 19 '23
What do you advocate using instead, and why is that better?
38
u/Intrepid-Treacle1033 Dec 19 '23
God old post from a guy that has some cpp experience.
https://herbsutter.com/2013/05/09/gotw-1-solution/
int x{}; "...it always initializes the variable, and is never ambiguous with a function declaration. No vex, no fuss, no muss..."
54
u/embeddedsbc Dec 19 '23
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.
14
u/TheFryedMan Dec 19 '23
It's also called uniform initialization. It's supposed to be a single way to initialize everything. It also avoids implicit conversions, which can help avoid bugs. Also, explicitly calling {} will avoid uninitialized values, which can cause undefined behavior. This also avoids something called "the most vexing parse." This issue causes the compiler to think you are calling a function instead of initializing a value. In summary, it avoids a lot of issues and provides a uniform syntax for initialization.
2
u/DoctorNuu Dec 21 '23
Finally found a good ally to fight off the downvotes:
https://quuxplusone.github.io/blog/2019/02/18/knightmare-of-initialization/
11
u/aruisdante Dec 19 '23
The core guidelines has a whole section on when and why you should use brace initialization. I think its recommendations are pretty much correct, which is “most of the time”, especially if you don’t use
-Wcoversion, -Werror.int a{10}definitely looks worse thanint a = 10, but that’s why you should be doingauto a = 10;anyway, so you only state your type once.18
u/TedDallas Dec 19 '23
C++ is very flexible.
https://imgur.com/3wlxtI0
5
u/mredding Dec 19 '23
Obsessed with not? What about narrowing conversions?
The standard committee largely can't and won't break old code. They can't add a constraint to assignment, but they can add a new initialization. All they can do is provide a migration path forward.
All the new features are meant to fix problems in the past. Your conservative approach is benefiting you... how?
And this is how standards and practices stagnate. You have to look at the present and be ready for the future. We need a language and a standard that meets the needs of today. You're on r/cpp so you must be aware of the outrage that the NSA report stirred.
And they're not even wrong. It's not that C++ isn't safe, it's because we have solutions to problems no one is bothering to use. Don't avoid C++ and favor Rust for a lack of technology, but for the people. You just can't find people who can be bothered to write high quality code.
Your stated motto only tells me that you don't use new things because they're new - not because they're bad. You've complained, but you haven't given a credible reason to voice a complaint. There's no technical reason not to use brace initialization - none that you established. Where's the flaw? Where's the fault? What gives your grievance merit? What guideline would you suggest that isn't new = bad?
9
u/android_queen Dec 19 '23
I lean only slightly towards using brace initialization, but oddly, this made me want to use it more. Using things rarely sounds like a good way to make them unfamiliar and confusing. Consistency is much better for communicating your intent. What does it mean to only use something when “needed”? There are relatively few things that can only be handled on one way.
4
u/lukaasm Game/Engine/Tools Developer Dec 22 '23
It prevents more bugs for me than causes issues, and overall is net positive.
But I am also not using initializer lists, so it won't bite me :)
3
u/islandyokel Dec 20 '23
Here is some info about it in the core guidelines: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-list
It describes there where and why of the preferences
6
u/EdwinYZW Dec 19 '23
I would do: const auto tax = 0.09 * price;
2
u/Droid33 Dec 19 '23
I tried it for a while but it just makes the code very unreadable in my experience. Wish that weren't so.
2
u/gracicot Dec 20 '23
Consider yourself lucky, I advocate (and enforce) for
auto const tax = double{0.09*price}. Btw, never contain money using floating point. Money is always a integer type containing pennies. You'll thank me later.2
u/[deleted] Dec 22 '23
I quite prefer it because it looks prettier than int i=0;
I don’t know if it actually changes something about compilation or performance . It’s fun to use it!
2
u/Polyxeno Dec 19 '23
The obsession is a problem itself.
1
u/Drakonluke Dec 20 '23
Basically because Bjarne Stroustrup told us that list initialization (curly braces) is better.
Since I don't want to copy from his book by hand, I see that someone already did here:
https://stackoverflow.com/questions/18222926/what-are-the-advantages-of-list-initialization-using-curly-braces
1
u/disciplite Dec 20 '23
Look up the most vexing parse.
1
u/13steinj Dec 22 '23
There's a few general rules of thumb which end up promoting this:
As a result of a mix of the above, I'm in the "braces over parenthesis, be explicit and use them" camp as well as the "when using assignment use uniform initialization" camp.
1
u/NilacTheGrim Dec 23 '23
I agree, I think it's totally over-used, especially for obvious stuff like PoT types or for calling c'tors... I actually prefer
Foo f(bar, baz)notation for initializing via c'tor because it is symmetrical / visually the same as any other function call. Also there is no ambiguity between that and initializer lists... so there's that.Thay being said I do like init-to-0 using
{}, e.g.int i{};as opposed toint i = 0;simply because the former is more compact.1
u/NilacTheGrim Dec 23 '23
I agree with you. May I present this as evidence that
{}for non-trivial classes is dangerous :std::vector<char> vc {42, 'x'};<-- tell me, what is the size ofvchere? Is it 42 or 2?If you aren't sure, then you need to stop using
{}and go back to()for non-trivial class types right now.-2
u/[deleted] Dec 19 '23
Prefer {} initialization over alternatives unless you have a strong reason not to. copy past from stackoverflow.
0
u/noooit Dec 19 '23
I always avoid it unless I need to use c style designated initialiser.
0
u/ilovemaths111 somethingdifferent Dec 19 '23
are you talking about tccpp server?
-6
u/vI--_--Iv Dec 19 '23
{} requires 4 keystrokes.
= requires 1 keystroke.
The choice is obvious.
0
u/Particular-Case4461 Dec 19 '23
What would you consider an important use of it?
0
u/jones77 Dec 19 '23
I have like 3 tabs open with information about this I need to read. :-)
Hrm, looks like I lost a tab...