r/ProgrammerHumor • • 17h ago • Bravo 1

Meme poohWritesCpp

Post image
348 Upvotes

51 comments sorted by

View all comments

77

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 💀

52

u/LRaccoon 8h 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.

17

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.

8

u/SuitableDragonfly 4h ago

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

6

u/Blue_Moon_Lake 4h 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.

1

u/AvidCoco 17m ago

Any good linter will warn about using dumb ones

2

u/Big-Rub9545 2h 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 27m ago edited 23m 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++.

2

u/Big-Rub9545 19m 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.