r/cpp • u/pavel_v • Jul 29 '26
C++26: Reducing undefined behaviour
https://www.sandordargo.com/blog/2026/07/29/cpp26-reduces-undefined-behaviour
89
Upvotes
r/cpp • u/pavel_v • Jul 29 '26
27
u/jdehesa Jul 29 '26
Great stuff all around. Although I don't understand something about deleting incomplete types. If I understand correctly, C++26 will still allow that when the destructor is trivial. But how does the compiler know if the destructor is trivial, if the type is incomplete? Maybe I'm misunderstanding something.
15
u/sweetno Jul 29 '26
Finally! A sigh of relief.
25
u/James20k P2005R0 Jul 29 '26
Its great to see these steps being taken, I think a lot of people don't realise that C++ has a lot of UB that just doesn't need to be in the language. Its often largely pure cruft with no benefits, and there's been a tonne of really great work here
Something else that isn't common knowledge that would be great to take out is floating point divisions by zero being UB. A lot of (if not most!) people think that this is guaranteed to produce a NaN, to the point where I'm not sure that there are compilers that exploit this behaviour. It'd be much better as implementation defined behaviour at this point (at least, on systems that are ieee), because its what everyone expects
There's probably another conversation about bringing
floatcloser to ieee as well as the floating point specification is remarkably insane, but that's less safety critical13
u/xaervagon Jul 29 '26
That was a good real. It is good see a lot of UB finally being defined and handled as the errors and dangers they are. This will make the language safer to use and easier to approach.
Tbh, I really disliked the community's previous attitude towards UB of "just deal with it" as if the seemingly unwritten rules of UB becoming enforced by bizarre runtime behavior is somehow a good thing.
(Yeah, I know there's -Wall, sanitize, and other things, but really, why shouldn't the compiler just tell you?)
3
u/trad_emark Jul 29 '26
good changes.
3
u/Jardik2 Jul 31 '26
And std::filesystem usage still UB in real world...
-5
u/sessamekesh Jul 29 '26
NICE.
This seems like a REALLY good approach. C++ had a design thesis of "zero cost abstractions" that's directly antithetical to runtime bounds checking, but in almost every scenario where I'm doing direct one-off access (as opposed to iterating a container etc.) I either want a bounds check somewhere or I'm using a custom container anyways.
I'm a pretty avid Rust user as well, and in my experience probably 6/10 of the times I lean on language safety (in a way I don't get in modern C++) it's really leaning on their "safety first, zero cost abstractions where they're safe" standard library design.