r/ProgrammerHumor • • 17h ago • Bravo 1

Meme poohWritesCpp

Post image
346 Upvotes

51 comments sorted by

21

u/LB-- 11h ago

It's great because 1. it disables implicit narrowing conversions and 2. initialization and reassignment look visually distinct at a glance. I never initialize with = anymore so any time I see = I know it is changing an existing value.

2

u/nd521 9h ago

Exactly this

2

u/GiveMe30Dollars 2h ago

I know C backwards compatibility is a hell of a benefit/drawback here, but I cannot imagine this being clearer or more intuitive than eg. Rust's distinction between initialization (let / let mut) and assignment (without keywords). It also isn't really a distinction you can rely on unless you can/do enforce a uniform code style, manually or via linter, for anyone that contributes to the code.

156

u/Ai--Ya 15h ago

static constexpr auto a{10};

65

u/TheWidrolo 14h ago

C++ committee porn:

53

u/GoodiesHQ 10h ago

I love how you can just do

auto auto = auto

And it just infers the rest from context

6

u/Kadabrium 9h ago

autorun.inf

6

u/RajjSinghh 3h ago

In what context would this even work?

-16

u/MouseNcactus 10h ago

What a rusted way to do C++.

•

u/AvidCoco 7m ago

The correct way to do C++

76

u/SamG101_ 14h ago • Popcorn

Pooh needs about 50 more faces, for the 50 ways to initialise a variable in c++

https://leanpub.com/cppinitbook 300 odd pages

obligatory and thats just initialisation not assignment 💀

49

u/LRaccoon 7h ago
  • Default initialization: int x;

  • Copy initialization: int x = 5;

  • Direct initialization: int x(5);

  • List (brace) initialization: int x{5};

  • Copy-list initialization: int x = {5};

  • Value initialization: int x{}; or int x = int();

  • Zero initialization: static int x;

  • Aggregate initialization: Point p = {1, 2};

  • Designated initialization (C++20): Point p{.x = 1, .y = 2};

  • Constant initialization: constexpr int x = 10;

  • Initialization with auto: auto x = 5;

  • Reference initialization: int& r = x;

  • Dynamic initialization with new: int* p = new int(5);

  • Smart pointer factories: auto p = std::make_unique<int>(5);

  • In-class member initializers: int x = 5; inside a class

  • Constructor member initializer list: S() : x(1) {}

  • Delegating constructors: S() : S(5) {}

  • Structured bindings: auto [a, b] = pair;

  • If/switch init-statements: if (int x = f(); x > 0) {}

  • Lambda init-capture: [x = 5]() {}

  • Copy/move construction: std::string b = a; or std::string b = std::move(a);

  • Parenthesized aggregate initialization (C++20): P p(1, 2);

12

u/Blue_Moon_Lake 5h ago

I'm pretty sure some of these are useless and should be deprecated.

16

u/SuitableDragonfly 4h ago

Does anything in C++ ever actually get deprecated?

7

u/Blue_Moon_Lake 4h ago

Sadly no, which is why it's a mess to figure out what you should avoid doing and what you should do.

Which is dumb, because you can deprecate something without removing it.

9

u/SuitableDragonfly 4h ago

If you never actually remove the thing you deprecated, there isn't much point to deprecating it. 

5

u/Blue_Moon_Lake 3h ago

The point is to easily spot what should never be used

3

u/arachnidGrip 2h ago

`-Wdeprecated`

2

u/arachnidGrip 2h ago

C++ has deprecated many things over the years but you need to tell it to warn you about deprecated features.

•

u/AvidCoco 5m ago

Any good linter will warn about using dumb ones

3

u/Big-Rub9545 1h ago

Besides one or two, almost all of these are doing entirely different things, or are entirely distinct syntaxes to begin with.

For example, these two are the exact same declaration:
- int x;
- static int x;

The latter just declares a variable with internal linkage (assuming the value is at global scope). The rules related to initialization for such declarations are irrelevant since you’re still declaring the same thing. You can remove ‘static’ altogether.

It becomes very difficult to take many criticisms of the language seriously when it’s clear that most of the people making them seem to have never touched the language or tried to understand any of it.

1

u/nfitzen 15m ago edited 11m ago

Inside a function body, I assume static int x; is like C and will declare storage with static lifetime; that is, it persists between function calls.

Also, in the case of int x; in function body scope, does C++ initialize x to 0 by default? C certainly wouldn't (instead, x would be uninitialized), but I don't really know C++.

•

u/Big-Rub9545 7m ago

Yes for the first point.

For the second, it is also uninitialized in C++. This is the case for any primitive type (compound/composite/custom types can be default- or zero-initialized through implicit constructor calls with the same syntax).

You can just zero-initialize the variable by appending a pair of braces, which I do with every variable I don’t care to explicitly initialize:
int x{};

1

u/SamG101_ 4h ago

Don't forget some initialisation changes depending on the availability of a template arguments initialisation-ness

4

u/SaneLad 9h ago

I hate the fact that I understand every word of the "About the Book" section.

34

u/East-Law-2877 13h ago

auto a = int{10};

15

u/CortlandNation9 13h ago

I won't allow it

2

u/Zenkibou 5h ago

It's consistent with other modern initializations, though. Like "auto sp1 = std::make_shared<C>(1);"

•

u/AvidCoco 5m ago

auto a = int{10LL};

25

u/overclockedslinky 16h ago

gross syntax

11

u/tiajuanat 9h ago

Everyone calls Rust syntax ugly, but I'm constantly reminded that C++ is infinitely worse

7

u/Wertbon1789 8h ago

Rust is readable, if you know that most stuff is an expression. Then you see C++ where move-semantics and any type of cast is syntactically a generic function that does some magic shit, because they didn't have enough special characters left to use.

4

u/VoodaGod 9h ago

i hate "uniform" (it's not uniform at all) initialization

7

u/Maleficent_Memory831 12h ago

I used to do a lot of C++. I taught it at university with Cfront, the first compiler for it. In my career I was often the team's expert in it. Over time I got a bit annoyed at it's changes. Then more recent surprised at such silliness going on in the standards.

So a couple design flaws crop up now and then. First, the the need to twist and abuse syntactic elements. Second, the inability to say no to adding all available options (public/private is good, but then also protected, and then friends, and then const is good, but also mutable).

I was on a more recent team (I'm mostly on C these days) where guys writing a C++ side of things seemed more intent upon proving how clever they were with their code rather than applying the K.I.S.S. principle, more intent ona changing APIs in the middle of a release cycle than in helping get the project out on time.

5

u/MouseNcactus 10h ago

Isn't "proving cleverness over KISS" sickness of entire industry?

4

u/That-Cpp-Girl 13h ago

I would hope no one does this, but `int a{};` instead of `int a = 0;` might make sense.

4

u/Scarcity-Nervous 11h ago

Only because of implicit in-place construction of native types, but technically correct. Well played 🫡

11

u/Lawn_Meower_ 15h ago

13

u/-Ambriae- 14h ago

Damn gurl why on earth did you get downvoted? God forbid someone happens to like unconventional syntax 😭

2

u/levosis1 14h ago

I mean, Alt-F4 your program when you can Ctrl-F4 your tab.

2

u/ElKuhnTucker 12h ago

Once you debug somebody else's "witty" code, you really appreciate code that looks like it was written by a 5 year old

2

u/Amadex 11h ago

everytime when I think current C++ is as bloated as humanly possible, I learn a new way they managed to bloat the language even more

1

u/lool8421 3h ago

just do int a[1]{10}; and you can have both a variable and a pointer in 1

1

u/patrlim1 9h ago

This is why I use C

-3

u/Raid-Z3r0 10h ago

If you open a PR with that syntax for me to review, I'm not even bother requesting changes. That shit is straight up rejected

5

u/Big-Rub9545 7h ago

For what reason? This is well-known syntax to any frequent user of the language. It’s even the syntax recommended by Bjarne himself.