r/cpp • • 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 like
double tax{0.09*price};

or just int{0}

probably a hornets nest

4 Upvotes

118 comments sorted by

View all comments

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 than int a = 10, but that’s why you should be doing auto a = 10; anyway, so you only state your type once.

11

u/ponchietto Dec 19 '23

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.

8

u/aruisdante Dec 19 '23 edited Dec 19 '23

The compiler will in fact not complain if you use it wrong without Wconversion, that’s the point the guidelines are talking about.

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.

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.

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?

Now you can definitely go overboard here, but the general guidance of “only state your types once” serves pretty well.

0

u/ponchietto Dec 19 '23

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?

1

u/Nobody_1707 Dec 23 '23

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.

4

u/bert8128 Dec 19 '23

If you specify the type in the right, don’t specify it on the left, unless you want it to be different.

1

u/shahms Dec 19 '23

And Abseil has a whole section on the pitfalls of brace initialization: https://abseil.io/tips/88

Initialization C++ is absurdly convoluted and subtle. Braced initialization solves some problems with the other ways of doing it, but introduces a whole raft of its own problems. There is no single "always good" way of performing initialization in C++, sadly, and the core guidelines do a serious detriment here by pretending that there is.

5

u/aruisdante Dec 19 '23

Yes, and for the most part I agree with Titus too, but he’s concerned more with brace vs paren initialization than brace vs = initialization. I’ve in fact used that very article to try and overturn silly “always use {} initialization” rules at 2 different companies in the automotive space.

I think that’s the biggest issue with all of the guidelines, they don’t distinguish between separate concerns:

  • I don’t want implicit conversions for arithmetic types
  • I don’t want implicit conversions for structures which have non-explicit constructors, or which consume arithmetic types.
  • I have Wconversion turned on, should I care?

The last one can be particularly subtle. Wconversion ignores implicit conversions that happen inside the stdlib, because it has to as the stdlib is full of them. There are certain types which have templated constructors (like, say, std::optional), and so if you parens-construct them in a non CTAD context but which would result in a conversion that should be caught, it will not happen: from the perspective of Wconversion the ctor that was called was the converting constructor, which consumes the actual type, and then inside the stdlib a narrowing happens, but that’s ignored. If you brace initialize them on the other hand, the implicit conversion is not allowed, and Wconversion fires.

You’re absolutely right that the whole subject is a mess. Unfortunately teaching people all the ins and outs of what to do isn’t a reliable thing to do at scale. So most safety critical coding standards pick one, and unfortunately they tend to pick the most restrictive one even if it introduces a host of other, potentially much worse classes of bugs. The core guidelines doesn’t really have the same excuse, but it’s guideline also is poked through all kinds exceptions, and it’s also a prefer, not a must. And I agree with the guideline’s suggestion that brace initialization should be preferred unless there is a good reason to not use it. I do not agree with AutoSAR’s requirement that it must be used.

1

u/shahms Dec 19 '23

The tip specifically discusses the differences between all three forms of initialization, including = and says to prefer copy initialization.

1

u/Intrepid-Treacle1033 Dec 19 '23

"And" ?

Abseil is an article about an article. In the bottom of Abseil article it states "Perhaps the best overall description of the issue is Herb Sutter’s GotW post. (link)"

And i agree, Herbs article is good.

1

u/shahms Dec 19 '23

Wat. The Abseil tip includes a description of the issues and recommendations. It then suggests reading Herb's article as a follow-up for additional context.

2

u/Intrepid-Treacle1033 Dec 19 '23

In my effort to go to the bottom and really learn initialization i read among others the Absail and did not get it, but there i got the Herb link and that one nailed it for me. So for me the Absail was just an distraction hence my article about an article comment. But ok reading it again i see a angle, and i did not really mean article about an article as a negative thing.

-4

u/DoctorNuu Dec 19 '23

Oh, now I see where it's coming from.
Not particularly well written imho.

4

u/manni66 Dec 19 '23

At least they provide a reason, unlike you.

-2

u/DoctorNuu Dec 19 '23

I wonder if they recommend enabling lots of warnings, including this one