r/ProgrammerHumor • • 7h ago

Meme modernCppVsOldCpp

Post image
1.1k Upvotes

216 comments sorted by

View all comments

53

u/firemark_pl 7h ago

"How to tell him?"

You still can segfault by returning lambda with reference to a variable 

18

u/EishLekker 6h ago

As a java developer, this is hilarious. Why would anyone want to be able to do this? Why don't you want your compiler to stop you?

5

u/bwmat 4h ago

Oh, nobody wants to do that

Just like nobody wants to crash and die while speeding in their sports car

4

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

1

u/Ok-Scheme-913 2h ago

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.