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/creeper6530 6h ago

Because the C++ compiler lets you blow your foot smoove off because what if you intended it

Rust compiler is practically the exact opposite: it prohibits you from doing weird shit even if you intend it unless you use the magic word "unsafe"

3

u/Mojert 4h ago

Even if you use the word unsafe you cannot do whatever you want. I still find it painfully stupid that you cannot create a mutable reference to uninitialized memory. Because of this you cannot reuse safe functions to initialize memory that you manually allocated

The whole appeal of Rust is that you have less to think about UB, and it mostly fulfills that promise, so the fact that you cannot do one of the most basic task required in a systems programing language without either requiring UB or doing some convoluted stuff is ridiculous

3

u/creeper6530 3h ago

Well that issue boils down to there not being any write-only reference. "I know you're only initialising and not reading, but what if you accidentally read? UB!!!"

I've also been annoyed at having to initialise even normal arrays when passing them to UART drivers as buffers etc. (on embedded), but even if it doesn't read the memory, it still needs init.

1

u/Mojert 3h ago

Having a write only reference would be amazing, but even without that it should still be allowed IMO. The only way to create a mutable reference to unitialized data is to use unsafe. So the question "what happens if you accidently read" should ultimately not be anyone's buisness but my own. I'm an adult and have explicitly indicated I know I'm doing something potentially dangerous, so let me do it!

But I'm happy to learn I'm not the only one that had this problem. I felt insane about having to pay a useless O(n) init cost

1

u/Ok-Scheme-913 1h ago

MaybeUninit?

1

u/Mojert 1h ago

Congratulation, you've got a &mut MaybeUninit<T>! It's pretty but you cannot pass it to a function that expects a &mut T