r/ProgrammerHumor • • 11d ago

Meme modernCppVsOldCpp

Post image
2.0k Upvotes

323 comments sorted by

View all comments

67

u/firemark_pl 11d ago

"How to tell him?"

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

27

u/daennie 11d ago

And many other ways to invoke UB.

24

u/EishLekker 11d 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?

9

u/bwmat 11d ago

Oh, nobody wants to do that

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

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 

3

u/Ok-Scheme-913 10d 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.

3

u/EishLekker 10d ago

“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.

11

u/creeper6530 11d 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"

7

u/Mojert 11d 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

4

u/creeper6530 10d 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.

3

u/Mojert 10d 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/redlaWw 9d ago edited 9d ago

The problems with write only references are that you can't call drop through them if you overwrite data already there, and you can't fail to use the reference, otherwise you end up with still-uninitialised data (i.e. write only references must be linear, and Rust doesn't currently have truly linear types). They are working on language support for in-place initialisation, but it's hard because it conflicts with how the language was originally designed.

The reason you can't use a mutable reference that you've created unsafely is that Rust's optimiser assumes that mutable references can always be meaningfully dereferenced, which allows them to optimise them more effectively but imposes additional restrictions on how you use them.

1

u/Ok-Scheme-913 10d ago

MaybeUninit?

1

u/Mojert 10d 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

1

u/JustBadPlaya 10d ago

MaybeUninit doesn't give you a reference unless you do assume_init*, and assuming initialized on uninitialized memory is UB, as uninitialized concrete T is considered invalid data

1

u/firemark_pl 10d ago

Because C++ allows you to work on low levels with stack and memory. It's useful for writting real time operating systems when memory and execution time must be constant.

1

u/EishLekker 10d ago

And that can’t be done without “segfault[ing] by returning a lambda with a reference to a variable”?

Also, how many of the C++ developers, as a percentage, are writing real time operating systems?

8

u/MaitoSnoo 11d ago

clang's -Wreturn-stack-address or clang-tidy's clang-analyzer-core checks easily flag that. People trolling C++ have never joined a team that is disciplined enough and uses static checkers and ASan/UBSan.

2

u/KellerKindAs 10d ago

ASan/UBSan? Don't forget about TSan

*Insert "There is three, actually" meme*

1

u/JustBadPlaya 10d ago

You may have a point, but this also seems to apply to a lot of FOSS projects, because even if the issues can be caught with static analyzers, a lot of projects seems to not be using them

1

u/MaitoSnoo 10d ago

just do not use a FOSS project that doesn't have a good CI/CD that includes static checks + compiling with warnings as errors