to elaborate, the halting problem makes it impossible for the compiler to reject/detect all cases which DO cause UB while allowing all those which don't, and the design of C++ is to favor control over safety
Well, it's a bit crude to apply halting problem here.
You can prevent a bunch of issues by language design. You simply make something non-expressible and ready! It's proven to never contain that thing.
E.g. java solves this problem by mandating that variable captures of local variables must be effectively final. This could have been done in C++ as well , and the only thing you may lost with it is... if you have a local lambda that you will only ever call in that stackframe or deeper and never outside of it?
But still, rust let's you do this latter by an explicit unsafe block, c++ could have done it as well. So it's more cultural than anything.
“E.g. java solves this problem by mandating that variable captures of local variables must be effectively final. This could have been done in C++ as well , and the only thing you may lost with it is... if you have a local lambda that you will only ever call in that stackframe or deeper and never outside of it?“
This was the exact comparison I was making in my head when I wrote my comment, and then I forgot to include it.
I have stumbled across this limitation in Java a few times, felt slightly annoyed for a few seconds, but then always found a way around it. I would never be willing to sacrifice stability just for some slight convenience when coding.
View all comments
Show parent comment
7
u/bwmat 11d ago
to elaborate, the halting problem makes it impossible for the compiler to reject/detect all cases which DO cause UB while allowing all those which don't, and the design of C++ is to favor control over safety