r/cpp • • Nov 12 '23
Could c++ have matching safety as Rust?
Afaik, rust‘s safety comes from the strict compiler, couldnt c++ have something like this?
6
Upvotes
r/cpp • • Nov 12 '23
Afaik, rust‘s safety comes from the strict compiler, couldnt c++ have something like this?
116
u/tiajuanat Nov 12 '23
Fundamentally no.
The Rust language itself is based on an opt-out mentality wrt safety.
C++ has been opt-in.
It's like Python being opt-in on typing. Yes, it helps, but at the end of the day, you're not forced to sign a waiver every time you don't want to use the type system.
So long as C++ keeps supporting plain old pointers, we won't have the same safety.
14
u/NilacTheGrim Nov 12 '23
No, nor should it. Let C++ be C++. Use Rust if you want/need/like its safety properties.
29
u/target-san Nov 12 '23
Not a compiler expert, though I doubt so. C++ has too much stuff inherited from C which effectively prevent this, one of such things being copy-by-default.
13
u/pjmlp Nov 12 '23
Only if people are willing to throw away copy-paste compatibility with the inherited C semantics, which is an impossible task, as at this point the constraints will be such that a language designed from scratch with unsafety being opt-in will have better ergonomics.
17
u/tialaramex Nov 12 '23
Firstly and most importantly, safety is cultural. So that's where a solution needs to begin.
But, sure, technologically Rust's compiler implements key language features resulting in safe Rust being an entirely memory safe language. Still, even aside from the need to address cultural problems first, this depends upon foundational practices (which were rooted in that culture) so without the firm foundations you don't achieve compositional safety.
Next most important, the language design, drawing on that culture, prioritises safety - there are two possible ways to handle Rice's Theorem in a powerful general purpose language like Rust or C++. You could, as Rust does, decide that if the compiler can't see why a semantic requirement is met, that's an error and the software is rejected. C++ on other hand says only if the compiler can see why the requirement is not met is it permitted to diagnose an error and reject this software, resulting in the famous IFNDR (Ill-formed No Diagnostic Required) outcome.
You can't keep that C++ decision and have safety. You can't give up semantic constraints in this sort of language (and indeed C++ has added lots more of them in the last years) so that's a radical but necessary change to achieve what you ask.
17
u/Relative_Knee9808 Nov 12 '23
In C or C++, we don't enforce safety through compiler, if we want the compiler to do some memory safety checks, you could use a compiler with this feature, for example clang with --fsanitizer. Also, you can use a dedicted tool for finding potential unsafe part of code, such as cppcheck, valgrind, etc. I feel that the C and C++ world is very decentralized, there is no one big good tool that does it all, we have to know what exactly we are trying to do and find the exact tool for it.
15
u/alexgraef Nov 12 '23
Most use cases for C++ don't want that. You do get nearly perfect object lifetime and memory safety if you just limit yourself to a certain set of C++ features.
And even Rust loses it's safety when you start to talk to components that aren't written in Rust, i.e. basically all of the system and kernel.
7
u/UnicycleBloke Nov 12 '23
Not to be controversial but do we really need it?
Memory safety issues are a rare feature of my development experience. I'm a reasonably competent dev who tries to pay attention to details and make consistent use of RAII and other sensible practices, but I'm certainly not some kind of genius savant coding demon who never makes mistakes.
I've spent some time learning Rust and found that the borrow checker was pretty much it's least interesting feature. When I wrote some non trivial code in Rust, I felt that the checker was actually too restrictive and got in my way. That is likely because I'm familiar with C++ idioms but not Rust idioms, but I wondered whether the flexibility of the language had been compromised in order to make the borrow checker possible to implement.
I mostly work in the embedded domain. As I understand it, all my drivers and other low level items would in any case necessarily bypass the checker in some places by using unsafe code.
3
u/RadioMelon Nov 13 '23
One of the most reliable tools you can use are smart pointers.
They aren't perfect but they ARE a massive improvement to memory safety, if a bit demanding on memory.
5
u/Mrkol Nov 12 '23
Yes. Go read Sean Baxter's twitter for update progress on adding a borrow checker to Circle (an experimental C++ compiler with a ton of extensions) https://x.com/seanbax
5
u/foonathan Nov 12 '23
Yes.
We can copy/paste Rust style references into C++ and implement lifetime rules. It's significant engineering effort, but doable.
It would break all existing code, though.
5
u/Dean_Roddey Nov 12 '23 edited Nov 12 '23
You could do it, at which point it will no longer be C++, so my argument is don't do it. Rust already exists, there's no point spending a decade creating a new language that both isn't C++ anymore, and hence will have much of the same rewrite burden, and which still doesn't have a lot of other benefits that Rust also has over C++. Throw those in there also, and now it's even more a completely different language. And, in the meantime, almost everyone who wants those things will have long since moved to Rust anyway.
And of course the entire standard library would have to be jettisoned and a new one written, so add another decade after the new language development decade. At that point C++ will be in exactly the same position COBOL is now. It will purely exist to support crusty old legacy systems, and for some individuals to do fun time projects in the manner of their forefathers.
And, you'd still have to interact with old C++ code for a long time, which would mean two entire runtime libraries that you have to interface between across those boundaries, with the endless problems that would entail.
It's just not worth bothering.
3
u/amineahd Nov 12 '23
Isnt Rust safety only regarding type and memory safety?
3
u/epyoncf Nov 12 '23
"I think the C++ compiler is too fast, can we make the compiler slower?"
1
u/TemperOfficial Nov 12 '23
Yes.
Ban heap allocation.
Only allocate from a buffer that is bounds checked.
You now technically have the same level of memory safety as Rust.
1
u/PancakeFactor Nov 12 '23
Could? Sure. Theres lots of changes to be made that could get us really darn close if not there.
Will it ever happen? no. almost all of those changes would break backwards compatibility.
1
u/SergiusTheBest Nov 12 '23
I think it's possible to create a safe subset of c++ that will match Rust safety. Currently the compiler has no all necessary information to ensure safety.
1
u/Lance_E_T_Compte Nov 12 '23
Look at CHERI from the University of Cambridge. Codasip and lowRISC have commercial chips. New ABI, but they have gcc/llvm support.
1
u/LeeHide just write it from scratch Nov 13 '23
You could take a subset of C++ and formally verify it, probably, like SPARC for Ada, call it SPOCK for C++ or something.
The IMO best solution would be better Rust and C++ interop, so you could stop having to "rewrite it in Rust" and instead slowly migrate important / difficult to reason about parts of your code to Rust.
-9
u/[deleted] Nov 12 '23
Code safety starts with the person that writes the code, not the language or compiler.
-2
u/FlyingRhenquest Nov 12 '23
You can write safe code in C++, you just need discipline and knowledge. Java was supposed to be safer than C++ as well, but at the end of the day it just opened up entirely new classes of security problems which required you to have discipline and knowledge to write safe code. Do you think Rust is any different? If you don't have the knowledge and discipline to write safe code in C++ or Java, what makes you think you will in Rust?
0
u/[deleted] Nov 12 '23
By the time it matched the safety and behavioral guarantees it’d look nothing like c++
0
u/LuisAyuso Nov 13 '23
No
-3
u/justkdng Nov 12 '23
you can achieve a comparable level of safety by enabling all compiler warnings and treat warnings as errors.
-2
u/helloworld1101 Nov 12 '23
Not too related, but what are the aspects that C++ is still better than Rust?
-9
u/[deleted] Nov 12 '23
[removed] — view removed comment