5 ms·
Modern C++ is safer than old C++, but i doubt it is safer than Rust.
by guardian5x 4y ago
Modern C++ is safer than old C++, but i doubt it is safer than Rust.
- P5fRxh5kUvp2th 4y agowhy do you doubt that? safe rust is organized just like safe C++.
- aldanor 4y agoSafe Rust prevents you from doing stuff, it just won't compile. More importantly, it prevents your dependencies from doing stupid stuff if they're forbid(unsafe). "Safe C++" you can segfault or mem leak in a single line. Good luck reading through a few dozen k lines of code of a 3rd party library to check if they adhere to every single convention of "safe C++".
- karmakurtisaani 4y agoAlso, "safe c++" is still c++. You can write it as safe as you want, but all it takes is one 3 month contractor to come shit all over you codebase and leave. If your company doesn't have enough resources to control what goes in at all times, you will easily get stuck with fixing someone else's memory leaks. At least rust has built-in safeguards against that.
- nequo 4y ago> you can segfault or mem leak in a single line. Good luck reading through a few dozen k lines of code of a 3rd party library You have articulated one of the biggest motivations I have for avoiding C++ in scientific computing. An R library that uses C++ to speed up a clustering algorithm that seemingly arbitrarily segfaults on my input is not the thing I should be having to debug.
- saghm 4y agoI think it's important to note that Rust can still leak memory in safe code. Memory leaks are not "unsafe"; they're a performance issue and could possibly lead to panics due to running out of memory, but panicking is not unsafe (i.e. it's deterministic and not a source of undefined behavior). Rust provides a lot of absolutely critical guarantees, and I fully agree that it's much safer than C++, but I think it's important not to overstate the benefits (not that you did say that Rust prevents memory leaks, but I worry that someone might incorrectly infer that due to the way it was mentioned for C++).
- UncleMeat 4y agoRust (mostly) makes you do it right. C++ happily lets you pass a temporary string to a function taking string_view and blowing yourself up. "Just use smart pointers everywhere" is not actually sufficient to prevent serious bugs.
- CJefferson 4y agoThe problem with "Safe C++", is you immediately hit unsafe behaviour as soon as you write v[x] for a C array, std::array, or std::vector.
- netr0ute 4y agoThen just don't do that, because v.at(x) does exactly the same thing and is safe and requires a whopping 3 characters more to type.
- CJefferson 4y agoYes, but that is one of many, many small things you have to remember and never get wrong. I wonder how many C++ code bases never use v[x] on containers where it is undefined behaviour to go out of bounds? I've not seen one myself.
- saghm 4y agoLiterally all UB is avoidable if you just "don't do" the things that cause it, and yet it still happens all the time. You might be right that this specific source of UB might not be a large issue in practice, but there are so many different causes of it that overall the strategy of "just don't write code that causes UB" is empirically not effective at scale.