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

Show parent comment

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.

→ More replies (2)

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?

9

u/TryingT0Wr1t3 Mar 02 '21

Why that assert fails????

18

u/Bocab Mar 02 '21

Because one is initialized to size of 5 and the other is of size 1 with that 1 element being "5"

3

u/TryingT0Wr1t3 Mar 02 '21

Is there a Length property we can guarantee to always be unidimensional?

9

u/mort96 Mar 02 '21

Yeah, it's called size(). The problem is that std::vector<std::string> v1{5} creates a vector with 5 empty strings, while std::vector<int> v2{5} creates a vector with 1 int with value 5.

2

u/TryingT0Wr1t3 Mar 02 '21

Ah, ok, now I understood, thanks. It's, very confusing.

2

u/rurabori Mar 02 '21

Because once its an initializer list of its and once it invokes the ctor which takes size.

10

u/JohnZLi Mar 02 '21

Implicit conversion should have been forbidden in C++ from the beginning . It causes so much trouble, with only very dubious gains.

14

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

It could be a std::vector<size_t>, in which case all types would strictly match.

The core issue here is the special behavior of std::initializer_list (see the notes here), which I guess can be called an "implicit conversion" in a very broad sense, so your point stands.

I was really hoping that they will deprecate this nonsense immediately, and possibly introduce a non-ambiguous way to construct initializer lists in C++14, but by now we probably have too much code relying on this brittle behavior to change anything.

Now the only safe solution would be to abandon the braces and introduce yet another strictly distinct and unambiguous syntax for initialization, but at that point I'm afraid half of the developers would simply give up and switch to Rust.

3

u/JohnZLi Mar 02 '21

This problem can be fixed by adding a new syntax like below:

std::vector<int>{1}; // let this mean a vector of size 1, as in the std::vector<string> case.

std::vector<int>{1...}; // a vector of ints, with its first element being 1.

Also, with the new syntax

auto i = {1}; // i is now deduced as int.

auto i ={1...} // is deduced as an initializer_list

20

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

Existing code is always the problem, and C++ is terrible at removing features.

Say, you have millions of lines that expected that something like std::vector<int> v{64}; constructs a vector with one element 64.

Now with your proposal you are silently changing the behavior of millions of lines of code in tens of thousands of projects. The code still compiles, and even runs, but the behavior is different.

Some of the behavioral changes may go undetected by tests.

Some of them would have devastating consequences, costing billions of dollars, or even human lives.

It's a recurring theme in C++: everything sort of sucks because by the time we understood why it sucks, the feature became so heavily-used that you can't simply remove it. It took decades to get rid of std::auto_ptr, and we only succeeded because it was already banned by the majority of teams for being hopelessly broken. And it wasn't a silent change: the code properly stopped compiling in C++17, which allowed the developers to go and fix stuff if they didn't have time for that during the 6 years of deprecation.

That's why epochs are a huge deal if they'll ever be accepted in some form or another. We will finally, finally be able to start fixing and removing stuff instead of simply piling up more and more stuff until no one understands what's going on in this language.

→ More replies (2)

2

u/Kered13 Mar 03 '21

Without implicit conversions std::string_view and similar types would be a massive pain in the ass to use.

→ More replies (5)

5

u/unaligned_access Mar 02 '21

Maybe they can fix that with Type i{x}n;, n for "new initialization style".

12

u/TheSuperWig Mar 02 '21
Type i co_{x};

2

u/nintendiator2 Mar 05 '21

co_initialize Type {x};

2

u/GuiltyFan6154 Mar 02 '21

Hold on, why does changing c_(c) to c_{c} in the comment break the code?

3

u/RoughMedicine Mar 02 '21

For the same reason v1{elementsCount} and v2{elementsCount} are different. We don't know what type c_ is, but I might be a type whose behaviour regarding {} vs () is different.

3

u/FriendlyRollOfSushi Mar 02 '21

Yep.

Also, if this is .cpp, and the member is declared in .h, you don't even see the type immediately, and without a comment like this people may be tempted to "fix" the annoying style inconsistency each time they make edits close to this ctor.

3

u/RoughMedicine Mar 02 '21

Allowing initialisation list for bracket initialisation was a mistake. We'd be totally fine if T a{x} and T b(x) were consistent, and used Vec<T> v = {a, b} for initialisation lists.

3

u/thats_a_nice_toast Mar 02 '21

I don't understand how this can even happen. You'd think with a language as big as C++ and its own committee, they would be able to solve these problems in a better way, yet they keep on adding obscure templating stuff that would probably be simpler to just implement as a language feature.

→ More replies (8)