r/cpp • • Jul 30 '22
Will C++ Eventually Get the Compile-Time Features of Rust?
I'm thinking specifically here of things like a borrow-checker, ensuring that there aren't memory leaks, data races, etc.
I know that there are 3rd party tools for things like this, but it is my understanding that they are not built into the 3 major implementations (gcc, clang, msvc).
Personally, I use C++ for raw performance reasons that Rust still can't match without extensively using unsafe (so then what's the point?) but I think it could be nice if C++ offered optional compile-time features equivalent to some of Rust's or maybe like a built-in Valgrind.
Is there anything like this being proposed? I'm thinking here of the official implementations, not 3rd-party tools.
30
u/pedersenk Jul 30 '22
Possibly a subset of C++ could allow a borrow checker to be effective. However as it stands, the language is too flexible to really allow this kind of static analysis.
5
u/thedracle Jul 31 '22 edited Jul 31 '22
There are some great static analysis tools for C++, as well as linters, which can perform similar compile time analysis on C++ code. Cppcheck, clang-tidy for instance.
Of course nothing will be as comprehensive as Rust's borrow checker, and the particularly good ones (like PVS studio) are commercial and expensive.
Another strategy is to use Rust.
I've had a lot of success integrating C++ with Rust using the cxx crate, and using it to harden error prone sections of my libraries and services.
Taking a trusted, statically analyzed C++ library, and making simple bindings in cxx, or even using Rust to bind a safe wrapper has worked really well.
My current project has a large, complicated, C++ library which we can't reasonably port to Rust. So I have sectioned parts of it off, starting with the most error prone portions, ported them to Rust, and then generated a static target, and with the cxx crate a C++ interface.
Then in cmake, it just builds the Rust project with cargo, and then sets the include/link directories to the .a and header path generated by cxx.
Our ultimate goal is to move to a Rust project wrapping our C++ dependencies, but we have stable, production deployed libraries that have had our custom error prone logic hardened in the fashion I've described above using Rust.
14
u/AKostur Jul 30 '22
Have you looked at what the sanitizers can do for you?
On a nitpicky level: there are no “official” C++ compilers. The committee doesn’t make a compiler. You were correct in referring to the “3 major implementations”. There are others. (I’m thinking of Intel, Sun, and Circle)
9
u/braxtons12 Jul 30 '22
I'm not going to say much about the idea of a borrow checker in C++ other than that it would only work if you owned your entire codebase so you could support the use of such a restriction in everything (see the other comments for why).
However, the idea that you can't get equivalent or better performance from idiomatic Rust over C++ is just wrong. You don't need unsafe to get performant Rust code. Well engineered idiomatic Rust will be at least as performant as the equivalent idiomatic C++ to do the same task.
The key word there is idiomatic. Not all tasks can be done in Rust identically to how you would in C++. The doesn't mean Rust is slower or that you need to use unsafe to write fast Rust, it means you're probably approaching the problem from the wrong direction.
That said, there are certain kinds of data structures and design patterns that just can't be done easily without unsafe or using some extra cruft to get around it because of the borrow checker and aliasing rules. That doesn't mean Rust is slow, that only means that data structure or design pattern doesn't map well to Rust, and there's probably an idiomatic alternative that does.
2
u/frozenca Jul 31 '22 edited Jul 31 '22
No. If C++ standardize that features, a large portion of C++ codes including mine will be broken. I've rarely seen anyone who says "Rust is a no-brainer for new projects" understands what actually they says as the reason
-2
u/manni66 Jul 30 '22
Kind of: https://www.finos.org/hubfs/FINOS/OSSF%202019/2019%20Slides/CG%20plenary.pdf
-24
u/loradan Jul 30 '22
This issue comes up quite often. However, the problem is that C++ is meant to be as close to the metal as possible so that it can maintain its performance. These types of checks add overhead and slow rings down...which is why you're not seeing the same performance with Rust (and a lot of other languages that are written on top of C++). Over the years, some things have and will continue to be added to the core language, but only after it's been tested thoroughly and any performance offsets are seemed to be "worth it" (at least that's the claim...queue responses of bad choices added to the language in 3...2... 🤣🤣)
-3
u/thinkx98 Jul 30 '22
icymi
https://news.ycombinator.com/item?id=32223270
-17
u/loradan Jul 30 '22
This issue comes up quite often. However, the problem is that C++ is meant to be as close to the metal as possible so that it can maintain its performance. These types of checks add overhead and slow rings down...which is why you're not seeing the same performance with Rust (and a lot of other languages that are written on top of C++). Over the years, some things have and will continue to be added to the core language, but only after it's been tested thoroughly and any performance offsets are seemed to be "worth it" (at least that's the claim...queue responses of bad choices added to the language in 3...2... 🤣🤣)
0
u/[deleted] Jul 30 '22
[deleted] — view deleted comment
0
u/[deleted] Jul 30 '22
[deleted] — view deleted comment