r/cpp • • Mar 01 '21

"Simple" C++ Initialization Flowchart

http://randomcat.org/cpp_initialization/initialization.png
204 Upvotes

65 comments sorted by

View all comments

103

u/atimholt Mar 01 '21

25

u/piglett23 Mar 01 '21

Wow, I recently started a position developing safety-critical software and that really clears up why we use brace-initialization. It seems to just work for most things. Thanks for that!

49

u/FriendlyRollOfSushi Mar 01 '21 edited Mar 02 '21

Never once I've seen the trust towards the committee fall as low as it did when people in my company started to migrate to C++11 and realized that this is their life now. Haven't recovered since, to be honest.

The example is de-obfuscated, obviously: it's usually an issue somewhere deep, sometimes in templated code where you don't even know what the types are just by looking at the code. In a large enough company with enough people coming and leaving and a moderately relaxed review process, you are pretty much guaranteed to have a steady flow of bugs like this. It's a footgun with infinite ammo.

I sort of understand why this happened, but it feels like we as a community completely failed to do things right for once, so that we could stop teaching all other initialization methods to people and pretend that the () syntax doesn't exist and shouldn't be used anymore.

Now I see mixed-style initialization lists all the time (see the bonus part from the link above), and cry internally from time to time while thinking about this whole situation.

Sigh...

14

u/nintendiator2 Mar 02 '21

There's a reason why I always initialize as T variable = T(expr). Vexing parse safe, works across all Standard versions, also with default constructor. Alternatively if I know I don't have to support the three pre-C++11 compilers in my work stack, auto variable = T(expr). Honestly I don't understand how did the Committee fucked things up this bad.

8

u/FriendlyRollOfSushi Mar 02 '21 edited Mar 02 '21

This pattern has its own set of problems. Here is an example.

Braces are massively superior to parens due to how they prohibit narrowing in many cases, but thanks to std::initializer_list, we can't really write code like this with standard containers without shooting ourselves in the foot from time to time.

3

u/nintendiator2 Mar 02 '21 edited Mar 02 '21

That's half your fault tho; you are using a signed type to represent an unsigned concept. And C++ is already horrible at transparent numeric conversions (honestly I would have gotten rid of transparent signed-unsigned conversion with the big changes in C++11, and remade the pattern for fetching the maximum unsigned value from -1 to ~0 which at least makes binary sense, but oh well, missed chances).

As things stand though, I prefer my version. The alternative is to have to ifdef basically every variable declaration for pre and post C++11 mode. And optimization-wise, I just let the compiler do its work, that's why it's running on a computer while I'm (at the moment) running on a human.

1

u/evaned Mar 02 '21 edited Mar 02 '21

I know -Wconversion is a very noisy warning on many code bases (probably most...) given that it's not in -Wall or even -Wextra (as you point out), but if you can turn it on, Clang's version of that warning does yell about that line. (On GCC you'll need -Wsign-conversion.) And as a bonus, get warnings about the other 17 zillion places implicit conversions can happen other than going from an initializer to a variable.

1

u/CenterOfMultiverse Mar 02 '21

Making size const allows conversion to 232 - 5 and then godbolt declines to request to so much memory^^.

2

u/staletic Mar 02 '21

That doesn't work for aggregates before C++20. Since C++20, sure.

3

u/nintendiator2 Mar 02 '21

Ooooh, good catch. I guess there's just no way to win...

...Maybe unless I use a macro?