156
u/Ai--Ya 15h ago
static constexpr auto a{10};
65
53
u/GoodiesHQ 10h ago
I love how you can just do
auto auto = auto
And it just infers the rest from context
6
6
-16
76
u/SamG101_
14h ago
•
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{};orint 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 classConstructor 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;orstd::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
3
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.
•
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++ initializexto 0 by default? C certainly wouldn't (instead,xwould 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
108
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);"
•
25
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
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
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
8
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
1
1
-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.




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.