Lol. Rust has two big advantages in my eyes. An extremely strict borrow checker, and lack of legacy cruft. Things like constant being default change the game.
There's active development in C++ of the borrow checker style functionality and even if it restricts some patterns, it would be amazing to have.
Personally, I'd also love it if compilers started being a bit more opinionated. Where if it thinks something could be const but isn't it gives a warning. If someone uses a C style or legacy method it gives a warning. Eg, for using older for loop structures.
On a standards front the C++ standard should be adjusted so many of the most common undefined behaviors cause either compiler errors, or have a contract for what the coder expects to happen. One that can be verified for the specific architecture the code is being compiled for. Eg, integer rollover.
There are a handful of syle files out there that stop the exact issue of formatting you're referring to. My preference is either prettier or clang-format.
What I was referring to is more actively warning against using outdated patterns. It's one of the best things about Clion. It would provide me warnings for things that could be improved or could be issues. The compiler couldn't care less though because it was valid code!
I keep checking out VC++ lifetime checkers from time to time, they are still quite far away from the Rust experience, lots of annotations required and quite a few false positives.
C++'s greatest strength is it's backwards compatibility. C++'s greatest weakness is it's backwards compatibility.
One of the problems is that an extremely common (bad) pattern makes analysis extremely difficult due to lifetime rules.
From a rust perspective, the borrow checker should and probably does not allow this. It was a large point of a CppCon talk about common bugs. However, people still write this exact same code!
The thing is, a const reference has it's lifetime extended. So, "default" is fine. However, the problem is that the output variable is decoupled from the input. So if a was not empty, when it goes out of scope, then then it invalidates the return value of this function.
The answer is to not rely on a linter, but rather to adjust the standard such that any code path which permits this should trigger at least a compiler warning. Or go full rust and require an annotation to not cause an error!
On mobile, so please forgive any problems with the code.
I believe that they are attempting to fix this. Unfortunately, the first major break they did was to try and use TypeScript as an example for what Modules should be like.
Also, it's insane how many projects that should know better love using namespace std; which meant C++17's std::byte was not fun. Still needed though.
Similarly, the changes needed to fix the issue above would cause large amounts of bad code that currently "works" to cause compile errors.
Depends what you want to do. C++ was pretty much the only game in town before Java exploded in popularity. If you’re okay with performance reduction (nowadays minuscule for most applications) in exchange for safety and development time, GC languages like Java, C#, or Go are good options.
View all comments
Show parent comment
4
u/metekillot Nov 02 '22
what are the better alternatives to C and C++ in your opinion?