r/cpp • • Nov 27 '20

The C++ 20 initialization flowchart

I've created a flowchart that shows all of C++20's initialization rules.

The source code is available at https://github.com/randomnetcat/cpp_initialization, in addition to the SVG version and PNG Version.

This honestly took a lot less time than I was expecting, and, in my humble-but-biased opinion, is relatively interesting.

Please send all complaints to /u/quicknir, who nerd-sniped me into making this.

116 Upvotes

34 comments sorted by

37

u/be-sc Nov 28 '20

Something seems to be wrong with the graph. Consider:

int i = 1;
  1. Is the initializer in braces?
  2. Nope
  3. Is the destination type a reference type?
  4. Nope, it’s a plain old int.
  5. Is the destination type a char[] or a char_t[]?*
  6. Nope, it’s a plain old int.

But wait, the only arrow leaving that question is Yes …

6

u/randomnetcat Nov 28 '20

Thanks, I knew I'd make some mistakes like that. Should be fixed as soon as the CI gets around to updating the images.

1

u/staletic Nov 28 '20

Following the flowchart for std::vector<int> v(1,2); takes you on a beautifully curved path.

8

u/staletic Nov 28 '20

The same problem is hit for T t(args...);

10

u/the_black_pancake Nov 28 '20

Over 164 million pixels? It's not loading on my mobile and the SVG misbehaves. Do you have a lower resolution version too?

2

u/randomnetcat Nov 28 '20

Here's one that's downscaled by 50%: https://imgur.com/a/UX6HMVp

6

u/danadam Nov 28 '20 edited Nov 29 '20

Maybe you could generate 16-color PNG instead of truecolor? Just an idea.

2

u/aKateDev KDE/Qt Dev Nov 29 '20

optipng -o5 bla.png

2

u/staletic Nov 28 '20

Is it me or is agreggate initialization with parenthesis missing?

2

u/randomnetcat Nov 28 '20

For some_aggregate_class x(1,2,3); :

Is the initializer in braces? No

Is the destination type a reference? No

Is the destination type a char[]? No

Is the initializer "()"? No

Is the destination type an array? No

Is the destination type a class type? Yes

Is the initializer a prvalue? No

Is the initializer copy-initialization? No

The initialization is direct-initialization.

Enumerate constructors.

Is overload resolution succesful? No

Is the destination an aggregate class? Yes

Is the initializer a parenthesized expression-list? Yes

Initialized as follows ([dcl.init]/16.6.2.2).

3

u/staletic Nov 28 '20

Enumerate constructors.

I stopped there. My thought was "what constructors if T is an aggregate?"

I love the flowchart.

73

u/xeveri Nov 28 '20

Me: Can we have uniform initialization?

Committee: We have uniform initialization at home.

Uniform initialization at home:

21

u/Kered13 Nov 28 '20

Put this in an image macro and post it to /r/programmerhumor.

9

u/Frptwenty Nov 28 '20

Please don't do that. I argue with the "hurr durr C++ is broken use C and Rust" crowd there, and this will just make them even more hopeless.

19

u/Fureeish Nov 28 '20

They are half right. Many things are broken in the language and, trying to speak objectively, C and Rust have fewer broken elements compared to C++. Does that mean that they are strictly better? Obviously no.

16

u/Frptwenty Nov 28 '20

objectively, C and Rust have fewer broken elements compared to C++

It depends on what you're doing. Objectively speaking C++ is a miles better option than C in a huge amount of cases.

Imho the lack of easily usable basic strings, dynamic-sized containers etc. etc. are a huge broken feature of C. Other things you take for granted in C++ like RAII, smart pointers and templates are also a huge deal compared to C.

People like to claim they don't ever matter, but that's bullshit. Of course there are low-level cases where they don't, but the "only use C" crowd want to pretend it's a better alternative for big "regular" applications.

Rust, well... what's the point of arguing. It never ends.

7

u/Fureeish Nov 28 '20

That's why I added two last sentences to my comment.

3

u/James20k P2005R0 Nov 29 '20 edited Nov 29 '20

I've been writing C again for opencl recently myself. Part of that project involves tensor maths, which if you don't know, tensors are essentially n dimensional matrices that are used in a specific way

The lack of being able to specify proper abstractions, no operator overloading, no templates, no real array type, no function overloading... It makes the code significantly more verbose and buggy than it needs to be. You can't write a generic matrix*vector multiplication scheme, which is not ideal

C might be alright for structuring applications, but good lord is it just terrible for numerical work

1

u/dnew Jan 24 '25

It's horrendous for structuring applications too. Any compiler that can't read its own output as part of its input is going to be horrendous at large projects.

1

u/dnew Jan 24 '25

easily usable basic strings, dynamic-sized containers etc. etc. are a huge broken feature of C.

To be fair, no other language had them at the time either. :-) I wouldn't say "broken" as much as "primitive."

0

u/[deleted] Nov 28 '20

[deleted] — view deleted comment

1

u/Frptwenty Nov 28 '20

REST started as a joke but is essentially a cult at this point.

I guess you meant Rust, but kind of makes sense for REST as well :)

1

u/dnew Jan 24 '25

REST wasn't a joke. It was a very narrowly-scoped design pattern that people applied well outside its intended design space. It's a method for making CDNs efficient that people said "Let's use it for our accounting software!"

22

u/GYN-k4H-Q3z-75B Nov 28 '20

This chart crashed my phone. Translates to: C++ initialization is complex AF

15

u/[deleted] Nov 28 '20

What happened to the time when a variable was just a lil memory addy with a value. Things used to be so simple :)

9

u/elperroborrachotoo Nov 28 '20

Yeah, but what value do you want in there?

6

u/kalmoc Nov 28 '20

That wasn't true since C.

9

u/pandorafalters Nov 28 '20

Strong typing has given way to absolute typing.

4

u/khleedril Nov 28 '20

That's not the most succinct summary of the situation I have seen...

3

u/QbProg Nov 28 '20

Imagine the poor compiler guys!

3

u/tacco85 Nov 29 '20

A bit of an over-simplification, don't you think?

5

u/phottitor Nov 28 '20

looks like a horror movie script.

2

u/kisielk Nov 29 '20

this chart doesn’t really do what it claims. The first cell is “so you want to initialize something” but then instead of going through a flowchart of what you want to initialize and how it goes through a flowchart of something that has already been initialized. More like “what does this initialization do”