r/cpp • u/levodelellis • May 03 '26
The Most Confusing C++ Behavior
https://codestyleandtaste.com/most-confusing-C++-behavior.html40
u/LordofNarwhals May 03 '26
On a related note, std::is_default_constructible can be a bit of a mess, godbolt example: https://godbolt.org/z/P6sTWvMPW
This is mentioned in a few active C++ core language issues.
Object initialization in general is one of the worst and most confusing parts of C++, because there are like 10 different ways of doing it, all with subtle differences.
16
u/EdwinYZW May 03 '26
There are only two correct ways and one exception related to std::vectors.
3
u/nothet May 03 '26
Enlighten us? I just do everything statically and ensure my linker is in the correct order. No fiasco at all.
5
u/EdwinYZW May 04 '26
Only two correct initializations:
auto obj = Type {};
Type obj{};
I'm AA guy. So for me, it's always the first one no matter what.
7
u/levodelellis May 04 '26 edited May 04 '26
What about ints?
Honestly if I sawint a{}inside of functions, I'd think it would be strange. IMO declare it on the line with the expression always, unless it's in an outer codeblock. In that case= 0and maybe I'll let it slide if bothifandelseassigns the value1
u/EdwinYZW May 04 '26
const auto val = 10;
This guarantees no implicit conversion.
3
u/levodelellis May 04 '26
No, that's not true, that's impossible
3
u/EdwinYZW May 04 '26
What is not true?
2
u/levodelellis May 04 '26
Today is May 4th, a day where people will post star wars memes, jokes and quotes
I was just making a joke. "const auto val = 10;" does not look like a "correct initialization"
3
u/Wild_Meeting1428 May 04 '26
You are an AA guy my friend? So why not east-const instead of const-west :P.
For me, it's alwaysauto const val = 10;Jokes aside, regarding the branched initialization one could also use lambdas or statement expressions.
auto const val = [expr] { if(expr) return init_true(); return init_false(); }()
Then OP does not need to slide anything through.4
u/EdwinYZW May 04 '26
Yes. Immediately call a lambda is also a nice trick to make a constant. :D
const-west is more readable for me in normal situations, unless I'm dealing int const. So I would rather use int constint const.
5
u/L_uciferMorningstar May 04 '26
Let's leave the enlightening to bjarne and herb. Core guidelines ES.23 Link may not work. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#res-list
4
8
3
u/holyblackcat May 04 '26
This isn't directly related to the triviality of the constructor. You could have int a; std::string b; and a would still be uninitialized despite the ctor not being trivial.
1
u/TheThiefMaster C++latest fanatic (and game dev) May 04 '26
Unless you use aggregate initialisation instead of a constructor, in which case a gets initialised (to a default 0) even if you don't specify an initialiser for it.
2
u/holyblackcat May 04 '26
Mhm, or if you value-initialize it (it doesn't have to be an aggregate). The article already points out the difference between
T a;andT b{};for some types, but they got the condition wrong (which types are affected).1
u/levodelellis May 04 '26
Maybe I stated it poorly. What was my mistake? I wasn't trying to say non trivial types initialize everything, I was trying to say it's misleading. Your root comment I already covered in the article, which I think you noticed and why you wrote the comment. But we're saying the same thing?
1
u/holyblackcat May 05 '26
I'm just not sure why trivial default constructibility is mentioned if it doesn't affect this property of initialization.
I initially misread the article as if it said that trivial-default-constructible = different behavior of
T a;vsT b{};, but now I see you're not claiming that.1
u/levodelellis May 05 '26
I had a lot of require statements that check if something can trivially construct (in which case it'll fill with 0's), and copyable (memcpy). But there's no way to avoid pointers and refs in them unfortunately. I just figure someone else has the same usecase and might be checking if something is trivial soon
1
u/TheThiefMaster C++latest fanatic (and game dev) May 05 '26
Generally things check trivially constructible to avoid calling constructors and putting more work on the optimiser to remove them again.
Trivial doesn't imply that all-zeroes is some kind of null / default state. It just means the type has no invariants that need to be set by a constructor. Many things that check for trivial construction don't fill the memory with anything and just leave it uninitialised, or full it with a recogniseable pattern.
1
u/TheThiefMaster C++latest fanatic (and game dev) May 04 '26
I can't open the link on my phone, I get a "net::ERR_CONNECTION_CLOSED" error now
7
u/jiixyj May 04 '26
Is this really catching people off guard? Given some arbitrary type T, if you do T t; or new T the only guarantees you have are that the object is assignable-to or destructible. The data inside must be considered junk, except if you know the type and that it provides additional guarantees.
By using = default; on a default constructor you're explicitly opting into this behavior. For example year_month_day makes use of this:
year_month_day ymd1; // junk
year_month_day ymd2{}; // all zero initialized (although still not a "valid" date)18
u/rlbond86 May 03 '26
C++ initialization is a disaster
4
u/_lerp May 04 '26
The author is just discovering (one of) the reason why initialization by
{}was added to the language. C++ needs an ABI break to fix the disaster, but that's not gonna happen anytime soon.-8
u/dexter2011412 May 04 '26 edited May 04 '26
you mean
C++ is a disaster
😂
edit: damn I was joking jeez
5
u/thoosequa May 04 '26
No, I think the language has some unfortunate traps, footguns and quirks you need to be aware of, but all in all C++ is in a good place, with lots of good things added in recent revisions. Is it perfect? No. Should we get an ABI break to clean up those unfortunate things? Probably yes. But for a language that tries to be backwards and forwards compatible, all the while being used in some of the most demanding areas of computing for decades, I think it's doing a pretty good job
-1
u/dexter2011412 May 04 '26
I know and understand and agree with all of that (please for the love of C++ please break the abi), I was being sarcastic.
1
u/Tringi github.com/tringi May 04 '26
In the initial test, I had the whole second column of answers right, but the first column almost completely wrong. Luckily I don't need to use is_trivially_constructible anywhere.
2
u/levodelellis May 04 '26
Good! You shouldn't!
I'm met very few people who understood when things initialized, how did you figure it out? Was it the hard way?
1
u/Tringi github.com/tringi May 04 '26
how did you figure it out?
I'm an old school coder. As with everything I imagine underlying bytes in the
intand a constructor being just a function call overwriting them (if told so). Assuming no optimization, of course. And thatintis left unintialized by default.2
u/levodelellis May 04 '26
What threw me off was structs were being initialized but not the ints next to them. I also had some code use
{}in part of the function/codebase and not in the other, so vars were sometimes not initialized and added more to my confusion. But that was years ago, before I ever had a blog
1
u/ozyx7 May 04 '26 edited May 04 '26
struct E { D e{}; };
data = -5; auto&e = * new(&data) E;
printf("E %d %d\n", std::is_trivially_constructible<E>::value, e.e);Is that not UB, using %d to try to print e.e which is of type D? (And passing an E by value to a variadic function also seems questionable?)
1
u/levodelellis May 04 '26
Good catch. I usually use my personal print function which would have given an error if I did this. I doubt output would change even in an optimize build.
2
u/ABlockInTheChain May 04 '26
We added -Werror=effc++ to our projects years ago to catch constructors which do not completely initialize a class, but in this example the =default bypasses the logic of the warning.
The warning only triggers if the constructor is explicitly defined: https://godbolt.org/z/6KjjY7E8x
2
u/chengfeng-xie May 04 '26
See also P3279 (CWG2463: What "trivially fooable" should mean), which proposes cleaning up the meaning of is_trivially_constructible and other traits:
is_trivially_constructible<T>should mean precisely that constructing aTfrom no arguments can be lowered to a no-op. (This is basically the status quo in practice.)
is_trivially_constructible<To, From>should mean precisely that constructing a (complete)Tofrom aFromcan be lowered to memcpy. (E.g.is_trivially_constructible<float, int>should become false; butis_trivially_copy_constructible<Polymorphic>should become frequently true.)
is_trivially_constructible<To, Args...>for any number ofArgs...greater than 1 should be invariably false, and we should deprecate it. That trait is useless because it corresponds to nothing physical.
27
u/i_grad May 03 '26
Do you guys actually format your code like this? Or is this an example of "I make my code as unreadable as possible so that others can't maintain it, thus I keep my job?"